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