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