1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* This file is part of Phpfastcache. |
6
|
|
|
* |
7
|
|
|
* @license MIT License (MIT) |
8
|
|
|
* |
9
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt and LICENCE files. |
10
|
|
|
* |
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
12
|
|
|
* @author Contributors https://github.com/PHPSocialNetwork/phpfastcache/graphs/contributors |
13
|
|
|
*/ |
14
|
|
|
declare(strict_types=1); |
15
|
|
|
|
16
|
|
|
namespace Phpfastcache\Helper; |
17
|
|
|
|
18
|
|
|
use DateInterval; |
19
|
|
|
use DateTime; |
20
|
|
|
use Phpfastcache\CacheManager; |
21
|
|
|
use Phpfastcache\Config\ConfigurationOptionInterface; |
22
|
|
|
use Phpfastcache\Core\Item\ExtendedCacheItemInterface; |
23
|
|
|
use Phpfastcache\Core\Pool\ExtendedCacheItemPoolInterface; |
24
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheDriverCheckException; |
25
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheDriverException; |
26
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheDriverNotFoundException; |
27
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheInvalidArgumentException; |
28
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheLogicException; |
29
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheRootException; |
30
|
|
|
use Phpfastcache\Exceptions\PhpfastcacheSimpleCacheException; |
31
|
|
|
use Psr\Cache\InvalidArgumentException; |
32
|
|
|
use Psr\SimpleCache\CacheInterface; |
33
|
|
|
use Traversable; |
34
|
|
|
|
35
|
|
|
class Psr16Adapter implements CacheInterface |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var ExtendedCacheItemPoolInterface |
39
|
|
|
*/ |
40
|
|
|
protected ExtendedCacheItemPoolInterface $internalCacheInstance; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Psr16Adapter constructor. |
44
|
|
|
* @param $driver |
45
|
|
|
* @param null|ConfigurationOptionInterface $config |
46
|
|
|
* @throws PhpfastcacheDriverCheckException |
47
|
|
|
* @throws PhpfastcacheLogicException |
48
|
|
|
* @throws PhpfastcacheDriverException |
49
|
|
|
* @throws PhpfastcacheDriverNotFoundException |
50
|
|
|
*/ |
51
|
|
|
public function __construct($driver, ConfigurationOptionInterface $config = null) |
52
|
|
|
{ |
53
|
|
|
if ($driver instanceof ExtendedCacheItemPoolInterface) { |
54
|
|
|
if ($config !== null) { |
55
|
|
|
throw new PhpfastcacheLogicException("You can't pass a config parameter along with an non-string '\$driver' parameter."); |
56
|
|
|
} |
57
|
|
|
$this->internalCacheInstance = $driver; |
58
|
|
|
} else { |
59
|
|
|
$this->internalCacheInstance = CacheManager::getInstance($driver, $config); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $key |
65
|
|
|
* @param mixed|null $default |
66
|
|
|
* @return mixed|null |
67
|
|
|
* @throws PhpfastcacheSimpleCacheException |
68
|
|
|
*/ |
69
|
|
|
public function get($key, $default = null) |
70
|
|
|
{ |
71
|
|
|
try { |
72
|
|
|
$cacheItem = $this->internalCacheInstance->getItem($key); |
73
|
|
|
if (!$cacheItem->isExpired() && $cacheItem->get() !== null) { |
74
|
|
|
return $cacheItem->get(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $default; |
78
|
|
|
} catch (PhpfastcacheInvalidArgumentException $e) { |
79
|
|
|
throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $key |
85
|
|
|
* @param mixed $value |
86
|
|
|
* @param null|int|DateInterval $ttl |
87
|
|
|
* @return bool |
88
|
|
|
* @throws PhpfastcacheSimpleCacheException |
89
|
|
|
*/ |
90
|
|
|
public function set($key, $value, $ttl = null): bool |
91
|
|
|
{ |
92
|
|
|
try { |
93
|
|
|
$cacheItem = $this->internalCacheInstance |
94
|
|
|
->getItem($key) |
95
|
|
|
->set($value); |
96
|
|
|
if (\is_int($ttl) && $ttl <= 0) { |
97
|
|
|
$cacheItem->expiresAt((new DateTime('@0'))); |
98
|
|
|
} elseif ($ttl !== null) { |
99
|
|
|
$cacheItem->expiresAfter($ttl); |
100
|
|
|
} |
101
|
|
|
return $this->internalCacheInstance->save($cacheItem); |
102
|
|
|
} catch (PhpfastcacheInvalidArgumentException $e) { |
103
|
|
|
throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $key |
109
|
|
|
* @return bool |
110
|
|
|
* @throws PhpfastcacheSimpleCacheException |
111
|
|
|
* @throws InvalidArgumentException |
112
|
|
|
*/ |
113
|
|
|
public function delete($key): bool |
114
|
|
|
{ |
115
|
|
|
try { |
116
|
|
|
return $this->internalCacheInstance->deleteItem($key); |
117
|
|
|
} catch (PhpfastcacheInvalidArgumentException $e) { |
118
|
|
|
throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return bool |
124
|
|
|
* @throws PhpfastcacheSimpleCacheException |
125
|
|
|
*/ |
126
|
|
|
public function clear(): bool |
127
|
|
|
{ |
128
|
|
|
try { |
129
|
|
|
return $this->internalCacheInstance->clear(); |
130
|
|
|
} catch (PhpfastcacheRootException $e) { |
131
|
|
|
throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param iterable $keys |
137
|
|
|
* @param null $default |
|
|
|
|
138
|
|
|
* @return ExtendedCacheItemInterface[]|iterable |
139
|
|
|
* @throws PhpfastcacheSimpleCacheException |
140
|
|
|
*/ |
141
|
|
|
public function getMultiple($keys, $default = null) |
142
|
|
|
{ |
143
|
|
|
if ($keys instanceof Traversable) { |
144
|
|
|
$keys = \iterator_to_array($keys); |
145
|
|
|
} |
146
|
|
|
try { |
147
|
|
|
return \array_map( |
148
|
|
|
static fn (ExtendedCacheItemInterface $item) => $item->isHit() ? $item->get() : $default, |
149
|
|
|
$this->internalCacheInstance->getItems($keys) |
|
|
|
|
150
|
|
|
); |
151
|
|
|
} catch (PhpfastcacheInvalidArgumentException $e) { |
152
|
|
|
throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string[] $values |
158
|
|
|
* @param null|int|DateInterval $ttl |
159
|
|
|
* @return bool |
160
|
|
|
* @throws PhpfastcacheSimpleCacheException |
161
|
|
|
*/ |
162
|
|
|
public function setMultiple($values, $ttl = null): bool |
163
|
|
|
{ |
164
|
|
|
try { |
165
|
|
|
foreach ($values as $key => $value) { |
166
|
|
|
$cacheItem = $this->internalCacheInstance->getItem($key)->set($value); |
167
|
|
|
|
168
|
|
|
if (\is_int($ttl) && $ttl <= 0) { |
169
|
|
|
$cacheItem->expiresAt((new DateTime('@0'))); |
170
|
|
|
} elseif ($ttl !== null) { |
171
|
|
|
$cacheItem->expiresAfter($ttl); |
172
|
|
|
} |
173
|
|
|
$this->internalCacheInstance->saveDeferred($cacheItem); |
174
|
|
|
unset($cacheItem); |
175
|
|
|
} |
176
|
|
|
return $this->internalCacheInstance->commit(); |
177
|
|
|
} catch (PhpfastcacheInvalidArgumentException $e) { |
178
|
|
|
throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param iterable|array $keys |
184
|
|
|
* @return bool |
185
|
|
|
* @throws PhpfastcacheSimpleCacheException |
186
|
|
|
* @throws InvalidArgumentException |
187
|
|
|
*/ |
188
|
|
|
public function deleteMultiple($keys): bool |
189
|
|
|
{ |
190
|
|
|
try { |
191
|
|
|
if ($keys instanceof Traversable) { |
192
|
|
|
return $this->internalCacheInstance->deleteItems(\iterator_to_array($keys)); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
if (\is_array($keys)) { |
196
|
|
|
return $this->internalCacheInstance->deleteItems($keys); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
throw new phpFastCacheInvalidArgumentException('$keys must be an array/Traversable instance.'); |
200
|
|
|
} catch (PhpfastcacheInvalidArgumentException $e) { |
201
|
|
|
throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param string $key |
207
|
|
|
* @return bool |
208
|
|
|
* @throws PhpfastcacheSimpleCacheException |
209
|
|
|
*/ |
210
|
|
|
public function has($key): bool |
211
|
|
|
{ |
212
|
|
|
try { |
213
|
|
|
$cacheItem = $this->internalCacheInstance->getItem($key); |
214
|
|
|
return $cacheItem->isHit() && !$cacheItem->isExpired(); |
215
|
|
|
} catch (PhpfastcacheInvalidArgumentException $e) { |
216
|
|
|
throw new PhpfastcacheSimpleCacheException($e->getMessage(), 0, $e); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|