|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace IQParts\Cache\Adapter; |
|
4
|
|
|
|
|
5
|
|
|
final class NamespaceAdapter implements CacheAdapterInterface |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @var string |
|
9
|
|
|
*/ |
|
10
|
|
|
private $namespace; |
|
11
|
|
|
/** |
|
12
|
|
|
* @var CacheAdapterInterface |
|
13
|
|
|
*/ |
|
14
|
|
|
private $cacheAdapter; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* NamespaceAdapter constructor. |
|
18
|
|
|
* @param string $namespace |
|
19
|
|
|
* @param CacheAdapterInterface $cacheAdapter |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct(string $namespace, CacheAdapterInterface $cacheAdapter) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->namespace = $namespace . ':'; |
|
24
|
|
|
$this->cacheAdapter = $cacheAdapter; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $key |
|
29
|
|
|
* @return mixed |
|
30
|
|
|
*/ |
|
31
|
|
|
public function get(string $key) |
|
32
|
|
|
{ |
|
33
|
|
|
return $this->cacheAdapter->get($this->namespace . $key); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $key |
|
38
|
|
|
* @param $value |
|
39
|
|
|
* @param int|null $ttl |
|
40
|
|
|
* @return void |
|
41
|
|
|
*/ |
|
42
|
|
|
public function set(string $key, $value, int $ttl = null): void |
|
43
|
|
|
{ |
|
44
|
|
|
$this->cacheAdapter->set($this->namespace . $key, $value, $ttl); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $key |
|
49
|
|
|
* @return void |
|
50
|
|
|
*/ |
|
51
|
|
|
public function delete(string $key): void |
|
52
|
|
|
{ |
|
53
|
|
|
$this->cacheAdapter->delete($this->namespace . $key); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $key |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
public function keys($key = '*'): array |
|
61
|
|
|
{ |
|
62
|
|
|
if (substr($key, -1) !== '*') { |
|
63
|
|
|
$key .= '*'; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** @var string[] $keys */ |
|
67
|
|
|
$keys = $this->cacheAdapter->keys($this->namespace . $key); |
|
68
|
|
|
$allowed = []; |
|
69
|
|
|
$len = \strlen($this->namespace); |
|
70
|
|
|
|
|
71
|
|
|
foreach ($keys as $cacheKey) { |
|
72
|
|
|
if (0 === strpos($cacheKey, $this->namespace)) { |
|
73
|
|
|
$allowed[] = substr($cacheKey, $len); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $allowed; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param $key |
|
82
|
|
|
* @return int |
|
83
|
|
|
*/ |
|
84
|
|
|
public function ttl($key): int |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->cacheAdapter->ttl($this->namespace . $key); |
|
87
|
|
|
} |
|
88
|
|
|
} |