|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace RemotelyLiving\PHPCacheAdapter\CacheItemPool; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Cache; |
|
8
|
|
|
use Psr\SimpleCache as PSRSimpleCache; |
|
9
|
|
|
use RemotelyLiving\PHPCacheAdapter\SimpleCache; |
|
10
|
|
|
|
|
11
|
|
|
final class CacheItemPool implements Cache\CacheItemPoolInterface |
|
12
|
|
|
{ |
|
13
|
|
|
private PSRSimpleCache\CacheInterface $cache; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var array<string|int, array<string, \RemotelyLiving\PHPCacheAdapter\CacheItemPool\CacheItem>> |
|
17
|
|
|
*/ |
|
18
|
|
|
private array $deferred = []; |
|
19
|
|
|
|
|
20
|
|
|
private CacheKeyBuilder $cacheKeyBuilder; |
|
21
|
|
|
|
|
22
|
|
|
private ?int $defaultTTL = null; |
|
23
|
|
|
|
|
24
|
|
|
public function __destruct() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->commit(); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
private function __construct( |
|
30
|
|
|
PSRSimpleCache\CacheInterface $cache, |
|
31
|
|
|
?string $namespace = null, |
|
32
|
|
|
int $defaultTTL = null |
|
33
|
|
|
) { |
|
34
|
|
|
$this->cache = $cache; |
|
35
|
|
|
$this->cacheKeyBuilder = CacheKeyBuilder::create($namespace); |
|
36
|
|
|
$this->defaultTTL = $defaultTTL; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public static function createRedis( |
|
40
|
|
|
\Redis $redis, |
|
41
|
|
|
?string $namespace = null, |
|
42
|
|
|
int $defaultTTL = null |
|
43
|
|
|
): Cache\CacheItemPoolInterface { |
|
44
|
|
|
return new self(SimpleCache\Redis::create($redis), $namespace, $defaultTTL); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public static function createMemcached( |
|
48
|
|
|
\Memcached $memcached, |
|
49
|
|
|
?string $namespace = null, |
|
50
|
|
|
int $defaultTTL = null |
|
51
|
|
|
): Cache\CacheItemPoolInterface { |
|
52
|
|
|
return new self(SimpleCache\Memcached::create($memcached), $namespace, $defaultTTL); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public static function createMemory( |
|
56
|
|
|
int $maxItems = null, |
|
57
|
|
|
?string $namespace = null, |
|
58
|
|
|
int $defaultTTL = null |
|
59
|
|
|
): Cache\CacheItemPoolInterface { |
|
60
|
|
|
return new self(SimpleCache\Memory::create($maxItems), $namespace, $defaultTTL); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public static function createAPCu( |
|
64
|
|
|
?string $namespace = null, |
|
65
|
|
|
int $defaultTTL = null |
|
66
|
|
|
): Cache\CacheItemPoolInterface { |
|
67
|
|
|
return new self(SimpleCache\APCu::create(), $namespace, $defaultTTL); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public static function createFromSimpleCache( |
|
71
|
|
|
PSRSimpleCache\CacheInterface $cache, |
|
72
|
|
|
?string $namespace = null, |
|
73
|
|
|
int $defaultTTL = null |
|
74
|
|
|
): Cache\CacheItemPoolInterface { |
|
75
|
|
|
return new self($cache, $namespace, $defaultTTL); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @inheritDoc |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getItem($key) |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->getItems([$key])->current(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @inheritDoc |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getItems(array $keys = []): \Generator |
|
90
|
|
|
{ |
|
91
|
|
|
foreach ($this->cache->getMultiple($this->cacheKeyBuilder->buildKeys($keys)) as $key => $item) { |
|
92
|
|
|
$normalizedKey = $this->cacheKeyBuilder->removeNamespace($key); |
|
93
|
|
|
yield $normalizedKey => ($item) ? $item->setAsHit() : CacheItem::create($normalizedKey); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @inheritDoc |
|
99
|
|
|
*/ |
|
100
|
|
|
public function hasItem($key): bool |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->cache->has($this->cacheKeyBuilder->buildKey($key)); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @inheritDoc |
|
107
|
|
|
*/ |
|
108
|
|
|
public function clear(): bool |
|
109
|
|
|
{ |
|
110
|
|
|
$this->deferred = []; |
|
111
|
|
|
return $this->cache->clear(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @inheritDoc |
|
116
|
|
|
*/ |
|
117
|
|
|
public function deleteItem($key): bool |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->deleteItems([$key]); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @inheritDoc |
|
124
|
|
|
*/ |
|
125
|
|
|
public function deleteItems(array $keys): bool |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->cache->deleteMultiple($this->cacheKeyBuilder->buildKeys($keys)); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @inheritDoc |
|
132
|
|
|
*/ |
|
133
|
|
|
public function save(Cache\CacheItemInterface $item): bool |
|
134
|
|
|
{ |
|
135
|
|
|
$this->saveDeferred($item); |
|
136
|
|
|
|
|
137
|
|
|
return $this->commit(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @inheritDoc |
|
142
|
|
|
*/ |
|
143
|
|
|
public function saveDeferred(Cache\CacheItemInterface $item): bool |
|
144
|
|
|
{ |
|
145
|
|
|
$this->deferred[$item->getTTL()][$this->cacheKeyBuilder->buildKey($item->getKey())] = $item; |
|
146
|
|
|
return true; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @inheritDoc |
|
151
|
|
|
*/ |
|
152
|
|
|
public function commit(): bool |
|
153
|
|
|
{ |
|
154
|
|
|
foreach ($this->deferred as $ttl => $items) { |
|
155
|
|
|
$this->cache->setMultiple($items, ($ttl !== '') ? $ttl : $this->defaultTTL); |
|
156
|
|
|
unset($this->deferred[$ttl]); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return true; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|