-
Notifications
You must be signed in to change notification settings - Fork 1
/
CacheItemPool.php
137 lines (116 loc) · 3.33 KB
/
CacheItemPool.php
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
<?php
namespace Koded\Caching;
use Exception;
use Psr\Cache\{CacheItemInterface, CacheItemPoolInterface};
use function Koded\Stdlib\now;
abstract class CacheItemPool implements CacheItemPoolInterface
{
protected Cache $client;
/** @var CacheItemInterface[] */
private array $deferred = [];
abstract public function __construct(string $client, array $parameters);
// @codeCoverageIgnoreStart
public function __destruct()
{
$this->commit();
}
// @codeCoverageIgnoreEnd
public function commit(): bool
{
foreach ($this->deferred as $key => $item) {
if (true === $this->save($item)) {
unset($this->deferred[$key]);
}
}
return empty($this->deferred);
}
public function save(CacheItemInterface $item): bool
{
/** @var CacheItem $item */
return $this->client->set($item->getKey(), $item->get(), $item->getExpiresAt());
}
public function getItems(array $keys = []): array
{
$items = [];
foreach ($keys as $key) {
$items[$key] = $this->getItem($key);
}
return $items;
}
public function getItem($key): CacheItemInterface
{
try {
$item = new class($key, $this->client->getTtl()) extends CacheItem {};
if (false === $this->client->has($key)) {
if (isset($this->deferred[$key])) {
return clone $this->deferred[$key];
}
return $item;
}
(function() {
$this->isHit = true;
return $this;
})->call($item);
return $item->set($this->client->get($key));
} catch (Exception $e) {
throw CachePoolException::from($e);
}
}
public function hasItem($key): bool
{
try {
return isset($this->deferred[$key]) || $this->client->has($key);
} catch (Exception $e) {
throw CachePoolException::from($e);
}
}
public function clear(): bool
{
if ($cleared = $this->client->clear()) {
$this->deferred = [];
}
return $cleared;
}
public function deleteItems(array $keys): bool
{
try {
return $this->client->deleteMultiple($keys);
} catch (Exception $e) {
throw CachePoolException::from($e);
}
}
public function deleteItem($key): bool
{
try {
if ($deleted = $this->client->delete($key)) {
unset($this->deferred[$key]);
}
return $deleted;
} catch (Exception $e) {
throw CachePoolException::from($e);
}
}
public function saveDeferred(CacheItemInterface $item): bool
{
/** @var CacheItem $item */
if (null !== $item->getExpiresAt() && $item->getExpiresAt() <= now()->getTimestamp()) {
return false;
}
$this->deferred[$item->getKey()] = (function() {
$this->isHit = true;
return $this;
})->call($item);
return true;
}
/**
* Returns the instance of the underlying cache client.
*
* This method is not part of the PSR-6.
*
* @return Cache
*/
public function client(): Cache
{
return $this->client;
}
}