kodedphp /
cache-extended
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Koded\Caching; |
||
| 4 | |||
| 5 | use Exception; |
||
| 6 | use Psr\Cache\{CacheItemInterface, CacheItemPoolInterface}; |
||
| 7 | use function Koded\Stdlib\now; |
||
| 8 | |||
| 9 | |||
| 10 | abstract class CacheItemPool implements CacheItemPoolInterface |
||
| 11 | { |
||
| 12 | /** @var Cache */ |
||
| 13 | protected $client; |
||
| 14 | |||
| 15 | /** @var CacheItemInterface[] */ |
||
| 16 | private $deferred = []; |
||
| 17 | |||
| 18 | |||
| 19 | abstract public function __construct(string $client, array $parameters); |
||
| 20 | |||
| 21 | // @codeCoverageIgnoreStart |
||
| 22 | public function __destruct() |
||
| 23 | { |
||
| 24 | $this->commit(); |
||
| 25 | } |
||
| 26 | // @codeCoverageIgnoreEnd |
||
| 27 | |||
| 28 | 56 | public function commit(): bool |
|
| 29 | { |
||
| 30 | 56 | foreach ($this->deferred as $key => $item) { |
|
| 31 | 35 | if (true === $this->save($item)) { |
|
| 32 | 35 | unset($this->deferred[$key]); |
|
| 33 | } |
||
| 34 | } |
||
| 35 | |||
| 36 | 56 | return empty($this->deferred); |
|
| 37 | } |
||
| 38 | |||
| 39 | |||
| 40 | 216 | public function save(CacheItemInterface $item): bool |
|
| 41 | { |
||
| 42 | /** @var CacheItem $item */ |
||
| 43 | 216 | return $this->client->set($item->getKey(), $item->get(), $item->getExpiresAt()); |
|
|
0 ignored issues
–
show
|
|||
| 44 | } |
||
| 45 | |||
| 46 | |||
| 47 | 133 | public function getItems(array $keys = []): array |
|
| 48 | { |
||
| 49 | 133 | $items = []; |
|
| 50 | 133 | foreach ($keys as $key) { |
|
| 51 | 126 | $items[$key] = $this->getItem($key); |
|
| 52 | } |
||
| 53 | |||
| 54 | 21 | return $items; |
|
| 55 | } |
||
| 56 | |||
| 57 | |||
| 58 | 483 | public function getItem($key): CacheItemInterface |
|
| 59 | { |
||
| 60 | try { |
||
| 61 | $item = new class($key, $this->client->getTtl()) extends CacheItem {}; |
||
| 62 | |||
| 63 | 483 | if (false === $this->client->has($key)) { |
|
| 64 | 371 | if (isset($this->deferred[$key])) { |
|
| 65 | 49 | return clone $this->deferred[$key]; |
|
| 66 | } |
||
| 67 | 364 | return $item; |
|
| 68 | } |
||
| 69 | |||
| 70 | (function() { |
||
| 71 | 175 | $this->isHit = true; |
|
|
0 ignored issues
–
show
|
|||
| 72 | 175 | return $this; |
|
| 73 | 175 | })->call($item); |
|
| 74 | |||
| 75 | 175 | return $item->set($this->client->get($key)); |
|
| 76 | |||
| 77 | 224 | } catch (Exception $e) { |
|
| 78 | 224 | throw CachePoolException::from($e); |
|
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | |||
| 83 | 174 | public function hasItem($key): bool |
|
| 84 | { |
||
| 85 | try { |
||
| 86 | 174 | return isset($this->deferred[$key]) || $this->client->has($key); |
|
| 87 | 112 | } catch (Exception $e) { |
|
| 88 | 112 | throw CachePoolException::from($e); |
|
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | |||
| 93 | 818 | public function clear(): bool |
|
| 94 | { |
||
| 95 | 818 | if ($cleared = $this->client->clear()) { |
|
| 96 | 818 | $this->deferred = []; |
|
| 97 | } |
||
| 98 | |||
| 99 | 818 | return $cleared; |
|
| 100 | } |
||
| 101 | |||
| 102 | |||
| 103 | 119 | public function deleteItems(array $keys): bool |
|
| 104 | { |
||
| 105 | try { |
||
| 106 | 119 | return $this->client->deleteMultiple($keys); |
|
| 107 | 112 | } catch (Exception $e) { |
|
| 108 | 112 | throw CachePoolException::from($e); |
|
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | |||
| 113 | 140 | public function deleteItem($key): bool |
|
| 114 | { |
||
| 115 | try { |
||
| 116 | 140 | if ($deleted = $this->client->delete($key)) { |
|
| 117 | 28 | unset($this->deferred[$key]); |
|
| 118 | } |
||
| 119 | |||
| 120 | 28 | return $deleted; |
|
| 121 | |||
| 122 | 112 | } catch (Exception $e) { |
|
| 123 | 112 | throw CachePoolException::from($e); |
|
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | |||
| 128 | 70 | public function saveDeferred(CacheItemInterface $item): bool |
|
| 129 | { |
||
| 130 | /** @var CacheItem $item */ |
||
| 131 | 70 | if (null !== $item->getExpiresAt() && $item->getExpiresAt() <= now()->getTimestamp()) { |
|
| 132 | 14 | return false; |
|
| 133 | } |
||
| 134 | |||
| 135 | $this->deferred[$item->getKey()] = (function() { |
||
| 136 | 56 | $this->isHit = true; |
|
|
0 ignored issues
–
show
|
|||
| 137 | 56 | return $this; |
|
| 138 | 56 | })->call($item); |
|
| 139 | |||
| 140 | 56 | return true; |
|
| 141 | } |
||
| 142 | } |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.