@@ -27,123 +27,123 @@ |
||
| 27 | 27 | |
| 28 | 28 | trait CacheItemTrait |
| 29 | 29 | { |
| 30 | - use EventManagerDispatcherTrait; |
|
| 31 | - use ClassNamespaceResolverTrait; |
|
| 32 | - |
|
| 33 | - protected string $key; |
|
| 34 | - |
|
| 35 | - protected mixed $data; |
|
| 36 | - |
|
| 37 | - protected DateTimeInterface $expirationDate; |
|
| 38 | - |
|
| 39 | - protected DateTimeInterface $creationDate; |
|
| 40 | - |
|
| 41 | - protected DateTimeInterface $modificationDate; |
|
| 42 | - |
|
| 43 | - protected bool $isHit = false; |
|
| 44 | - |
|
| 45 | - public function getKey(): string |
|
| 46 | - { |
|
| 47 | - return $this->key; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - public function get(): mixed |
|
| 51 | - { |
|
| 52 | - if (!$this->isHit()) { |
|
| 53 | - return null; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - return $this->data; |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 61 | - */ |
|
| 62 | - public function set(mixed $value): static |
|
| 63 | - { |
|
| 64 | - if ($value instanceof \Closure) { |
|
| 65 | - throw new PhpfastcacheInvalidArgumentException('The value set cannot be an instance of \\Closure.'); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - if (\is_resource($value)) { |
|
| 69 | - throw new PhpfastcacheInvalidArgumentException('The value set cannot be a resource'); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - $this->eventManager->dispatch(Event::CACHE_ITEM_SET, $this, new EventReferenceParameter($value, true)); |
|
| 73 | - |
|
| 74 | - $this->data = $value; |
|
| 75 | - |
|
| 76 | - return $this; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * @return bool |
|
| 81 | - */ |
|
| 82 | - public function isHit(): bool |
|
| 83 | - { |
|
| 84 | - return $this->isHit; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * @param bool $isHit |
|
| 89 | - * @return ExtendedCacheItemInterface |
|
| 90 | - */ |
|
| 91 | - public function setHit(bool $isHit): ExtendedCacheItemInterface |
|
| 92 | - { |
|
| 93 | - $this->isHit = $isHit; |
|
| 94 | - |
|
| 95 | - return $this; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 100 | - */ |
|
| 101 | - public function expiresAt(?\DateTimeInterface $expiration): static |
|
| 102 | - { |
|
| 103 | - if ($expiration instanceof DateTimeInterface) { |
|
| 104 | - $this->eventManager->dispatch(Event::CACHE_ITEM_EXPIRE_AT, $this, $expiration); |
|
| 105 | - $this->expirationDate = $this->demutateDatetime($expiration); |
|
| 106 | - } else { |
|
| 107 | - throw new PhpfastcacheInvalidArgumentException('$expiration must be an object implementing the DateTimeInterface got: ' . \gettype($expiration)); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - return $this; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * @return $this |
|
| 115 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 116 | - * @throws \Exception |
|
| 117 | - */ |
|
| 118 | - public function expiresAfter(int|\DateInterval|null $time): static |
|
| 119 | - { |
|
| 120 | - if (\is_numeric($time)) { |
|
| 121 | - if ($time <= 0) { |
|
| 122 | - /** |
|
| 123 | - * 5 months, however memcached or memory cached will be gone when u restart it |
|
| 124 | - * just recommended for sqlite. files |
|
| 125 | - */ |
|
| 126 | - $time = 30 * 24 * 3600 * 5; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - $this->eventManager->dispatch(Event::CACHE_ITEM_EXPIRE_AFTER, $this, $time); |
|
| 130 | - |
|
| 131 | - $this->expirationDate = (new DateTime())->add(new DateInterval(\sprintf('PT%dS', $time))); |
|
| 132 | - } elseif ($time instanceof DateInterval) { |
|
| 133 | - $this->eventManager->dispatch(Event::CACHE_ITEM_EXPIRE_AFTER, $this, $time); |
|
| 134 | - |
|
| 135 | - $this->expirationDate = (new DateTime())->add($time); |
|
| 136 | - } else { |
|
| 137 | - throw new PhpfastcacheInvalidArgumentException(\sprintf('Invalid date format, got "%s"', \gettype($time))); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - return $this; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - protected function demutateDatetime(\DateTimeInterface $dateTime): \DateTimeInterface |
|
| 144 | - { |
|
| 145 | - return $dateTime instanceof \DateTimeImmutable |
|
| 146 | - ? \DateTime::createFromImmutable($dateTime) |
|
| 147 | - : $dateTime; |
|
| 148 | - } |
|
| 30 | + use EventManagerDispatcherTrait; |
|
| 31 | + use ClassNamespaceResolverTrait; |
|
| 32 | + |
|
| 33 | + protected string $key; |
|
| 34 | + |
|
| 35 | + protected mixed $data; |
|
| 36 | + |
|
| 37 | + protected DateTimeInterface $expirationDate; |
|
| 38 | + |
|
| 39 | + protected DateTimeInterface $creationDate; |
|
| 40 | + |
|
| 41 | + protected DateTimeInterface $modificationDate; |
|
| 42 | + |
|
| 43 | + protected bool $isHit = false; |
|
| 44 | + |
|
| 45 | + public function getKey(): string |
|
| 46 | + { |
|
| 47 | + return $this->key; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + public function get(): mixed |
|
| 51 | + { |
|
| 52 | + if (!$this->isHit()) { |
|
| 53 | + return null; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + return $this->data; |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 61 | + */ |
|
| 62 | + public function set(mixed $value): static |
|
| 63 | + { |
|
| 64 | + if ($value instanceof \Closure) { |
|
| 65 | + throw new PhpfastcacheInvalidArgumentException('The value set cannot be an instance of \\Closure.'); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + if (\is_resource($value)) { |
|
| 69 | + throw new PhpfastcacheInvalidArgumentException('The value set cannot be a resource'); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + $this->eventManager->dispatch(Event::CACHE_ITEM_SET, $this, new EventReferenceParameter($value, true)); |
|
| 73 | + |
|
| 74 | + $this->data = $value; |
|
| 75 | + |
|
| 76 | + return $this; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * @return bool |
|
| 81 | + */ |
|
| 82 | + public function isHit(): bool |
|
| 83 | + { |
|
| 84 | + return $this->isHit; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * @param bool $isHit |
|
| 89 | + * @return ExtendedCacheItemInterface |
|
| 90 | + */ |
|
| 91 | + public function setHit(bool $isHit): ExtendedCacheItemInterface |
|
| 92 | + { |
|
| 93 | + $this->isHit = $isHit; |
|
| 94 | + |
|
| 95 | + return $this; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 100 | + */ |
|
| 101 | + public function expiresAt(?\DateTimeInterface $expiration): static |
|
| 102 | + { |
|
| 103 | + if ($expiration instanceof DateTimeInterface) { |
|
| 104 | + $this->eventManager->dispatch(Event::CACHE_ITEM_EXPIRE_AT, $this, $expiration); |
|
| 105 | + $this->expirationDate = $this->demutateDatetime($expiration); |
|
| 106 | + } else { |
|
| 107 | + throw new PhpfastcacheInvalidArgumentException('$expiration must be an object implementing the DateTimeInterface got: ' . \gettype($expiration)); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + return $this; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * @return $this |
|
| 115 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 116 | + * @throws \Exception |
|
| 117 | + */ |
|
| 118 | + public function expiresAfter(int|\DateInterval|null $time): static |
|
| 119 | + { |
|
| 120 | + if (\is_numeric($time)) { |
|
| 121 | + if ($time <= 0) { |
|
| 122 | + /** |
|
| 123 | + * 5 months, however memcached or memory cached will be gone when u restart it |
|
| 124 | + * just recommended for sqlite. files |
|
| 125 | + */ |
|
| 126 | + $time = 30 * 24 * 3600 * 5; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + $this->eventManager->dispatch(Event::CACHE_ITEM_EXPIRE_AFTER, $this, $time); |
|
| 130 | + |
|
| 131 | + $this->expirationDate = (new DateTime())->add(new DateInterval(\sprintf('PT%dS', $time))); |
|
| 132 | + } elseif ($time instanceof DateInterval) { |
|
| 133 | + $this->eventManager->dispatch(Event::CACHE_ITEM_EXPIRE_AFTER, $this, $time); |
|
| 134 | + |
|
| 135 | + $this->expirationDate = (new DateTime())->add($time); |
|
| 136 | + } else { |
|
| 137 | + throw new PhpfastcacheInvalidArgumentException(\sprintf('Invalid date format, got "%s"', \gettype($time))); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + return $this; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + protected function demutateDatetime(\DateTimeInterface $dateTime): \DateTimeInterface |
|
| 144 | + { |
|
| 145 | + return $dateTime instanceof \DateTimeImmutable |
|
| 146 | + ? \DateTime::createFromImmutable($dateTime) |
|
| 147 | + : $dateTime; |
|
| 148 | + } |
|
| 149 | 149 | } |
@@ -115,7 +115,7 @@ |
||
| 115 | 115 | * @throws PhpfastcacheInvalidArgumentException |
| 116 | 116 | * @throws \Exception |
| 117 | 117 | */ |
| 118 | - public function expiresAfter(int|\DateInterval|null $time): static |
|
| 118 | + public function expiresAfter(int | \DateInterval | null $time): static |
|
| 119 | 119 | { |
| 120 | 120 | if (\is_numeric($time)) { |
| 121 | 121 | if ($time <= 0) { |
@@ -103,7 +103,8 @@ discard block |
||
| 103 | 103 | if ($expiration instanceof DateTimeInterface) { |
| 104 | 104 | $this->eventManager->dispatch(Event::CACHE_ITEM_EXPIRE_AT, $this, $expiration); |
| 105 | 105 | $this->expirationDate = $this->demutateDatetime($expiration); |
| 106 | - } else { |
|
| 106 | + } |
|
| 107 | + else { |
|
| 107 | 108 | throw new PhpfastcacheInvalidArgumentException('$expiration must be an object implementing the DateTimeInterface got: ' . \gettype($expiration)); |
| 108 | 109 | } |
| 109 | 110 | |
@@ -129,11 +130,13 @@ discard block |
||
| 129 | 130 | $this->eventManager->dispatch(Event::CACHE_ITEM_EXPIRE_AFTER, $this, $time); |
| 130 | 131 | |
| 131 | 132 | $this->expirationDate = (new DateTime())->add(new DateInterval(\sprintf('PT%dS', $time))); |
| 132 | - } elseif ($time instanceof DateInterval) { |
|
| 133 | + } |
|
| 134 | + elseif ($time instanceof DateInterval) { |
|
| 133 | 135 | $this->eventManager->dispatch(Event::CACHE_ITEM_EXPIRE_AFTER, $this, $time); |
| 134 | 136 | |
| 135 | 137 | $this->expirationDate = (new DateTime())->add($time); |
| 136 | - } else { |
|
| 138 | + } |
|
| 139 | + else { |
|
| 137 | 140 | throw new PhpfastcacheInvalidArgumentException(\sprintf('Invalid date format, got "%s"', \gettype($time))); |
| 138 | 141 | } |
| 139 | 142 | |
@@ -27,109 +27,109 @@ |
||
| 27 | 27 | |
| 28 | 28 | trait ExtendedCacheItemPoolTrait |
| 29 | 29 | { |
| 30 | - use CacheItemPoolTrait; |
|
| 31 | - use AggregatablePoolTrait; |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * @inheritDoc |
|
| 35 | - * @param string $pattern |
|
| 36 | - * @return array<string, mixed> |
|
| 37 | - */ |
|
| 38 | - public function getAllItems(string $pattern = ''): iterable |
|
| 39 | - { |
|
| 40 | - $driverReadAllKeysCallback = fn (string $pattern): iterable => $this->driverReadAllKeys($pattern); |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * This event allow you to customize the callback and wrap it to an invoker |
|
| 44 | - * like SebastianBergmann\Invoke\Invoke, so you can set up custom timeouts. |
|
| 45 | - */ |
|
| 46 | - $this->eventManager->dispatch(Event::CACHE_GET_ALL_ITEMS, $this, new EventReferenceParameter($driverReadAllKeysCallback)); |
|
| 47 | - $keys = $driverReadAllKeysCallback($pattern); |
|
| 48 | - |
|
| 49 | - if (count($keys) > 0) { |
|
| 50 | - return $this->getItems($keys instanceof \Traversable ? iterator_to_array($keys) : $keys); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - return []; |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @inheritDoc |
|
| 58 | - * @throws PhpfastcacheCoreException |
|
| 59 | - * @throws PhpfastcacheDriverException |
|
| 60 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 61 | - * @throws PhpfastcacheLogicException |
|
| 62 | - */ |
|
| 63 | - public function getItemsAsJsonString(array $keys = [], int $options = \JSON_THROW_ON_ERROR, int $depth = 512): string |
|
| 64 | - { |
|
| 65 | - return \json_encode( |
|
| 66 | - \array_map( |
|
| 67 | - static fn(CacheItemInterface $item) => $item->get(), |
|
| 68 | - \array_values($this->getItems($keys)) |
|
| 69 | - ), |
|
| 70 | - $options, |
|
| 71 | - $depth |
|
| 72 | - ); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - public function detachAllItems(): static |
|
| 76 | - { |
|
| 77 | - foreach ($this->itemInstances as $item) { |
|
| 78 | - $this->detachItem($item); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - return $this; |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - public function detachItem(CacheItemInterface $item): static |
|
| 85 | - { |
|
| 86 | - if (isset($this->itemInstances[$item->getKey()])) { |
|
| 87 | - $this->deregisterItem($item->getKey()); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - return $this; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * @param ExtendedCacheItemInterface ...$items |
|
| 95 | - * @return bool |
|
| 96 | - * @throws PhpfastcacheCoreException |
|
| 97 | - * @throws PhpfastcacheDriverException |
|
| 98 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 99 | - * @throws PhpfastcacheLogicException |
|
| 100 | - */ |
|
| 101 | - public function saveMultiple(ExtendedCacheItemInterface ...$items): bool |
|
| 102 | - { |
|
| 103 | - $this->eventManager->dispatch(Event::CACHE_SAVE_MULTIPLE_ITEMS, $this, new EventReferenceParameter($items)); |
|
| 104 | - |
|
| 105 | - if (\count($items)) { |
|
| 106 | - foreach ($items as $item) { |
|
| 107 | - $this->save($item); |
|
| 108 | - } |
|
| 109 | - return true; |
|
| 110 | - } |
|
| 111 | - return false; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * @return string |
|
| 116 | - */ |
|
| 117 | - public function getHelp(): string |
|
| 118 | - { |
|
| 119 | - return ''; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 124 | - */ |
|
| 125 | - public function throwUnsupportedDriverReadAllPattern(string $linkReference = ''): void |
|
| 126 | - { |
|
| 127 | - throw new PhpfastcacheInvalidArgumentException( |
|
| 128 | - sprintf( |
|
| 129 | - '%s does not support a pattern argument.%s', |
|
| 130 | - $this->getDriverName(), |
|
| 131 | - $linkReference ? " See $linkReference" : '' |
|
| 132 | - ) |
|
| 133 | - ); |
|
| 134 | - } |
|
| 30 | + use CacheItemPoolTrait; |
|
| 31 | + use AggregatablePoolTrait; |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * @inheritDoc |
|
| 35 | + * @param string $pattern |
|
| 36 | + * @return array<string, mixed> |
|
| 37 | + */ |
|
| 38 | + public function getAllItems(string $pattern = ''): iterable |
|
| 39 | + { |
|
| 40 | + $driverReadAllKeysCallback = fn (string $pattern): iterable => $this->driverReadAllKeys($pattern); |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * This event allow you to customize the callback and wrap it to an invoker |
|
| 44 | + * like SebastianBergmann\Invoke\Invoke, so you can set up custom timeouts. |
|
| 45 | + */ |
|
| 46 | + $this->eventManager->dispatch(Event::CACHE_GET_ALL_ITEMS, $this, new EventReferenceParameter($driverReadAllKeysCallback)); |
|
| 47 | + $keys = $driverReadAllKeysCallback($pattern); |
|
| 48 | + |
|
| 49 | + if (count($keys) > 0) { |
|
| 50 | + return $this->getItems($keys instanceof \Traversable ? iterator_to_array($keys) : $keys); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + return []; |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @inheritDoc |
|
| 58 | + * @throws PhpfastcacheCoreException |
|
| 59 | + * @throws PhpfastcacheDriverException |
|
| 60 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 61 | + * @throws PhpfastcacheLogicException |
|
| 62 | + */ |
|
| 63 | + public function getItemsAsJsonString(array $keys = [], int $options = \JSON_THROW_ON_ERROR, int $depth = 512): string |
|
| 64 | + { |
|
| 65 | + return \json_encode( |
|
| 66 | + \array_map( |
|
| 67 | + static fn(CacheItemInterface $item) => $item->get(), |
|
| 68 | + \array_values($this->getItems($keys)) |
|
| 69 | + ), |
|
| 70 | + $options, |
|
| 71 | + $depth |
|
| 72 | + ); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + public function detachAllItems(): static |
|
| 76 | + { |
|
| 77 | + foreach ($this->itemInstances as $item) { |
|
| 78 | + $this->detachItem($item); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + return $this; |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + public function detachItem(CacheItemInterface $item): static |
|
| 85 | + { |
|
| 86 | + if (isset($this->itemInstances[$item->getKey()])) { |
|
| 87 | + $this->deregisterItem($item->getKey()); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + return $this; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * @param ExtendedCacheItemInterface ...$items |
|
| 95 | + * @return bool |
|
| 96 | + * @throws PhpfastcacheCoreException |
|
| 97 | + * @throws PhpfastcacheDriverException |
|
| 98 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 99 | + * @throws PhpfastcacheLogicException |
|
| 100 | + */ |
|
| 101 | + public function saveMultiple(ExtendedCacheItemInterface ...$items): bool |
|
| 102 | + { |
|
| 103 | + $this->eventManager->dispatch(Event::CACHE_SAVE_MULTIPLE_ITEMS, $this, new EventReferenceParameter($items)); |
|
| 104 | + |
|
| 105 | + if (\count($items)) { |
|
| 106 | + foreach ($items as $item) { |
|
| 107 | + $this->save($item); |
|
| 108 | + } |
|
| 109 | + return true; |
|
| 110 | + } |
|
| 111 | + return false; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * @return string |
|
| 116 | + */ |
|
| 117 | + public function getHelp(): string |
|
| 118 | + { |
|
| 119 | + return ''; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 124 | + */ |
|
| 125 | + public function throwUnsupportedDriverReadAllPattern(string $linkReference = ''): void |
|
| 126 | + { |
|
| 127 | + throw new PhpfastcacheInvalidArgumentException( |
|
| 128 | + sprintf( |
|
| 129 | + '%s does not support a pattern argument.%s', |
|
| 130 | + $this->getDriverName(), |
|
| 131 | + $linkReference ? " See $linkReference" : '' |
|
| 132 | + ) |
|
| 133 | + ); |
|
| 134 | + } |
|
| 135 | 135 | } |
@@ -41,590 +41,590 @@ |
||
| 41 | 41 | */ |
| 42 | 42 | trait CacheItemPoolTrait |
| 43 | 43 | { |
| 44 | - use DriverBaseTrait { |
|
| 45 | - DriverBaseTrait::__construct as __driverBaseConstruct; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * @var string |
|
| 50 | - */ |
|
| 51 | - protected static string $unsupportedKeyChars = '{}()/\@:'; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @var ExtendedCacheItemInterface[]|CacheItemInterface[] |
|
| 55 | - */ |
|
| 56 | - protected array $deferredList = []; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @var ExtendedCacheItemInterface[]|CacheItemInterface[] |
|
| 60 | - */ |
|
| 61 | - protected array $itemInstances = []; |
|
| 62 | - |
|
| 63 | - protected DriverIO $IO; |
|
| 64 | - |
|
| 65 | - public function __construct(#[\SensitiveParameter] ConfigurationOptionInterface $config, string $instanceId, EventManagerInterface $em) |
|
| 66 | - { |
|
| 67 | - $this->IO = new DriverIO(); |
|
| 68 | - $this->__driverBaseConstruct($config, $instanceId, $em); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @throws PhpfastcacheLogicException |
|
| 73 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 74 | - */ |
|
| 75 | - public function setItem(CacheItemInterface $item): static |
|
| 76 | - { |
|
| 77 | - if (self::getItemClass() === $item::class) { |
|
| 78 | - if (!$this->getConfig()->isUseStaticItemCaching()) { |
|
| 79 | - throw new PhpfastcacheLogicException( |
|
| 80 | - 'The static item caching option (useStaticItemCaching) is disabled so you cannot attach an item.' |
|
| 81 | - ); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - $this->itemInstances[$item->getKey()] = $item; |
|
| 85 | - |
|
| 86 | - return $this; |
|
| 87 | - } |
|
| 88 | - throw new PhpfastcacheInvalidArgumentException( |
|
| 89 | - \sprintf( |
|
| 90 | - 'Invalid cache item class "%s" for driver "%s".', |
|
| 91 | - get_class($item), |
|
| 92 | - get_class($this) |
|
| 93 | - ) |
|
| 94 | - ); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * @inheritDoc |
|
| 99 | - * @throws PhpfastcacheCoreException |
|
| 100 | - * @throws PhpfastcacheDriverException |
|
| 101 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 102 | - * @throws PhpfastcacheLogicException |
|
| 103 | - */ |
|
| 104 | - public function getItems(array $keys = []): iterable |
|
| 105 | - { |
|
| 106 | - $items = []; |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Usually, drivers that are able to enable cache slams |
|
| 110 | - * does not benefit of driverReadMultiple() call. |
|
| 111 | - */ |
|
| 112 | - if (!$this->getConfig()->isPreventCacheSlams()) { |
|
| 113 | - $this->validateCacheKeys(...$keys); |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Check for local item instances first. |
|
| 117 | - */ |
|
| 118 | - foreach ($keys as $index => $key) { |
|
| 119 | - if (isset($this->itemInstances[$key]) && $this->getConfig()->isUseStaticItemCaching()) { |
|
| 120 | - $items[$key] = $this->itemInstances[$key]; |
|
| 121 | - // Key already exists in local item instances, no need to fetch it again. |
|
| 122 | - unset($keys[$index]); |
|
| 123 | - } |
|
| 124 | - } |
|
| 125 | - $keys = array_values($keys); |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * If there's still keys to fetch, let's choose the right method (if supported). |
|
| 129 | - */ |
|
| 130 | - if (count($keys) > 1) { |
|
| 131 | - $items = array_merge( |
|
| 132 | - array_combine($keys, array_map(fn($key) => new (self::getItemClass())($this, $key, $this->eventManager), $keys)), |
|
| 133 | - $items |
|
| 134 | - ); |
|
| 135 | - |
|
| 136 | - try { |
|
| 137 | - $driverArrays = $this->driverReadMultiple(...$items); |
|
| 138 | - } catch (PhpfastcacheUnsupportedMethodException) { |
|
| 139 | - /** |
|
| 140 | - * Fallback for drivers that does not yet implement driverReadMultiple() method. |
|
| 141 | - */ |
|
| 142 | - $driverArrays = array_combine( |
|
| 143 | - array_map(fn($item) => $item->getKey(), $items), |
|
| 144 | - array_map(fn($item) => $this->driverRead($item), $items) |
|
| 145 | - ); |
|
| 146 | - } finally { |
|
| 147 | - foreach ($items as $item) { |
|
| 148 | - $driverArray = $driverArrays[$item->getKey()] ?? null; |
|
| 149 | - if ($driverArray !== null) { |
|
| 150 | - $item->set($this->driverUnwrapData($driverArray)); |
|
| 151 | - $item->expiresAt($this->driverUnwrapEdate($driverArray)); |
|
| 152 | - if ($this->getConfig()->isItemDetailedDate()) { |
|
| 153 | - /** |
|
| 154 | - * If the itemDetailedDate has been |
|
| 155 | - * set after caching, we MUST inject |
|
| 156 | - * a new DateTime object on the fly |
|
| 157 | - */ |
|
| 158 | - $item->setCreationDate($this->driverUnwrapCdate($driverArray) ?: new DateTime()); |
|
| 159 | - $item->setModificationDate($this->driverUnwrapMdate($driverArray) ?: new DateTime()); |
|
| 160 | - } |
|
| 161 | - $item->setTags($this->driverUnwrapTags($driverArray)); |
|
| 162 | - $this->handleExpiredCacheItem($item); |
|
| 163 | - } else { |
|
| 164 | - $item->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())); |
|
| 165 | - } |
|
| 166 | - $item->isHit() ? $this->getIO()->incReadHit() : $this->getIO()->incReadMiss(); |
|
| 167 | - } |
|
| 168 | - } |
|
| 169 | - } else { |
|
| 170 | - $index = array_key_first($keys); |
|
| 171 | - if ($index !== null) { |
|
| 172 | - $items[$keys[$index]] = $this->getItem($keys[$index]); |
|
| 173 | - } |
|
| 174 | - } |
|
| 175 | - } else { |
|
| 176 | - $collection = []; |
|
| 177 | - |
|
| 178 | - foreach ($keys as $key) { |
|
| 179 | - $collection[$key] = $this->getItem($key); |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - return $collection; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - $this->eventManager->dispatch(Event::CACHE_GET_ITEMS, $this, $items); |
|
| 186 | - |
|
| 187 | - return $items; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * @param string $key |
|
| 192 | - * @return ExtendedCacheItemInterface |
|
| 193 | - * @throws PhpfastcacheCoreException |
|
| 194 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 195 | - * @throws PhpfastcacheLogicException |
|
| 196 | - * @throws PhpfastcacheDriverException |
|
| 197 | - * |
|
| 198 | - * @SuppressWarnings(PHPMD.NPathComplexity) |
|
| 199 | - * @SuppressWarnings(PHPMD.GotoStatement) |
|
| 200 | - */ |
|
| 201 | - public function getItem(string $key): ExtendedCacheItemInterface |
|
| 202 | - { |
|
| 203 | - /** |
|
| 204 | - * Replace array_key_exists by isset |
|
| 205 | - * due to performance issue on huge |
|
| 206 | - * loop dispatching operations |
|
| 207 | - */ |
|
| 208 | - if (!isset($this->itemInstances[$key]) || !$this->getConfig()->isUseStaticItemCaching()) { |
|
| 209 | - $this->validateCacheKeys($key); |
|
| 210 | - |
|
| 211 | - /** @var $item ExtendedCacheItemInterface */ |
|
| 212 | - $item = new (self::getItemClass())($this, $key, $this->eventManager); |
|
| 213 | - |
|
| 214 | - $getItemDriverRead = function (float $cacheSlamsSpendSeconds = 0) use (&$getItemDriverRead, $item): void { |
|
| 215 | - $config = $this->getConfig(); |
|
| 216 | - |
|
| 217 | - $driverArray = $this->driverRead($item); |
|
| 218 | - |
|
| 219 | - if ($driverArray) { |
|
| 220 | - $driverData = $this->driverUnwrapData($driverArray); |
|
| 221 | - |
|
| 222 | - if ($config instanceof IOConfigurationOptionInterface && $config->isPreventCacheSlams()) { |
|
| 223 | - while ($driverData instanceof ItemBatch) { |
|
| 224 | - if ($driverData->getItemDate()->getTimestamp() + $config->getCacheSlamsTimeout() < \time()) { |
|
| 225 | - /** |
|
| 226 | - * The timeout has been reached |
|
| 227 | - * Consider that the batch has |
|
| 228 | - * failed and serve an empty item |
|
| 229 | - * to avoid get stuck with a |
|
| 230 | - * batch item stored in driver |
|
| 231 | - */ |
|
| 232 | - $this->handleExpiredCacheItem($item); |
|
| 233 | - return; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - $this->eventManager->dispatch(Event::CACHE_GET_ITEM_IN_SLAM_BATCH, $this, $driverData, $cacheSlamsSpendSeconds); |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * Wait for a second before |
|
| 240 | - * attempting to get exit |
|
| 241 | - * the current batch process |
|
| 242 | - */ |
|
| 243 | - \usleep(100000); |
|
| 244 | - |
|
| 245 | - $getItemDriverRead($cacheSlamsSpendSeconds + 0.1); |
|
| 246 | - } |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - $item->set($driverData); |
|
| 250 | - $item->expiresAt($this->driverUnwrapEdate($driverArray)); |
|
| 251 | - |
|
| 252 | - if ($this->getConfig()->isItemDetailedDate()) { |
|
| 253 | - /** |
|
| 254 | - * If the itemDetailedDate has been |
|
| 255 | - * set after caching, we MUST inject |
|
| 256 | - * a new DateTime object on the fly |
|
| 257 | - */ |
|
| 258 | - $item->setCreationDate($this->driverUnwrapCdate($driverArray) ?: new DateTime()); |
|
| 259 | - $item->setModificationDate($this->driverUnwrapMdate($driverArray) ?: new DateTime()); |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - $item->setTags($this->driverUnwrapTags($driverArray)); |
|
| 263 | - $this->handleExpiredCacheItem($item); |
|
| 264 | - } else { |
|
| 265 | - $item->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())); |
|
| 266 | - } |
|
| 267 | - }; |
|
| 268 | - $getItemDriverRead(); |
|
| 269 | - } else { |
|
| 270 | - $item = $this->itemInstances[$key]; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - $this->eventManager->dispatch(Event::CACHE_GET_ITEM, $this, $item); |
|
| 274 | - |
|
| 275 | - $item->isHit() ? $this->getIO()->incReadHit() : $this->getIO()->incReadMiss(); |
|
| 276 | - |
|
| 277 | - return $item; |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - /** |
|
| 281 | - * @param string $key |
|
| 282 | - * @return bool |
|
| 283 | - * @throws PhpfastcacheCoreException |
|
| 284 | - * @throws PhpfastcacheDriverException |
|
| 285 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 286 | - * @throws PhpfastcacheLogicException |
|
| 287 | - */ |
|
| 288 | - public function hasItem(string $key): bool |
|
| 289 | - { |
|
| 290 | - return $this->getItem($key)->isHit(); |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * @return bool |
|
| 295 | - * @throws PhpfastcacheCoreException |
|
| 296 | - * @throws PhpfastcacheDriverException |
|
| 297 | - * @throws PhpfastcacheLogicException |
|
| 298 | - * @throws PhpfastcacheIOException |
|
| 299 | - */ |
|
| 300 | - public function clear(): bool |
|
| 301 | - { |
|
| 302 | - $this->eventManager->dispatch(Event::CACHE_CLEAR_ITEM, $this, $this->itemInstances); |
|
| 303 | - |
|
| 304 | - $this->getIO()->incWriteHit(); |
|
| 305 | - // Faster than detachAllItems() |
|
| 306 | - $this->itemInstances = []; |
|
| 307 | - |
|
| 308 | - return $this->driverClear(); |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - /** |
|
| 312 | - * @inheritDoc |
|
| 313 | - * @throws PhpfastcacheCoreException |
|
| 314 | - * @throws PhpfastcacheDriverException |
|
| 315 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 316 | - * @throws PhpfastcacheLogicException |
|
| 317 | - */ |
|
| 318 | - public function deleteItems(array $keys): bool |
|
| 319 | - { |
|
| 320 | - if (count($keys) > 1) { |
|
| 321 | - $return = true; |
|
| 322 | - try { |
|
| 323 | - $items = $this->getItems($keys); |
|
| 324 | - $return = $this->driverDeleteMultiple($keys); |
|
| 325 | - foreach ($items as $item) { |
|
| 326 | - $item->setHit(false); |
|
| 327 | - |
|
| 328 | - if (!\str_starts_with($item->getKey(), TaggableCacheItemPoolInterface::DRIVER_TAGS_KEY_PREFIX)) { |
|
| 329 | - $this->cleanItemTags($item); |
|
| 330 | - } |
|
| 331 | - } |
|
| 332 | - $this->getIO()->incWriteHit(); |
|
| 333 | - $this->eventManager->dispatch(Event::CACHE_DELETE_ITEMS, $this, $items); |
|
| 334 | - $this->deregisterItems($keys); |
|
| 335 | - } catch (PhpfastcacheUnsupportedMethodException) { |
|
| 336 | - foreach ($keys as $key) { |
|
| 337 | - $result = $this->deleteItem($key); |
|
| 338 | - if ($result !== true) { |
|
| 339 | - $return = false; |
|
| 340 | - } |
|
| 341 | - } |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - return $return; |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - $index = array_key_first($keys); |
|
| 348 | - if ($index !== null) { |
|
| 349 | - return $this->deleteItem($keys[$index]); |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - return false; |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - /** |
|
| 356 | - * @param string $key |
|
| 357 | - * @return bool |
|
| 358 | - * @throws PhpfastcacheCoreException |
|
| 359 | - * @throws PhpfastcacheDriverException |
|
| 360 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 361 | - * @throws PhpfastcacheLogicException |
|
| 362 | - */ |
|
| 363 | - public function deleteItem(string $key): bool |
|
| 364 | - { |
|
| 365 | - $item = $this->getItem($key); |
|
| 366 | - if ($item->isHit()) { |
|
| 367 | - $result = $this->driverDelete($item->getKey(), $item->getEncodedKey()); |
|
| 368 | - $item->setHit(false); |
|
| 369 | - $this->getIO()->incWriteHit(); |
|
| 370 | - |
|
| 371 | - $this->eventManager->dispatch(Event::CACHE_DELETE_ITEM, $this, $item); |
|
| 372 | - |
|
| 373 | - /** |
|
| 374 | - * De-register the item instance |
|
| 375 | - * then collect gc cycles |
|
| 376 | - */ |
|
| 377 | - $this->deregisterItem($key); |
|
| 378 | - |
|
| 379 | - /** |
|
| 380 | - * Perform a tag cleanup to avoid memory leaks |
|
| 381 | - */ |
|
| 382 | - if (!\str_starts_with($key, TaggableCacheItemPoolInterface::DRIVER_TAGS_KEY_PREFIX)) { |
|
| 383 | - $this->cleanItemTags($item); |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - return $result; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - return false; |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - /** |
|
| 393 | - * @param CacheItemInterface $item |
|
| 394 | - * @return bool |
|
| 395 | - */ |
|
| 396 | - public function saveDeferred(CacheItemInterface $item): bool |
|
| 397 | - { |
|
| 398 | - $this->assertCacheItemType($item, self::getItemClass()); |
|
| 399 | - if (!\array_key_exists($item->getKey(), $this->itemInstances)) { |
|
| 400 | - $this->itemInstances[$item->getKey()] = $item; |
|
| 401 | - } elseif (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
| 402 | - throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.'); |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - $this->eventManager->dispatch(Event::CACHE_SAVE_DEFERRED_ITEM, $this, $item); |
|
| 406 | - $this->deferredList[$item->getKey()] = $item; |
|
| 407 | - |
|
| 408 | - return true; |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - /** |
|
| 412 | - * @return bool |
|
| 413 | - * @throws PhpfastcacheCoreException |
|
| 414 | - * @throws PhpfastcacheDriverException |
|
| 415 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 416 | - * @throws PhpfastcacheLogicException |
|
| 417 | - */ |
|
| 418 | - public function commit(): bool |
|
| 419 | - { |
|
| 420 | - $this->eventManager->dispatch(Event::CACHE_COMMIT_ITEM, $this, new EventReferenceParameter($this->deferredList)); |
|
| 421 | - |
|
| 422 | - if (\count($this->deferredList)) { |
|
| 423 | - $return = true; |
|
| 424 | - foreach ($this->deferredList as $key => $item) { |
|
| 425 | - $result = $this->save($item); |
|
| 426 | - unset($this->deferredList[$key]); |
|
| 427 | - if ($result !== true) { |
|
| 428 | - $return = $result; |
|
| 429 | - } |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - return $return; |
|
| 433 | - } |
|
| 434 | - return false; |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - /** |
|
| 438 | - * @param CacheItemInterface $item |
|
| 439 | - * @return bool |
|
| 440 | - * @throws PhpfastcacheCoreException |
|
| 441 | - * @throws PhpfastcacheDriverException |
|
| 442 | - * @throws PhpfastcacheIOException |
|
| 443 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 444 | - * @throws PhpfastcacheLogicException |
|
| 445 | - */ |
|
| 446 | - public function save(CacheItemInterface $item): bool |
|
| 447 | - { |
|
| 448 | - $this->assertCacheItemType($item, self::getItemClass()); |
|
| 449 | - /** |
|
| 450 | - * @var ExtendedCacheItemInterface $item |
|
| 451 | - * |
|
| 452 | - * Replace array_key_exists by isset |
|
| 453 | - * due to performance issue on huge |
|
| 454 | - * loop dispatching operations |
|
| 455 | - */ |
|
| 456 | - if (!isset($this->itemInstances[$item->getKey()])) { |
|
| 457 | - if ($this->getConfig()->isUseStaticItemCaching()) { |
|
| 458 | - $this->itemInstances[$item->getKey()] = $item; |
|
| 459 | - } |
|
| 460 | - } elseif (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
| 461 | - throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.'); |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - $this->assertCacheItemType($item, self::getItemClass()); |
|
| 465 | - $this->eventManager->dispatch(Event::CACHE_SAVE_ITEM, $this, $item); |
|
| 466 | - |
|
| 467 | - if ($this->getConfig() instanceof IOConfigurationOptionInterface && $this->getConfig()->isPreventCacheSlams()) { |
|
| 468 | - /** |
|
| 469 | - * @var $itemBatch ExtendedCacheItemInterface |
|
| 470 | - */ |
|
| 471 | - $itemClassName = self::getItemClass(); |
|
| 472 | - $itemBatch = new $itemClassName($this, $item->getKey(), $this->eventManager); |
|
| 473 | - $itemBatch->set(new ItemBatch($item->getKey(), new DateTime())) |
|
| 474 | - ->expiresAfter($this->getConfig()->getCacheSlamsTimeout()); |
|
| 475 | - |
|
| 476 | - /** |
|
| 477 | - * To avoid SPL mismatches |
|
| 478 | - * we have to re-attach the |
|
| 479 | - * original item to the pool |
|
| 480 | - */ |
|
| 481 | - $this->driverWrite($itemBatch); |
|
| 482 | - $this->detachItem($itemBatch); |
|
| 483 | - $this->attachItem($item); |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - |
|
| 487 | - if ($this->driverWrite($item) && $this->driverWriteTags($item)) { |
|
| 488 | - $item->setHit(true) |
|
| 489 | - ->clearRemovedTags(); |
|
| 490 | - |
|
| 491 | - if ($this->getConfig()->isItemDetailedDate()) { |
|
| 492 | - $item->setModificationDate(new \DateTime()); |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - $this->getIO()->incWriteHit(); |
|
| 496 | - |
|
| 497 | - return true; |
|
| 498 | - } |
|
| 499 | - |
|
| 500 | - return false; |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - /** |
|
| 504 | - * @return DriverIO |
|
| 505 | - */ |
|
| 506 | - public function getIO(): DriverIO |
|
| 507 | - { |
|
| 508 | - return $this->IO; |
|
| 509 | - } |
|
| 510 | - |
|
| 511 | - /** |
|
| 512 | - * @internal This method de-register an item from $this->itemInstances |
|
| 513 | - */ |
|
| 514 | - protected function deregisterItem(string $itemKey): static |
|
| 515 | - { |
|
| 516 | - unset($this->itemInstances[$itemKey]); |
|
| 517 | - |
|
| 518 | - if (\gc_enabled()) { |
|
| 519 | - \gc_collect_cycles(); |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - return $this; |
|
| 523 | - } |
|
| 524 | - |
|
| 525 | - /** |
|
| 526 | - * @param string[] $itemKeys |
|
| 527 | - * @internal This method de-register multiple items from $this->itemInstances |
|
| 528 | - */ |
|
| 529 | - protected function deregisterItems(array $itemKeys): static |
|
| 530 | - { |
|
| 531 | - $this->itemInstances = array_diff_key($this->itemInstances, array_flip($itemKeys)); |
|
| 532 | - |
|
| 533 | - if (\gc_enabled()) { |
|
| 534 | - \gc_collect_cycles(); |
|
| 535 | - } |
|
| 536 | - |
|
| 537 | - return $this; |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - /** |
|
| 541 | - * @throws PhpfastcacheLogicException |
|
| 542 | - */ |
|
| 543 | - public function attachItem(CacheItemInterface $item): static |
|
| 544 | - { |
|
| 545 | - if (isset($this->itemInstances[$item->getKey()]) && \spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
| 546 | - throw new PhpfastcacheLogicException( |
|
| 547 | - 'The item already exists and cannot be overwritten because the Spl object hash mismatches ! |
|
| 44 | + use DriverBaseTrait { |
|
| 45 | + DriverBaseTrait::__construct as __driverBaseConstruct; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * @var string |
|
| 50 | + */ |
|
| 51 | + protected static string $unsupportedKeyChars = '{}()/\@:'; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @var ExtendedCacheItemInterface[]|CacheItemInterface[] |
|
| 55 | + */ |
|
| 56 | + protected array $deferredList = []; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @var ExtendedCacheItemInterface[]|CacheItemInterface[] |
|
| 60 | + */ |
|
| 61 | + protected array $itemInstances = []; |
|
| 62 | + |
|
| 63 | + protected DriverIO $IO; |
|
| 64 | + |
|
| 65 | + public function __construct(#[\SensitiveParameter] ConfigurationOptionInterface $config, string $instanceId, EventManagerInterface $em) |
|
| 66 | + { |
|
| 67 | + $this->IO = new DriverIO(); |
|
| 68 | + $this->__driverBaseConstruct($config, $instanceId, $em); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @throws PhpfastcacheLogicException |
|
| 73 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 74 | + */ |
|
| 75 | + public function setItem(CacheItemInterface $item): static |
|
| 76 | + { |
|
| 77 | + if (self::getItemClass() === $item::class) { |
|
| 78 | + if (!$this->getConfig()->isUseStaticItemCaching()) { |
|
| 79 | + throw new PhpfastcacheLogicException( |
|
| 80 | + 'The static item caching option (useStaticItemCaching) is disabled so you cannot attach an item.' |
|
| 81 | + ); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + $this->itemInstances[$item->getKey()] = $item; |
|
| 85 | + |
|
| 86 | + return $this; |
|
| 87 | + } |
|
| 88 | + throw new PhpfastcacheInvalidArgumentException( |
|
| 89 | + \sprintf( |
|
| 90 | + 'Invalid cache item class "%s" for driver "%s".', |
|
| 91 | + get_class($item), |
|
| 92 | + get_class($this) |
|
| 93 | + ) |
|
| 94 | + ); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * @inheritDoc |
|
| 99 | + * @throws PhpfastcacheCoreException |
|
| 100 | + * @throws PhpfastcacheDriverException |
|
| 101 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 102 | + * @throws PhpfastcacheLogicException |
|
| 103 | + */ |
|
| 104 | + public function getItems(array $keys = []): iterable |
|
| 105 | + { |
|
| 106 | + $items = []; |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Usually, drivers that are able to enable cache slams |
|
| 110 | + * does not benefit of driverReadMultiple() call. |
|
| 111 | + */ |
|
| 112 | + if (!$this->getConfig()->isPreventCacheSlams()) { |
|
| 113 | + $this->validateCacheKeys(...$keys); |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Check for local item instances first. |
|
| 117 | + */ |
|
| 118 | + foreach ($keys as $index => $key) { |
|
| 119 | + if (isset($this->itemInstances[$key]) && $this->getConfig()->isUseStaticItemCaching()) { |
|
| 120 | + $items[$key] = $this->itemInstances[$key]; |
|
| 121 | + // Key already exists in local item instances, no need to fetch it again. |
|
| 122 | + unset($keys[$index]); |
|
| 123 | + } |
|
| 124 | + } |
|
| 125 | + $keys = array_values($keys); |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * If there's still keys to fetch, let's choose the right method (if supported). |
|
| 129 | + */ |
|
| 130 | + if (count($keys) > 1) { |
|
| 131 | + $items = array_merge( |
|
| 132 | + array_combine($keys, array_map(fn($key) => new (self::getItemClass())($this, $key, $this->eventManager), $keys)), |
|
| 133 | + $items |
|
| 134 | + ); |
|
| 135 | + |
|
| 136 | + try { |
|
| 137 | + $driverArrays = $this->driverReadMultiple(...$items); |
|
| 138 | + } catch (PhpfastcacheUnsupportedMethodException) { |
|
| 139 | + /** |
|
| 140 | + * Fallback for drivers that does not yet implement driverReadMultiple() method. |
|
| 141 | + */ |
|
| 142 | + $driverArrays = array_combine( |
|
| 143 | + array_map(fn($item) => $item->getKey(), $items), |
|
| 144 | + array_map(fn($item) => $this->driverRead($item), $items) |
|
| 145 | + ); |
|
| 146 | + } finally { |
|
| 147 | + foreach ($items as $item) { |
|
| 148 | + $driverArray = $driverArrays[$item->getKey()] ?? null; |
|
| 149 | + if ($driverArray !== null) { |
|
| 150 | + $item->set($this->driverUnwrapData($driverArray)); |
|
| 151 | + $item->expiresAt($this->driverUnwrapEdate($driverArray)); |
|
| 152 | + if ($this->getConfig()->isItemDetailedDate()) { |
|
| 153 | + /** |
|
| 154 | + * If the itemDetailedDate has been |
|
| 155 | + * set after caching, we MUST inject |
|
| 156 | + * a new DateTime object on the fly |
|
| 157 | + */ |
|
| 158 | + $item->setCreationDate($this->driverUnwrapCdate($driverArray) ?: new DateTime()); |
|
| 159 | + $item->setModificationDate($this->driverUnwrapMdate($driverArray) ?: new DateTime()); |
|
| 160 | + } |
|
| 161 | + $item->setTags($this->driverUnwrapTags($driverArray)); |
|
| 162 | + $this->handleExpiredCacheItem($item); |
|
| 163 | + } else { |
|
| 164 | + $item->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())); |
|
| 165 | + } |
|
| 166 | + $item->isHit() ? $this->getIO()->incReadHit() : $this->getIO()->incReadMiss(); |
|
| 167 | + } |
|
| 168 | + } |
|
| 169 | + } else { |
|
| 170 | + $index = array_key_first($keys); |
|
| 171 | + if ($index !== null) { |
|
| 172 | + $items[$keys[$index]] = $this->getItem($keys[$index]); |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | + } else { |
|
| 176 | + $collection = []; |
|
| 177 | + |
|
| 178 | + foreach ($keys as $key) { |
|
| 179 | + $collection[$key] = $this->getItem($key); |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + return $collection; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + $this->eventManager->dispatch(Event::CACHE_GET_ITEMS, $this, $items); |
|
| 186 | + |
|
| 187 | + return $items; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * @param string $key |
|
| 192 | + * @return ExtendedCacheItemInterface |
|
| 193 | + * @throws PhpfastcacheCoreException |
|
| 194 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 195 | + * @throws PhpfastcacheLogicException |
|
| 196 | + * @throws PhpfastcacheDriverException |
|
| 197 | + * |
|
| 198 | + * @SuppressWarnings(PHPMD.NPathComplexity) |
|
| 199 | + * @SuppressWarnings(PHPMD.GotoStatement) |
|
| 200 | + */ |
|
| 201 | + public function getItem(string $key): ExtendedCacheItemInterface |
|
| 202 | + { |
|
| 203 | + /** |
|
| 204 | + * Replace array_key_exists by isset |
|
| 205 | + * due to performance issue on huge |
|
| 206 | + * loop dispatching operations |
|
| 207 | + */ |
|
| 208 | + if (!isset($this->itemInstances[$key]) || !$this->getConfig()->isUseStaticItemCaching()) { |
|
| 209 | + $this->validateCacheKeys($key); |
|
| 210 | + |
|
| 211 | + /** @var $item ExtendedCacheItemInterface */ |
|
| 212 | + $item = new (self::getItemClass())($this, $key, $this->eventManager); |
|
| 213 | + |
|
| 214 | + $getItemDriverRead = function (float $cacheSlamsSpendSeconds = 0) use (&$getItemDriverRead, $item): void { |
|
| 215 | + $config = $this->getConfig(); |
|
| 216 | + |
|
| 217 | + $driverArray = $this->driverRead($item); |
|
| 218 | + |
|
| 219 | + if ($driverArray) { |
|
| 220 | + $driverData = $this->driverUnwrapData($driverArray); |
|
| 221 | + |
|
| 222 | + if ($config instanceof IOConfigurationOptionInterface && $config->isPreventCacheSlams()) { |
|
| 223 | + while ($driverData instanceof ItemBatch) { |
|
| 224 | + if ($driverData->getItemDate()->getTimestamp() + $config->getCacheSlamsTimeout() < \time()) { |
|
| 225 | + /** |
|
| 226 | + * The timeout has been reached |
|
| 227 | + * Consider that the batch has |
|
| 228 | + * failed and serve an empty item |
|
| 229 | + * to avoid get stuck with a |
|
| 230 | + * batch item stored in driver |
|
| 231 | + */ |
|
| 232 | + $this->handleExpiredCacheItem($item); |
|
| 233 | + return; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + $this->eventManager->dispatch(Event::CACHE_GET_ITEM_IN_SLAM_BATCH, $this, $driverData, $cacheSlamsSpendSeconds); |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * Wait for a second before |
|
| 240 | + * attempting to get exit |
|
| 241 | + * the current batch process |
|
| 242 | + */ |
|
| 243 | + \usleep(100000); |
|
| 244 | + |
|
| 245 | + $getItemDriverRead($cacheSlamsSpendSeconds + 0.1); |
|
| 246 | + } |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + $item->set($driverData); |
|
| 250 | + $item->expiresAt($this->driverUnwrapEdate($driverArray)); |
|
| 251 | + |
|
| 252 | + if ($this->getConfig()->isItemDetailedDate()) { |
|
| 253 | + /** |
|
| 254 | + * If the itemDetailedDate has been |
|
| 255 | + * set after caching, we MUST inject |
|
| 256 | + * a new DateTime object on the fly |
|
| 257 | + */ |
|
| 258 | + $item->setCreationDate($this->driverUnwrapCdate($driverArray) ?: new DateTime()); |
|
| 259 | + $item->setModificationDate($this->driverUnwrapMdate($driverArray) ?: new DateTime()); |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + $item->setTags($this->driverUnwrapTags($driverArray)); |
|
| 263 | + $this->handleExpiredCacheItem($item); |
|
| 264 | + } else { |
|
| 265 | + $item->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())); |
|
| 266 | + } |
|
| 267 | + }; |
|
| 268 | + $getItemDriverRead(); |
|
| 269 | + } else { |
|
| 270 | + $item = $this->itemInstances[$key]; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + $this->eventManager->dispatch(Event::CACHE_GET_ITEM, $this, $item); |
|
| 274 | + |
|
| 275 | + $item->isHit() ? $this->getIO()->incReadHit() : $this->getIO()->incReadMiss(); |
|
| 276 | + |
|
| 277 | + return $item; |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + /** |
|
| 281 | + * @param string $key |
|
| 282 | + * @return bool |
|
| 283 | + * @throws PhpfastcacheCoreException |
|
| 284 | + * @throws PhpfastcacheDriverException |
|
| 285 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 286 | + * @throws PhpfastcacheLogicException |
|
| 287 | + */ |
|
| 288 | + public function hasItem(string $key): bool |
|
| 289 | + { |
|
| 290 | + return $this->getItem($key)->isHit(); |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * @return bool |
|
| 295 | + * @throws PhpfastcacheCoreException |
|
| 296 | + * @throws PhpfastcacheDriverException |
|
| 297 | + * @throws PhpfastcacheLogicException |
|
| 298 | + * @throws PhpfastcacheIOException |
|
| 299 | + */ |
|
| 300 | + public function clear(): bool |
|
| 301 | + { |
|
| 302 | + $this->eventManager->dispatch(Event::CACHE_CLEAR_ITEM, $this, $this->itemInstances); |
|
| 303 | + |
|
| 304 | + $this->getIO()->incWriteHit(); |
|
| 305 | + // Faster than detachAllItems() |
|
| 306 | + $this->itemInstances = []; |
|
| 307 | + |
|
| 308 | + return $this->driverClear(); |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + /** |
|
| 312 | + * @inheritDoc |
|
| 313 | + * @throws PhpfastcacheCoreException |
|
| 314 | + * @throws PhpfastcacheDriverException |
|
| 315 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 316 | + * @throws PhpfastcacheLogicException |
|
| 317 | + */ |
|
| 318 | + public function deleteItems(array $keys): bool |
|
| 319 | + { |
|
| 320 | + if (count($keys) > 1) { |
|
| 321 | + $return = true; |
|
| 322 | + try { |
|
| 323 | + $items = $this->getItems($keys); |
|
| 324 | + $return = $this->driverDeleteMultiple($keys); |
|
| 325 | + foreach ($items as $item) { |
|
| 326 | + $item->setHit(false); |
|
| 327 | + |
|
| 328 | + if (!\str_starts_with($item->getKey(), TaggableCacheItemPoolInterface::DRIVER_TAGS_KEY_PREFIX)) { |
|
| 329 | + $this->cleanItemTags($item); |
|
| 330 | + } |
|
| 331 | + } |
|
| 332 | + $this->getIO()->incWriteHit(); |
|
| 333 | + $this->eventManager->dispatch(Event::CACHE_DELETE_ITEMS, $this, $items); |
|
| 334 | + $this->deregisterItems($keys); |
|
| 335 | + } catch (PhpfastcacheUnsupportedMethodException) { |
|
| 336 | + foreach ($keys as $key) { |
|
| 337 | + $result = $this->deleteItem($key); |
|
| 338 | + if ($result !== true) { |
|
| 339 | + $return = false; |
|
| 340 | + } |
|
| 341 | + } |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + return $return; |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + $index = array_key_first($keys); |
|
| 348 | + if ($index !== null) { |
|
| 349 | + return $this->deleteItem($keys[$index]); |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + return false; |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + /** |
|
| 356 | + * @param string $key |
|
| 357 | + * @return bool |
|
| 358 | + * @throws PhpfastcacheCoreException |
|
| 359 | + * @throws PhpfastcacheDriverException |
|
| 360 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 361 | + * @throws PhpfastcacheLogicException |
|
| 362 | + */ |
|
| 363 | + public function deleteItem(string $key): bool |
|
| 364 | + { |
|
| 365 | + $item = $this->getItem($key); |
|
| 366 | + if ($item->isHit()) { |
|
| 367 | + $result = $this->driverDelete($item->getKey(), $item->getEncodedKey()); |
|
| 368 | + $item->setHit(false); |
|
| 369 | + $this->getIO()->incWriteHit(); |
|
| 370 | + |
|
| 371 | + $this->eventManager->dispatch(Event::CACHE_DELETE_ITEM, $this, $item); |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | + * De-register the item instance |
|
| 375 | + * then collect gc cycles |
|
| 376 | + */ |
|
| 377 | + $this->deregisterItem($key); |
|
| 378 | + |
|
| 379 | + /** |
|
| 380 | + * Perform a tag cleanup to avoid memory leaks |
|
| 381 | + */ |
|
| 382 | + if (!\str_starts_with($key, TaggableCacheItemPoolInterface::DRIVER_TAGS_KEY_PREFIX)) { |
|
| 383 | + $this->cleanItemTags($item); |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + return $result; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + return false; |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + /** |
|
| 393 | + * @param CacheItemInterface $item |
|
| 394 | + * @return bool |
|
| 395 | + */ |
|
| 396 | + public function saveDeferred(CacheItemInterface $item): bool |
|
| 397 | + { |
|
| 398 | + $this->assertCacheItemType($item, self::getItemClass()); |
|
| 399 | + if (!\array_key_exists($item->getKey(), $this->itemInstances)) { |
|
| 400 | + $this->itemInstances[$item->getKey()] = $item; |
|
| 401 | + } elseif (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
| 402 | + throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.'); |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + $this->eventManager->dispatch(Event::CACHE_SAVE_DEFERRED_ITEM, $this, $item); |
|
| 406 | + $this->deferredList[$item->getKey()] = $item; |
|
| 407 | + |
|
| 408 | + return true; |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + /** |
|
| 412 | + * @return bool |
|
| 413 | + * @throws PhpfastcacheCoreException |
|
| 414 | + * @throws PhpfastcacheDriverException |
|
| 415 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 416 | + * @throws PhpfastcacheLogicException |
|
| 417 | + */ |
|
| 418 | + public function commit(): bool |
|
| 419 | + { |
|
| 420 | + $this->eventManager->dispatch(Event::CACHE_COMMIT_ITEM, $this, new EventReferenceParameter($this->deferredList)); |
|
| 421 | + |
|
| 422 | + if (\count($this->deferredList)) { |
|
| 423 | + $return = true; |
|
| 424 | + foreach ($this->deferredList as $key => $item) { |
|
| 425 | + $result = $this->save($item); |
|
| 426 | + unset($this->deferredList[$key]); |
|
| 427 | + if ($result !== true) { |
|
| 428 | + $return = $result; |
|
| 429 | + } |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + return $return; |
|
| 433 | + } |
|
| 434 | + return false; |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + /** |
|
| 438 | + * @param CacheItemInterface $item |
|
| 439 | + * @return bool |
|
| 440 | + * @throws PhpfastcacheCoreException |
|
| 441 | + * @throws PhpfastcacheDriverException |
|
| 442 | + * @throws PhpfastcacheIOException |
|
| 443 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 444 | + * @throws PhpfastcacheLogicException |
|
| 445 | + */ |
|
| 446 | + public function save(CacheItemInterface $item): bool |
|
| 447 | + { |
|
| 448 | + $this->assertCacheItemType($item, self::getItemClass()); |
|
| 449 | + /** |
|
| 450 | + * @var ExtendedCacheItemInterface $item |
|
| 451 | + * |
|
| 452 | + * Replace array_key_exists by isset |
|
| 453 | + * due to performance issue on huge |
|
| 454 | + * loop dispatching operations |
|
| 455 | + */ |
|
| 456 | + if (!isset($this->itemInstances[$item->getKey()])) { |
|
| 457 | + if ($this->getConfig()->isUseStaticItemCaching()) { |
|
| 458 | + $this->itemInstances[$item->getKey()] = $item; |
|
| 459 | + } |
|
| 460 | + } elseif (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
| 461 | + throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.'); |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + $this->assertCacheItemType($item, self::getItemClass()); |
|
| 465 | + $this->eventManager->dispatch(Event::CACHE_SAVE_ITEM, $this, $item); |
|
| 466 | + |
|
| 467 | + if ($this->getConfig() instanceof IOConfigurationOptionInterface && $this->getConfig()->isPreventCacheSlams()) { |
|
| 468 | + /** |
|
| 469 | + * @var $itemBatch ExtendedCacheItemInterface |
|
| 470 | + */ |
|
| 471 | + $itemClassName = self::getItemClass(); |
|
| 472 | + $itemBatch = new $itemClassName($this, $item->getKey(), $this->eventManager); |
|
| 473 | + $itemBatch->set(new ItemBatch($item->getKey(), new DateTime())) |
|
| 474 | + ->expiresAfter($this->getConfig()->getCacheSlamsTimeout()); |
|
| 475 | + |
|
| 476 | + /** |
|
| 477 | + * To avoid SPL mismatches |
|
| 478 | + * we have to re-attach the |
|
| 479 | + * original item to the pool |
|
| 480 | + */ |
|
| 481 | + $this->driverWrite($itemBatch); |
|
| 482 | + $this->detachItem($itemBatch); |
|
| 483 | + $this->attachItem($item); |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + |
|
| 487 | + if ($this->driverWrite($item) && $this->driverWriteTags($item)) { |
|
| 488 | + $item->setHit(true) |
|
| 489 | + ->clearRemovedTags(); |
|
| 490 | + |
|
| 491 | + if ($this->getConfig()->isItemDetailedDate()) { |
|
| 492 | + $item->setModificationDate(new \DateTime()); |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + $this->getIO()->incWriteHit(); |
|
| 496 | + |
|
| 497 | + return true; |
|
| 498 | + } |
|
| 499 | + |
|
| 500 | + return false; |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + /** |
|
| 504 | + * @return DriverIO |
|
| 505 | + */ |
|
| 506 | + public function getIO(): DriverIO |
|
| 507 | + { |
|
| 508 | + return $this->IO; |
|
| 509 | + } |
|
| 510 | + |
|
| 511 | + /** |
|
| 512 | + * @internal This method de-register an item from $this->itemInstances |
|
| 513 | + */ |
|
| 514 | + protected function deregisterItem(string $itemKey): static |
|
| 515 | + { |
|
| 516 | + unset($this->itemInstances[$itemKey]); |
|
| 517 | + |
|
| 518 | + if (\gc_enabled()) { |
|
| 519 | + \gc_collect_cycles(); |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + return $this; |
|
| 523 | + } |
|
| 524 | + |
|
| 525 | + /** |
|
| 526 | + * @param string[] $itemKeys |
|
| 527 | + * @internal This method de-register multiple items from $this->itemInstances |
|
| 528 | + */ |
|
| 529 | + protected function deregisterItems(array $itemKeys): static |
|
| 530 | + { |
|
| 531 | + $this->itemInstances = array_diff_key($this->itemInstances, array_flip($itemKeys)); |
|
| 532 | + |
|
| 533 | + if (\gc_enabled()) { |
|
| 534 | + \gc_collect_cycles(); |
|
| 535 | + } |
|
| 536 | + |
|
| 537 | + return $this; |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + /** |
|
| 541 | + * @throws PhpfastcacheLogicException |
|
| 542 | + */ |
|
| 543 | + public function attachItem(CacheItemInterface $item): static |
|
| 544 | + { |
|
| 545 | + if (isset($this->itemInstances[$item->getKey()]) && \spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
| 546 | + throw new PhpfastcacheLogicException( |
|
| 547 | + 'The item already exists and cannot be overwritten because the Spl object hash mismatches ! |
|
| 548 | 548 | You probably tried to re-attach a detached item which has been already retrieved from cache.' |
| 549 | - ); |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - if (!$this->getConfig()->isUseStaticItemCaching()) { |
|
| 553 | - throw new PhpfastcacheLogicException( |
|
| 554 | - 'The static item caching option (useStaticItemCaching) is disabled so you cannot attach an item.' |
|
| 555 | - ); |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - $this->itemInstances[$item->getKey()] = $item; |
|
| 559 | - |
|
| 560 | - return $this; |
|
| 561 | - } |
|
| 562 | - |
|
| 563 | - public function isAttached(CacheItemInterface $item): bool |
|
| 564 | - { |
|
| 565 | - if (isset($this->itemInstances[$item->getKey()])) { |
|
| 566 | - return \spl_object_hash($item) === \spl_object_hash($this->itemInstances[$item->getKey()]); |
|
| 567 | - } |
|
| 568 | - return false; |
|
| 569 | - } |
|
| 570 | - |
|
| 571 | - protected function validateCacheKeys(string ...$keys): void |
|
| 572 | - { |
|
| 573 | - foreach ($keys as $key) { |
|
| 574 | - if (\preg_match('~([' . \preg_quote(self::$unsupportedKeyChars, '~') . ']+)~', $key, $matches)) { |
|
| 575 | - throw new PhpfastcacheInvalidArgumentException( |
|
| 576 | - 'Unsupported key character detected: "' . $matches[1] . '". |
|
| 549 | + ); |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + if (!$this->getConfig()->isUseStaticItemCaching()) { |
|
| 553 | + throw new PhpfastcacheLogicException( |
|
| 554 | + 'The static item caching option (useStaticItemCaching) is disabled so you cannot attach an item.' |
|
| 555 | + ); |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + $this->itemInstances[$item->getKey()] = $item; |
|
| 559 | + |
|
| 560 | + return $this; |
|
| 561 | + } |
|
| 562 | + |
|
| 563 | + public function isAttached(CacheItemInterface $item): bool |
|
| 564 | + { |
|
| 565 | + if (isset($this->itemInstances[$item->getKey()])) { |
|
| 566 | + return \spl_object_hash($item) === \spl_object_hash($this->itemInstances[$item->getKey()]); |
|
| 567 | + } |
|
| 568 | + return false; |
|
| 569 | + } |
|
| 570 | + |
|
| 571 | + protected function validateCacheKeys(string ...$keys): void |
|
| 572 | + { |
|
| 573 | + foreach ($keys as $key) { |
|
| 574 | + if (\preg_match('~([' . \preg_quote(self::$unsupportedKeyChars, '~') . ']+)~', $key, $matches)) { |
|
| 575 | + throw new PhpfastcacheInvalidArgumentException( |
|
| 576 | + 'Unsupported key character detected: "' . $matches[1] . '". |
|
| 577 | 577 | Please check: https://github.com/PHPSocialNetwork/phpfastcache/wiki/%5BV6%5D-Unsupported-characters-in-key-identifiers' |
| 578 | - ); |
|
| 579 | - } |
|
| 580 | - } |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - protected function handleExpiredCacheItem(ExtendedCacheItemInterface $item): void |
|
| 584 | - { |
|
| 585 | - if ($item->isExpired()) { |
|
| 586 | - /** |
|
| 587 | - * Using driverDelete() instead of delete() |
|
| 588 | - * to avoid infinite loop caused by |
|
| 589 | - * getItem() call in delete() method |
|
| 590 | - * As we MUST return an item in any |
|
| 591 | - * way, we do not de-register here |
|
| 592 | - */ |
|
| 593 | - $this->driverDelete($item->getKey(), $item->getEncodedKey()); |
|
| 594 | - |
|
| 595 | - /** |
|
| 596 | - * Reset the Item |
|
| 597 | - */ |
|
| 598 | - $item->set(null) |
|
| 599 | - ->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())) |
|
| 600 | - ->setHit(false) |
|
| 601 | - ->setTags([]); |
|
| 602 | - |
|
| 603 | - if ($this->getConfig()->isItemDetailedDate()) { |
|
| 604 | - /** |
|
| 605 | - * If the itemDetailedDate has been |
|
| 606 | - * set after caching, we MUST inject |
|
| 607 | - * a new DateTime object on the fly |
|
| 608 | - */ |
|
| 609 | - $item->setCreationDate(new DateTime()); |
|
| 610 | - $item->setModificationDate(new DateTime()); |
|
| 611 | - } |
|
| 612 | - } else { |
|
| 613 | - $item->setHit(true); |
|
| 614 | - } |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - /** |
|
| 618 | - * @param ExtendedCacheItemInterface[] $items |
|
| 619 | - * @param bool $encoded |
|
| 620 | - * @param string $keyPrefix |
|
| 621 | - * @return string[] |
|
| 622 | - */ |
|
| 623 | - protected function getKeys(array $items, bool $encoded = false, string $keyPrefix = ''): array |
|
| 624 | - { |
|
| 625 | - return array_map( |
|
| 626 | - static fn(ExtendedCacheItemInterface $item) => $keyPrefix . ($encoded ? $item->getEncodedKey() : $item->getKey()), |
|
| 627 | - $items |
|
| 628 | - ); |
|
| 629 | - } |
|
| 578 | + ); |
|
| 579 | + } |
|
| 580 | + } |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + protected function handleExpiredCacheItem(ExtendedCacheItemInterface $item): void |
|
| 584 | + { |
|
| 585 | + if ($item->isExpired()) { |
|
| 586 | + /** |
|
| 587 | + * Using driverDelete() instead of delete() |
|
| 588 | + * to avoid infinite loop caused by |
|
| 589 | + * getItem() call in delete() method |
|
| 590 | + * As we MUST return an item in any |
|
| 591 | + * way, we do not de-register here |
|
| 592 | + */ |
|
| 593 | + $this->driverDelete($item->getKey(), $item->getEncodedKey()); |
|
| 594 | + |
|
| 595 | + /** |
|
| 596 | + * Reset the Item |
|
| 597 | + */ |
|
| 598 | + $item->set(null) |
|
| 599 | + ->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())) |
|
| 600 | + ->setHit(false) |
|
| 601 | + ->setTags([]); |
|
| 602 | + |
|
| 603 | + if ($this->getConfig()->isItemDetailedDate()) { |
|
| 604 | + /** |
|
| 605 | + * If the itemDetailedDate has been |
|
| 606 | + * set after caching, we MUST inject |
|
| 607 | + * a new DateTime object on the fly |
|
| 608 | + */ |
|
| 609 | + $item->setCreationDate(new DateTime()); |
|
| 610 | + $item->setModificationDate(new DateTime()); |
|
| 611 | + } |
|
| 612 | + } else { |
|
| 613 | + $item->setHit(true); |
|
| 614 | + } |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + /** |
|
| 618 | + * @param ExtendedCacheItemInterface[] $items |
|
| 619 | + * @param bool $encoded |
|
| 620 | + * @param string $keyPrefix |
|
| 621 | + * @return string[] |
|
| 622 | + */ |
|
| 623 | + protected function getKeys(array $items, bool $encoded = false, string $keyPrefix = ''): array |
|
| 624 | + { |
|
| 625 | + return array_map( |
|
| 626 | + static fn(ExtendedCacheItemInterface $item) => $keyPrefix . ($encoded ? $item->getEncodedKey() : $item->getKey()), |
|
| 627 | + $items |
|
| 628 | + ); |
|
| 629 | + } |
|
| 630 | 630 | } |
@@ -211,7 +211,7 @@ |
||
| 211 | 211 | /** @var $item ExtendedCacheItemInterface */ |
| 212 | 212 | $item = new (self::getItemClass())($this, $key, $this->eventManager); |
| 213 | 213 | |
| 214 | - $getItemDriverRead = function (float $cacheSlamsSpendSeconds = 0) use (&$getItemDriverRead, $item): void { |
|
| 214 | + $getItemDriverRead = function(float $cacheSlamsSpendSeconds = 0) use (&$getItemDriverRead, $item): void { |
|
| 215 | 215 | $config = $this->getConfig(); |
| 216 | 216 | |
| 217 | 217 | $driverArray = $this->driverRead($item); |
@@ -62,8 +62,7 @@ discard block |
||
| 62 | 62 | |
| 63 | 63 | protected DriverIO $IO; |
| 64 | 64 | |
| 65 | - public function __construct(#[\SensitiveParameter] ConfigurationOptionInterface $config, string $instanceId, EventManagerInterface $em) |
|
| 66 | - { |
|
| 65 | + public function __construct(#[\SensitiveParameter] ConfigurationOptionInterface $config, string $instanceId, EventManagerInterface $em) { |
|
| 67 | 66 | $this->IO = new DriverIO(); |
| 68 | 67 | $this->__driverBaseConstruct($config, $instanceId, $em); |
| 69 | 68 | } |
@@ -135,7 +134,8 @@ discard block |
||
| 135 | 134 | |
| 136 | 135 | try { |
| 137 | 136 | $driverArrays = $this->driverReadMultiple(...$items); |
| 138 | - } catch (PhpfastcacheUnsupportedMethodException) { |
|
| 137 | + } |
|
| 138 | + catch (PhpfastcacheUnsupportedMethodException) { |
|
| 139 | 139 | /** |
| 140 | 140 | * Fallback for drivers that does not yet implement driverReadMultiple() method. |
| 141 | 141 | */ |
@@ -143,7 +143,8 @@ discard block |
||
| 143 | 143 | array_map(fn($item) => $item->getKey(), $items), |
| 144 | 144 | array_map(fn($item) => $this->driverRead($item), $items) |
| 145 | 145 | ); |
| 146 | - } finally { |
|
| 146 | + } |
|
| 147 | + finally { |
|
| 147 | 148 | foreach ($items as $item) { |
| 148 | 149 | $driverArray = $driverArrays[$item->getKey()] ?? null; |
| 149 | 150 | if ($driverArray !== null) { |
@@ -160,19 +161,22 @@ discard block |
||
| 160 | 161 | } |
| 161 | 162 | $item->setTags($this->driverUnwrapTags($driverArray)); |
| 162 | 163 | $this->handleExpiredCacheItem($item); |
| 163 | - } else { |
|
| 164 | + } |
|
| 165 | + else { |
|
| 164 | 166 | $item->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())); |
| 165 | 167 | } |
| 166 | 168 | $item->isHit() ? $this->getIO()->incReadHit() : $this->getIO()->incReadMiss(); |
| 167 | 169 | } |
| 168 | 170 | } |
| 169 | - } else { |
|
| 171 | + } |
|
| 172 | + else { |
|
| 170 | 173 | $index = array_key_first($keys); |
| 171 | 174 | if ($index !== null) { |
| 172 | 175 | $items[$keys[$index]] = $this->getItem($keys[$index]); |
| 173 | 176 | } |
| 174 | 177 | } |
| 175 | - } else { |
|
| 178 | + } |
|
| 179 | + else { |
|
| 176 | 180 | $collection = []; |
| 177 | 181 | |
| 178 | 182 | foreach ($keys as $key) { |
@@ -261,12 +265,14 @@ discard block |
||
| 261 | 265 | |
| 262 | 266 | $item->setTags($this->driverUnwrapTags($driverArray)); |
| 263 | 267 | $this->handleExpiredCacheItem($item); |
| 264 | - } else { |
|
| 268 | + } |
|
| 269 | + else { |
|
| 265 | 270 | $item->expiresAfter((int) abs($this->getConfig()->getDefaultTtl())); |
| 266 | 271 | } |
| 267 | 272 | }; |
| 268 | 273 | $getItemDriverRead(); |
| 269 | - } else { |
|
| 274 | + } |
|
| 275 | + else { |
|
| 270 | 276 | $item = $this->itemInstances[$key]; |
| 271 | 277 | } |
| 272 | 278 | |
@@ -332,7 +338,8 @@ discard block |
||
| 332 | 338 | $this->getIO()->incWriteHit(); |
| 333 | 339 | $this->eventManager->dispatch(Event::CACHE_DELETE_ITEMS, $this, $items); |
| 334 | 340 | $this->deregisterItems($keys); |
| 335 | - } catch (PhpfastcacheUnsupportedMethodException) { |
|
| 341 | + } |
|
| 342 | + catch (PhpfastcacheUnsupportedMethodException) { |
|
| 336 | 343 | foreach ($keys as $key) { |
| 337 | 344 | $result = $this->deleteItem($key); |
| 338 | 345 | if ($result !== true) { |
@@ -398,7 +405,8 @@ discard block |
||
| 398 | 405 | $this->assertCacheItemType($item, self::getItemClass()); |
| 399 | 406 | if (!\array_key_exists($item->getKey(), $this->itemInstances)) { |
| 400 | 407 | $this->itemInstances[$item->getKey()] = $item; |
| 401 | - } elseif (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
| 408 | + } |
|
| 409 | + elseif (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
| 402 | 410 | throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.'); |
| 403 | 411 | } |
| 404 | 412 | |
@@ -457,7 +465,8 @@ discard block |
||
| 457 | 465 | if ($this->getConfig()->isUseStaticItemCaching()) { |
| 458 | 466 | $this->itemInstances[$item->getKey()] = $item; |
| 459 | 467 | } |
| 460 | - } elseif (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
| 468 | + } |
|
| 469 | + elseif (\spl_object_hash($item) !== \spl_object_hash($this->itemInstances[$item->getKey()])) { |
|
| 461 | 470 | throw new RuntimeException('Spl object hash mismatches ! You probably tried to save a detached item which has been already retrieved from cache.'); |
| 462 | 471 | } |
| 463 | 472 | |
@@ -609,7 +618,8 @@ discard block |
||
| 609 | 618 | $item->setCreationDate(new DateTime()); |
| 610 | 619 | $item->setModificationDate(new DateTime()); |
| 611 | 620 | } |
| 612 | - } else { |
|
| 621 | + } |
|
| 622 | + else { |
|
| 613 | 623 | $item->setHit(true); |
| 614 | 624 | } |
| 615 | 625 | } |
@@ -21,268 +21,268 @@ |
||
| 21 | 21 | |
| 22 | 22 | interface TaggableCacheItemPoolInterface |
| 23 | 23 | { |
| 24 | - public const DRIVER_TAGS_KEY_PREFIX = '_TAG_'; |
|
| 24 | + public const DRIVER_TAGS_KEY_PREFIX = '_TAG_'; |
|
| 25 | 25 | |
| 26 | - public const DRIVER_TAGS_WRAPPER_INDEX = 'g'; |
|
| 26 | + public const DRIVER_TAGS_WRAPPER_INDEX = 'g'; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Allows you to get cache item(s) by at least **ONE** of the specified matching tag(s). |
|
| 30 | - * Default behavior |
|
| 31 | - */ |
|
| 32 | - public const TAG_STRATEGY_ONE = 1; |
|
| 28 | + /** |
|
| 29 | + * Allows you to get cache item(s) by at least **ONE** of the specified matching tag(s). |
|
| 30 | + * Default behavior |
|
| 31 | + */ |
|
| 32 | + public const TAG_STRATEGY_ONE = 1; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Allows you to get cache item(s) by **ALL** of the specified matching tag(s) |
|
| 36 | - * The cache item *CAN* have additional tag(s) |
|
| 37 | - */ |
|
| 38 | - public const TAG_STRATEGY_ALL = 2; |
|
| 34 | + /** |
|
| 35 | + * Allows you to get cache item(s) by **ALL** of the specified matching tag(s) |
|
| 36 | + * The cache item *CAN* have additional tag(s) |
|
| 37 | + */ |
|
| 38 | + public const TAG_STRATEGY_ALL = 2; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Allows you to get cache item(s) by **ONLY** the specified matching tag(s) |
|
| 42 | - * The cache item *CANNOT* have additional tag(s) |
|
| 43 | - */ |
|
| 44 | - public const TAG_STRATEGY_ONLY = 4; |
|
| 40 | + /** |
|
| 41 | + * Allows you to get cache item(s) by **ONLY** the specified matching tag(s) |
|
| 42 | + * The cache item *CANNOT* have additional tag(s) |
|
| 43 | + */ |
|
| 44 | + public const TAG_STRATEGY_ONLY = 4; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * Returns a traversable set of cache items by a tag name. |
|
| 48 | - * |
|
| 49 | - * @param string $tagName |
|
| 50 | - * An indexed array of keys of items to retrieve. |
|
| 51 | - * |
|
| 52 | - * @param int $strategy |
|
| 53 | - * |
|
| 54 | - * @return ExtendedCacheItemInterface[] |
|
| 55 | - * A traversable collection of Cache Items keyed by the cache keys of |
|
| 56 | - * each item. A Cache item will be returned for each key, even if that |
|
| 57 | - * key is not found. However, if no keys are specified then an empty |
|
| 58 | - * traversable MUST be returned instead. |
|
| 59 | - * @throws InvalidArgumentException |
|
| 60 | - * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
| 61 | - * MUST be thrown. |
|
| 62 | - * |
|
| 63 | - */ |
|
| 64 | - public function getItemsByTag(string $tagName, int $strategy = self::TAG_STRATEGY_ONE): array; |
|
| 46 | + /** |
|
| 47 | + * Returns a traversable set of cache items by a tag name. |
|
| 48 | + * |
|
| 49 | + * @param string $tagName |
|
| 50 | + * An indexed array of keys of items to retrieve. |
|
| 51 | + * |
|
| 52 | + * @param int $strategy |
|
| 53 | + * |
|
| 54 | + * @return ExtendedCacheItemInterface[] |
|
| 55 | + * A traversable collection of Cache Items keyed by the cache keys of |
|
| 56 | + * each item. A Cache item will be returned for each key, even if that |
|
| 57 | + * key is not found. However, if no keys are specified then an empty |
|
| 58 | + * traversable MUST be returned instead. |
|
| 59 | + * @throws InvalidArgumentException |
|
| 60 | + * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
| 61 | + * MUST be thrown. |
|
| 62 | + * |
|
| 63 | + */ |
|
| 64 | + public function getItemsByTag(string $tagName, int $strategy = self::TAG_STRATEGY_ONE): array; |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Returns a traversable set of cache items by one of multiple tag names. |
|
| 68 | - * |
|
| 69 | - * @param string[] $tagNames |
|
| 70 | - * An indexed array of keys of items to retrieve. |
|
| 71 | - * |
|
| 72 | - * @param int $strategy |
|
| 73 | - * |
|
| 74 | - * @return ExtendedCacheItemInterface[] |
|
| 75 | - * A traversable collection of Cache Items keyed by the cache keys of |
|
| 76 | - * each item. A Cache item will be returned for each key, even if that |
|
| 77 | - * key is not found. However, if no keys are specified then an empty |
|
| 78 | - * traversable MUST be returned instead. |
|
| 79 | - * @throws InvalidArgumentException |
|
| 80 | - * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
| 81 | - * MUST be thrown. |
|
| 82 | - * |
|
| 83 | - */ |
|
| 84 | - public function getItemsByTags(array $tagNames, int $strategy = self::TAG_STRATEGY_ONE): array; |
|
| 66 | + /** |
|
| 67 | + * Returns a traversable set of cache items by one of multiple tag names. |
|
| 68 | + * |
|
| 69 | + * @param string[] $tagNames |
|
| 70 | + * An indexed array of keys of items to retrieve. |
|
| 71 | + * |
|
| 72 | + * @param int $strategy |
|
| 73 | + * |
|
| 74 | + * @return ExtendedCacheItemInterface[] |
|
| 75 | + * A traversable collection of Cache Items keyed by the cache keys of |
|
| 76 | + * each item. A Cache item will be returned for each key, even if that |
|
| 77 | + * key is not found. However, if no keys are specified then an empty |
|
| 78 | + * traversable MUST be returned instead. |
|
| 79 | + * @throws InvalidArgumentException |
|
| 80 | + * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
| 81 | + * MUST be thrown. |
|
| 82 | + * |
|
| 83 | + */ |
|
| 84 | + public function getItemsByTags(array $tagNames, int $strategy = self::TAG_STRATEGY_ONE): array; |
|
| 85 | 85 | |
| 86 | - /** |
|
| 87 | - * Returns A json string that represents an array of items by tags-based. |
|
| 88 | - * |
|
| 89 | - * @param string[] $tagNames |
|
| 90 | - * An indexed array of keys of items to retrieve. |
|
| 91 | - * @param int $option \json_encode() options |
|
| 92 | - * @param int $depth \json_encode() depth |
|
| 93 | - * @param int $strategy |
|
| 94 | - * |
|
| 95 | - * @return string |
|
| 96 | - * @throws InvalidArgumentException |
|
| 97 | - * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
| 98 | - * MUST be thrown. |
|
| 99 | - * |
|
| 100 | - */ |
|
| 101 | - public function getItemsByTagsAsJsonString(array $tagNames, int $option = \JSON_THROW_ON_ERROR, int $depth = 512, int $strategy = self::TAG_STRATEGY_ONE): string; |
|
| 86 | + /** |
|
| 87 | + * Returns A json string that represents an array of items by tags-based. |
|
| 88 | + * |
|
| 89 | + * @param string[] $tagNames |
|
| 90 | + * An indexed array of keys of items to retrieve. |
|
| 91 | + * @param int $option \json_encode() options |
|
| 92 | + * @param int $depth \json_encode() depth |
|
| 93 | + * @param int $strategy |
|
| 94 | + * |
|
| 95 | + * @return string |
|
| 96 | + * @throws InvalidArgumentException |
|
| 97 | + * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
| 98 | + * MUST be thrown. |
|
| 99 | + * |
|
| 100 | + */ |
|
| 101 | + public function getItemsByTagsAsJsonString(array $tagNames, int $option = \JSON_THROW_ON_ERROR, int $depth = 512, int $strategy = self::TAG_STRATEGY_ONE): string; |
|
| 102 | 102 | |
| 103 | - /** |
|
| 104 | - * Removes the item from the pool by tag. |
|
| 105 | - * |
|
| 106 | - * @param string $tagName |
|
| 107 | - * The tag for which to delete |
|
| 108 | - * |
|
| 109 | - * @param int $strategy |
|
| 110 | - * |
|
| 111 | - * @return bool |
|
| 112 | - * True if the item was successfully removed. False if there was an error. |
|
| 113 | - * @throws InvalidArgumentException |
|
| 114 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
| 115 | - * MUST be thrown. |
|
| 116 | - * |
|
| 117 | - */ |
|
| 118 | - public function deleteItemsByTag(string $tagName, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 103 | + /** |
|
| 104 | + * Removes the item from the pool by tag. |
|
| 105 | + * |
|
| 106 | + * @param string $tagName |
|
| 107 | + * The tag for which to delete |
|
| 108 | + * |
|
| 109 | + * @param int $strategy |
|
| 110 | + * |
|
| 111 | + * @return bool |
|
| 112 | + * True if the item was successfully removed. False if there was an error. |
|
| 113 | + * @throws InvalidArgumentException |
|
| 114 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
| 115 | + * MUST be thrown. |
|
| 116 | + * |
|
| 117 | + */ |
|
| 118 | + public function deleteItemsByTag(string $tagName, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * Removes the item from the pool by one of multiple tag names. |
|
| 122 | - * |
|
| 123 | - * @param string[] $tagNames |
|
| 124 | - * The tag for which to delete |
|
| 125 | - * |
|
| 126 | - * @param int $strategy |
|
| 127 | - * |
|
| 128 | - * @return bool |
|
| 129 | - * True if the items were successfully removed. False if there was an error. |
|
| 130 | - * @throws InvalidArgumentException |
|
| 131 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
| 132 | - * MUST be thrown. |
|
| 133 | - * |
|
| 134 | - */ |
|
| 135 | - public function deleteItemsByTags(array $tagNames, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 120 | + /** |
|
| 121 | + * Removes the item from the pool by one of multiple tag names. |
|
| 122 | + * |
|
| 123 | + * @param string[] $tagNames |
|
| 124 | + * The tag for which to delete |
|
| 125 | + * |
|
| 126 | + * @param int $strategy |
|
| 127 | + * |
|
| 128 | + * @return bool |
|
| 129 | + * True if the items were successfully removed. False if there was an error. |
|
| 130 | + * @throws InvalidArgumentException |
|
| 131 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
| 132 | + * MUST be thrown. |
|
| 133 | + * |
|
| 134 | + */ |
|
| 135 | + public function deleteItemsByTags(array $tagNames, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 136 | 136 | |
| 137 | - /** |
|
| 138 | - * Increment the items from the pool by tag. |
|
| 139 | - * |
|
| 140 | - * @param string $tagName |
|
| 141 | - * The tag for which to increment |
|
| 142 | - * |
|
| 143 | - * @param int $step |
|
| 144 | - * |
|
| 145 | - * @param int $strategy |
|
| 146 | - * |
|
| 147 | - * @return bool |
|
| 148 | - * True if the item was successfully incremented. False if there was an error. |
|
| 149 | - * @throws InvalidArgumentException |
|
| 150 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
| 151 | - * MUST be thrown. |
|
| 152 | - * |
|
| 153 | - */ |
|
| 154 | - public function incrementItemsByTag(string $tagName, int $step = 1, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 137 | + /** |
|
| 138 | + * Increment the items from the pool by tag. |
|
| 139 | + * |
|
| 140 | + * @param string $tagName |
|
| 141 | + * The tag for which to increment |
|
| 142 | + * |
|
| 143 | + * @param int $step |
|
| 144 | + * |
|
| 145 | + * @param int $strategy |
|
| 146 | + * |
|
| 147 | + * @return bool |
|
| 148 | + * True if the item was successfully incremented. False if there was an error. |
|
| 149 | + * @throws InvalidArgumentException |
|
| 150 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
| 151 | + * MUST be thrown. |
|
| 152 | + * |
|
| 153 | + */ |
|
| 154 | + public function incrementItemsByTag(string $tagName, int $step = 1, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 155 | 155 | |
| 156 | - /** |
|
| 157 | - * Increment the items from the pool by one of multiple tag names. |
|
| 158 | - * |
|
| 159 | - * @param string[] $tagNames |
|
| 160 | - * The tag for which to increment |
|
| 161 | - * |
|
| 162 | - * @param int $step |
|
| 163 | - * |
|
| 164 | - * @param int $strategy |
|
| 165 | - * |
|
| 166 | - * @return bool |
|
| 167 | - * True if the items were successfully incremented. False if there was an error. |
|
| 168 | - * @throws InvalidArgumentException |
|
| 169 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
| 170 | - * MUST be thrown. |
|
| 171 | - * |
|
| 172 | - */ |
|
| 173 | - public function incrementItemsByTags(array $tagNames, int $step = 1, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 156 | + /** |
|
| 157 | + * Increment the items from the pool by one of multiple tag names. |
|
| 158 | + * |
|
| 159 | + * @param string[] $tagNames |
|
| 160 | + * The tag for which to increment |
|
| 161 | + * |
|
| 162 | + * @param int $step |
|
| 163 | + * |
|
| 164 | + * @param int $strategy |
|
| 165 | + * |
|
| 166 | + * @return bool |
|
| 167 | + * True if the items were successfully incremented. False if there was an error. |
|
| 168 | + * @throws InvalidArgumentException |
|
| 169 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
| 170 | + * MUST be thrown. |
|
| 171 | + * |
|
| 172 | + */ |
|
| 173 | + public function incrementItemsByTags(array $tagNames, int $step = 1, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 174 | 174 | |
| 175 | - /** |
|
| 176 | - * Decrement the items from the pool by tag. |
|
| 177 | - * |
|
| 178 | - * @param string $tagName |
|
| 179 | - * The tag for which to decrement |
|
| 180 | - * |
|
| 181 | - * @param int $step |
|
| 182 | - * |
|
| 183 | - * @param int $strategy |
|
| 184 | - * |
|
| 185 | - * @return bool |
|
| 186 | - * True if the item was successfully decremented. False if there was an error. |
|
| 187 | - * @throws InvalidArgumentException |
|
| 188 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
| 189 | - * MUST be thrown. |
|
| 190 | - * |
|
| 191 | - */ |
|
| 192 | - public function decrementItemsByTag(string $tagName, int $step = 1, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 175 | + /** |
|
| 176 | + * Decrement the items from the pool by tag. |
|
| 177 | + * |
|
| 178 | + * @param string $tagName |
|
| 179 | + * The tag for which to decrement |
|
| 180 | + * |
|
| 181 | + * @param int $step |
|
| 182 | + * |
|
| 183 | + * @param int $strategy |
|
| 184 | + * |
|
| 185 | + * @return bool |
|
| 186 | + * True if the item was successfully decremented. False if there was an error. |
|
| 187 | + * @throws InvalidArgumentException |
|
| 188 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
| 189 | + * MUST be thrown. |
|
| 190 | + * |
|
| 191 | + */ |
|
| 192 | + public function decrementItemsByTag(string $tagName, int $step = 1, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 193 | 193 | |
| 194 | - /** |
|
| 195 | - * Decrement the items from the pool by one of multiple tag names. |
|
| 196 | - * |
|
| 197 | - * @param string[] $tagNames |
|
| 198 | - * The tag for which to decrement |
|
| 199 | - * |
|
| 200 | - * @param int $step |
|
| 201 | - * |
|
| 202 | - * @param int $strategy |
|
| 203 | - * |
|
| 204 | - * @return bool |
|
| 205 | - * True if the item was successfully decremented. False if there was an error. |
|
| 206 | - * @throws InvalidArgumentException |
|
| 207 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
| 208 | - * MUST be thrown. |
|
| 209 | - * |
|
| 210 | - */ |
|
| 211 | - public function decrementItemsByTags(array $tagNames, int $step = 1, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 194 | + /** |
|
| 195 | + * Decrement the items from the pool by one of multiple tag names. |
|
| 196 | + * |
|
| 197 | + * @param string[] $tagNames |
|
| 198 | + * The tag for which to decrement |
|
| 199 | + * |
|
| 200 | + * @param int $step |
|
| 201 | + * |
|
| 202 | + * @param int $strategy |
|
| 203 | + * |
|
| 204 | + * @return bool |
|
| 205 | + * True if the item was successfully decremented. False if there was an error. |
|
| 206 | + * @throws InvalidArgumentException |
|
| 207 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
| 208 | + * MUST be thrown. |
|
| 209 | + * |
|
| 210 | + */ |
|
| 211 | + public function decrementItemsByTags(array $tagNames, int $step = 1, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 212 | 212 | |
| 213 | - /** |
|
| 214 | - * Decrement the items from the pool by tag. |
|
| 215 | - * |
|
| 216 | - * @param string $tagName |
|
| 217 | - * The tag for which to append |
|
| 218 | - * |
|
| 219 | - * @param array<mixed>|string $data |
|
| 220 | - * |
|
| 221 | - * @param int $strategy |
|
| 222 | - * |
|
| 223 | - * @return bool |
|
| 224 | - * True if the item was successfully appended. False if there was an error. |
|
| 225 | - * @throws InvalidArgumentException |
|
| 226 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
| 227 | - * MUST be thrown. |
|
| 228 | - * |
|
| 229 | - */ |
|
| 230 | - public function appendItemsByTag(string $tagName, array|string $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 213 | + /** |
|
| 214 | + * Decrement the items from the pool by tag. |
|
| 215 | + * |
|
| 216 | + * @param string $tagName |
|
| 217 | + * The tag for which to append |
|
| 218 | + * |
|
| 219 | + * @param array<mixed>|string $data |
|
| 220 | + * |
|
| 221 | + * @param int $strategy |
|
| 222 | + * |
|
| 223 | + * @return bool |
|
| 224 | + * True if the item was successfully appended. False if there was an error. |
|
| 225 | + * @throws InvalidArgumentException |
|
| 226 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
| 227 | + * MUST be thrown. |
|
| 228 | + * |
|
| 229 | + */ |
|
| 230 | + public function appendItemsByTag(string $tagName, array|string $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 231 | 231 | |
| 232 | - /** |
|
| 233 | - * Append the items from the pool by one of multiple tag names. |
|
| 234 | - * |
|
| 235 | - * @param string[] $tagNames |
|
| 236 | - * The tag for which to append |
|
| 237 | - * |
|
| 238 | - * @param array<mixed>|string $data |
|
| 239 | - * |
|
| 240 | - * @param int $strategy |
|
| 241 | - * |
|
| 242 | - * @return bool |
|
| 243 | - * True if the items were successfully appended. False if there was an error. |
|
| 244 | - * @throws InvalidArgumentException |
|
| 245 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
| 246 | - * MUST be thrown. |
|
| 247 | - * |
|
| 248 | - */ |
|
| 249 | - public function appendItemsByTags(array $tagNames, array|string $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 232 | + /** |
|
| 233 | + * Append the items from the pool by one of multiple tag names. |
|
| 234 | + * |
|
| 235 | + * @param string[] $tagNames |
|
| 236 | + * The tag for which to append |
|
| 237 | + * |
|
| 238 | + * @param array<mixed>|string $data |
|
| 239 | + * |
|
| 240 | + * @param int $strategy |
|
| 241 | + * |
|
| 242 | + * @return bool |
|
| 243 | + * True if the items were successfully appended. False if there was an error. |
|
| 244 | + * @throws InvalidArgumentException |
|
| 245 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
| 246 | + * MUST be thrown. |
|
| 247 | + * |
|
| 248 | + */ |
|
| 249 | + public function appendItemsByTags(array $tagNames, array|string $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 250 | 250 | |
| 251 | - /** |
|
| 252 | - * Prepend the items from the pool by tag. |
|
| 253 | - * |
|
| 254 | - * @param string $tagName |
|
| 255 | - * The tag for which to prepend |
|
| 256 | - * |
|
| 257 | - * @param array<mixed>|string $data |
|
| 258 | - * |
|
| 259 | - * @param int $strategy |
|
| 260 | - * |
|
| 261 | - * @return bool |
|
| 262 | - * True if the item was successfully prepended. False if there was an error. |
|
| 263 | - * @throws InvalidArgumentException |
|
| 264 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
| 265 | - * MUST be thrown. |
|
| 266 | - * |
|
| 267 | - */ |
|
| 268 | - public function prependItemsByTag(string $tagName, array|string $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 251 | + /** |
|
| 252 | + * Prepend the items from the pool by tag. |
|
| 253 | + * |
|
| 254 | + * @param string $tagName |
|
| 255 | + * The tag for which to prepend |
|
| 256 | + * |
|
| 257 | + * @param array<mixed>|string $data |
|
| 258 | + * |
|
| 259 | + * @param int $strategy |
|
| 260 | + * |
|
| 261 | + * @return bool |
|
| 262 | + * True if the item was successfully prepended. False if there was an error. |
|
| 263 | + * @throws InvalidArgumentException |
|
| 264 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
| 265 | + * MUST be thrown. |
|
| 266 | + * |
|
| 267 | + */ |
|
| 268 | + public function prependItemsByTag(string $tagName, array|string $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 269 | 269 | |
| 270 | - /** |
|
| 271 | - * Prepend the items from the pool by one of multiple tag names. |
|
| 272 | - * |
|
| 273 | - * @param string[] $tagNames |
|
| 274 | - * The tag for which to prepend |
|
| 275 | - * |
|
| 276 | - * @param array<mixed>|string $data |
|
| 277 | - * |
|
| 278 | - * @param int $strategy |
|
| 279 | - * |
|
| 280 | - * @return bool |
|
| 281 | - * True if the item was successfully prepended. False if there was an error. |
|
| 282 | - * @throws InvalidArgumentException |
|
| 283 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
| 284 | - * MUST be thrown. |
|
| 285 | - * |
|
| 286 | - */ |
|
| 287 | - public function prependItemsByTags(array $tagNames, array|string $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 270 | + /** |
|
| 271 | + * Prepend the items from the pool by one of multiple tag names. |
|
| 272 | + * |
|
| 273 | + * @param string[] $tagNames |
|
| 274 | + * The tag for which to prepend |
|
| 275 | + * |
|
| 276 | + * @param array<mixed>|string $data |
|
| 277 | + * |
|
| 278 | + * @param int $strategy |
|
| 279 | + * |
|
| 280 | + * @return bool |
|
| 281 | + * True if the item was successfully prepended. False if there was an error. |
|
| 282 | + * @throws InvalidArgumentException |
|
| 283 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
| 284 | + * MUST be thrown. |
|
| 285 | + * |
|
| 286 | + */ |
|
| 287 | + public function prependItemsByTags(array $tagNames, array|string $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 288 | 288 | } |
@@ -227,7 +227,7 @@ discard block |
||
| 227 | 227 | * MUST be thrown. |
| 228 | 228 | * |
| 229 | 229 | */ |
| 230 | - public function appendItemsByTag(string $tagName, array|string $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 230 | + public function appendItemsByTag(string $tagName, array | string $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 231 | 231 | |
| 232 | 232 | /** |
| 233 | 233 | * Append the items from the pool by one of multiple tag names. |
@@ -246,7 +246,7 @@ discard block |
||
| 246 | 246 | * MUST be thrown. |
| 247 | 247 | * |
| 248 | 248 | */ |
| 249 | - public function appendItemsByTags(array $tagNames, array|string $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 249 | + public function appendItemsByTags(array $tagNames, array | string $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 250 | 250 | |
| 251 | 251 | /** |
| 252 | 252 | * Prepend the items from the pool by tag. |
@@ -265,7 +265,7 @@ discard block |
||
| 265 | 265 | * MUST be thrown. |
| 266 | 266 | * |
| 267 | 267 | */ |
| 268 | - public function prependItemsByTag(string $tagName, array|string $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 268 | + public function prependItemsByTag(string $tagName, array | string $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 269 | 269 | |
| 270 | 270 | /** |
| 271 | 271 | * Prepend the items from the pool by one of multiple tag names. |
@@ -284,5 +284,5 @@ discard block |
||
| 284 | 284 | * MUST be thrown. |
| 285 | 285 | * |
| 286 | 286 | */ |
| 287 | - public function prependItemsByTags(array $tagNames, array|string $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 287 | + public function prependItemsByTags(array $tagNames, array | string $data, int $strategy = self::TAG_STRATEGY_ONE): bool; |
|
| 288 | 288 | } |
@@ -24,101 +24,101 @@ |
||
| 24 | 24 | |
| 25 | 25 | trait DriverPoolAbstractTrait |
| 26 | 26 | { |
| 27 | - /** |
|
| 28 | - * @return bool |
|
| 29 | - */ |
|
| 30 | - abstract protected function driverCheck(): bool; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @return bool |
|
| 34 | - */ |
|
| 35 | - abstract protected function driverConnect(): bool; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @param ExtendedCacheItemInterface $item |
|
| 39 | - * @return ?array<string, mixed> |
|
| 40 | - */ |
|
| 41 | - abstract protected function driverRead(ExtendedCacheItemInterface $item): ?array; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * @param ExtendedCacheItemInterface ...$items |
|
| 45 | - * @return array<array<string, mixed>> |
|
| 46 | - * @throws PhpfastcacheUnsupportedMethodException |
|
| 47 | - */ |
|
| 48 | - protected function driverReadMultiple(ExtendedCacheItemInterface ...$items): array |
|
| 49 | - { |
|
| 50 | - throw new PhpfastcacheUnsupportedMethodException(); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @return \Traversable<int, string> |
|
| 55 | - * @throws PhpfastcacheUnsupportedMethodException |
|
| 56 | - */ |
|
| 57 | - protected function driverReadAllKeys(string $pattern = ''): iterable |
|
| 58 | - { |
|
| 59 | - throw new PhpfastcacheUnsupportedMethodException( |
|
| 60 | - sprintf( |
|
| 61 | - 'The "readAll" operation is unsupported by the the "%s" driver. See the Wiki for more information at %s', |
|
| 62 | - $this->getDriverName(), |
|
| 63 | - Wiki::FETCH_ALL_KEY_URL, |
|
| 64 | - ) |
|
| 65 | - ); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @param ExtendedCacheItemInterface $item |
|
| 70 | - * @return bool |
|
| 71 | - */ |
|
| 72 | - abstract protected function driverWrite(ExtendedCacheItemInterface $item): bool; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @param ExtendedCacheItemInterface ...$item |
|
| 76 | - * @return bool |
|
| 77 | - * @throws PhpfastcacheUnsupportedMethodException |
|
| 78 | - */ |
|
| 79 | - protected function driverWriteMultiple(ExtendedCacheItemInterface ...$item): bool |
|
| 80 | - { |
|
| 81 | - /** |
|
| 82 | - * @todo Implement bulk writes to be for v10: |
|
| 83 | - * For methods commit() and saveMultiple() |
|
| 84 | - */ |
|
| 85 | - throw new PhpfastcacheUnsupportedMethodException(); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * @param string $key |
|
| 91 | - * @param string $encodedKey |
|
| 92 | - * @return bool |
|
| 93 | - */ |
|
| 94 | - abstract protected function driverDelete(string $key, string $encodedKey): bool; |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @param string[] $keys |
|
| 98 | - * @return bool |
|
| 99 | - * @throws PhpfastcacheUnsupportedMethodException |
|
| 100 | - */ |
|
| 101 | - protected function driverDeleteMultiple(array $keys): bool |
|
| 102 | - { |
|
| 103 | - throw new PhpfastcacheUnsupportedMethodException(); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * @return bool |
|
| 108 | - */ |
|
| 109 | - abstract protected function driverClear(): bool; |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * @param CacheItemInterface $item |
|
| 113 | - * @param string $expectedClassType |
|
| 114 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 115 | - */ |
|
| 116 | - protected function assertCacheItemType(CacheItemInterface $item, string $expectedClassType): void |
|
| 117 | - { |
|
| 118 | - if (!($item instanceof $expectedClassType)) { |
|
| 119 | - throw new PhpfastcacheInvalidArgumentException( |
|
| 120 | - \sprintf('Cross-driver type confusion detected: Expected "%s" object, got "%s"', $expectedClassType, $item::class) |
|
| 121 | - ); |
|
| 122 | - } |
|
| 123 | - } |
|
| 27 | + /** |
|
| 28 | + * @return bool |
|
| 29 | + */ |
|
| 30 | + abstract protected function driverCheck(): bool; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @return bool |
|
| 34 | + */ |
|
| 35 | + abstract protected function driverConnect(): bool; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @param ExtendedCacheItemInterface $item |
|
| 39 | + * @return ?array<string, mixed> |
|
| 40 | + */ |
|
| 41 | + abstract protected function driverRead(ExtendedCacheItemInterface $item): ?array; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * @param ExtendedCacheItemInterface ...$items |
|
| 45 | + * @return array<array<string, mixed>> |
|
| 46 | + * @throws PhpfastcacheUnsupportedMethodException |
|
| 47 | + */ |
|
| 48 | + protected function driverReadMultiple(ExtendedCacheItemInterface ...$items): array |
|
| 49 | + { |
|
| 50 | + throw new PhpfastcacheUnsupportedMethodException(); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @return \Traversable<int, string> |
|
| 55 | + * @throws PhpfastcacheUnsupportedMethodException |
|
| 56 | + */ |
|
| 57 | + protected function driverReadAllKeys(string $pattern = ''): iterable |
|
| 58 | + { |
|
| 59 | + throw new PhpfastcacheUnsupportedMethodException( |
|
| 60 | + sprintf( |
|
| 61 | + 'The "readAll" operation is unsupported by the the "%s" driver. See the Wiki for more information at %s', |
|
| 62 | + $this->getDriverName(), |
|
| 63 | + Wiki::FETCH_ALL_KEY_URL, |
|
| 64 | + ) |
|
| 65 | + ); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @param ExtendedCacheItemInterface $item |
|
| 70 | + * @return bool |
|
| 71 | + */ |
|
| 72 | + abstract protected function driverWrite(ExtendedCacheItemInterface $item): bool; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @param ExtendedCacheItemInterface ...$item |
|
| 76 | + * @return bool |
|
| 77 | + * @throws PhpfastcacheUnsupportedMethodException |
|
| 78 | + */ |
|
| 79 | + protected function driverWriteMultiple(ExtendedCacheItemInterface ...$item): bool |
|
| 80 | + { |
|
| 81 | + /** |
|
| 82 | + * @todo Implement bulk writes to be for v10: |
|
| 83 | + * For methods commit() and saveMultiple() |
|
| 84 | + */ |
|
| 85 | + throw new PhpfastcacheUnsupportedMethodException(); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * @param string $key |
|
| 91 | + * @param string $encodedKey |
|
| 92 | + * @return bool |
|
| 93 | + */ |
|
| 94 | + abstract protected function driverDelete(string $key, string $encodedKey): bool; |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @param string[] $keys |
|
| 98 | + * @return bool |
|
| 99 | + * @throws PhpfastcacheUnsupportedMethodException |
|
| 100 | + */ |
|
| 101 | + protected function driverDeleteMultiple(array $keys): bool |
|
| 102 | + { |
|
| 103 | + throw new PhpfastcacheUnsupportedMethodException(); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * @return bool |
|
| 108 | + */ |
|
| 109 | + abstract protected function driverClear(): bool; |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @param CacheItemInterface $item |
|
| 113 | + * @param string $expectedClassType |
|
| 114 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 115 | + */ |
|
| 116 | + protected function assertCacheItemType(CacheItemInterface $item, string $expectedClassType): void |
|
| 117 | + { |
|
| 118 | + if (!($item instanceof $expectedClassType)) { |
|
| 119 | + throw new PhpfastcacheInvalidArgumentException( |
|
| 120 | + \sprintf('Cross-driver type confusion detected: Expected "%s" object, got "%s"', $expectedClassType, $item::class) |
|
| 121 | + ); |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | 124 | } |
@@ -30,285 +30,285 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | trait IOHelperTrait |
| 32 | 32 | { |
| 33 | - use TaggableCacheItemPoolTrait; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @var array<string, string> |
|
| 37 | - */ |
|
| 38 | - public array $tmp = []; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Provide a generic getStats() method |
|
| 42 | - * for files-based drivers |
|
| 43 | - * @return DriverStatistic |
|
| 44 | - * @throws PhpfastcacheIOException |
|
| 45 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 46 | - */ |
|
| 47 | - public function getStats(): DriverStatistic |
|
| 48 | - { |
|
| 49 | - $stat = new DriverStatistic(); |
|
| 50 | - $path = $this->getFilePath(false); |
|
| 51 | - |
|
| 52 | - if (!is_dir($path)) { |
|
| 53 | - throw new PhpfastcacheIOException("Can't read PATH:" . $path); |
|
| 54 | - } |
|
| 55 | - $stat->setSize(Directory::dirSize($path)) |
|
| 56 | - ->setInfo('Number of files used to build the cache: ' . Directory::getFileCount($path)) |
|
| 57 | - ->setRawData( |
|
| 58 | - [ |
|
| 59 | - 'tmp' => $this->tmp, |
|
| 60 | - ] |
|
| 61 | - ); |
|
| 62 | - |
|
| 63 | - if ($this->getConfig()->isUseStaticItemCaching()) { |
|
| 64 | - $stat->setData(implode(', ', \array_keys($this->itemInstances))); |
|
| 65 | - } else { |
|
| 66 | - $stat->setData('No data available since static item caching option (useStaticItemCaching) is disabled.'); |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - return $stat; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * @param string|bool $keyword |
|
| 74 | - * @param bool $skip |
|
| 75 | - * @return string |
|
| 76 | - * @throws PhpfastcacheIOException |
|
| 77 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 78 | - */ |
|
| 79 | - protected function getFilePath(string|bool $keyword, bool $skip = false): string |
|
| 80 | - { |
|
| 81 | - $path = $this->getPath(); |
|
| 82 | - |
|
| 83 | - if ($keyword === false) { |
|
| 84 | - return $path; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - $filename = $this->encodeFilename($keyword); |
|
| 88 | - $folder = \substr($filename, 0, 2) . DIRECTORY_SEPARATOR . \substr($filename, 2, 2); |
|
| 89 | - $path = \rtrim($path, '/\\') . DIRECTORY_SEPARATOR . $folder; |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Skip Create Sub Folders; |
|
| 93 | - */ |
|
| 94 | - if (!$skip && !\is_dir($path) && @!\mkdir($path, $this->getDefaultChmod(), true) && !\is_dir($path)) { |
|
| 95 | - throw new PhpfastcacheIOException( |
|
| 96 | - 'Path "' . $path . '" is not writable, please set a chmod 0777 or any writable permission and make sure to make use of an absolute path !' |
|
| 97 | - ); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - return $path . \DIRECTORY_SEPARATOR . $filename . '.' . $this->getConfig()->getCacheFileExtension(); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @param bool $readonly |
|
| 105 | - * @return string |
|
| 106 | - * @throws PhpfastcacheIOException |
|
| 107 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 108 | - */ |
|
| 109 | - public function getPath(bool $readonly = false): string |
|
| 110 | - { |
|
| 111 | - $tmpDir = \rtrim(\ini_get('upload_tmp_dir') ?: \sys_get_temp_dir(), '\\/') . DIRECTORY_SEPARATOR . 'phpfastcache'; |
|
| 112 | - $httpHost = $this->getConfig()->getSuperGlobalAccessor()('SERVER', 'HTTP_HOST'); |
|
| 113 | - $securityKey = $this->buildSecurityKey($httpHost); |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Extends the temporary directory |
|
| 117 | - * with the security key and the driver name |
|
| 118 | - */ |
|
| 119 | - $tmpDir = \rtrim($tmpDir, '/') . DIRECTORY_SEPARATOR; |
|
| 120 | - |
|
| 121 | - if (empty($this->getConfig()->getPath())) { |
|
| 122 | - $path = $tmpDir; |
|
| 123 | - } else { |
|
| 124 | - $path = \rtrim($this->getConfig()->getPath(), '/') . DIRECTORY_SEPARATOR; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - $pathSuffix = $securityKey . DIRECTORY_SEPARATOR . $this->getDriverName(); |
|
| 128 | - $fullPath = Directory::getAbsolutePath($path . $pathSuffix); |
|
| 129 | - $fullPathTmp = Directory::getAbsolutePath($tmpDir . $pathSuffix); |
|
| 130 | - |
|
| 131 | - $this->mkdir($fullPath, $fullPathTmp); |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * In readonly mode we only attempt |
|
| 135 | - * to verify if the directory exists |
|
| 136 | - * or not, if it does not then we |
|
| 137 | - * return the temp dir |
|
| 138 | - */ |
|
| 139 | - if ($readonly) { |
|
| 140 | - if ($this->getConfig()->isAutoTmpFallback() && (!@\file_exists($fullPath) || !@\is_writable($fullPath))) { |
|
| 141 | - return $fullPathTmp; |
|
| 142 | - } |
|
| 143 | - return $fullPath; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - return realpath($fullPath); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - protected function buildSecurityKey(?string $httpHost): string |
|
| 150 | - { |
|
| 151 | - $securityKey = $this->getConfig()->getSecurityKey(); |
|
| 152 | - if (!$securityKey || \mb_strtolower($securityKey) === 'auto') { |
|
| 153 | - if (isset($httpHost)) { |
|
| 154 | - $securityKey = \preg_replace('/^www./', '', \strtolower(\str_replace(':', '_', $httpHost))); |
|
| 155 | - } else { |
|
| 156 | - $securityKey = (SapiDetector::isWebScript() ? 'web' : 'cli'); |
|
| 157 | - } |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - if (!empty($securityKey)) { |
|
| 161 | - $securityKey .= '/'; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - return static::cleanFileName($securityKey); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * @throws PhpfastcacheIOException |
|
| 169 | - */ |
|
| 170 | - protected function mkdir(string $fullPath, string $fullPathTmp): void |
|
| 171 | - { |
|
| 172 | - $fullPathHash = $this->getConfig()->getDefaultFileNameHashFunction()($fullPath); |
|
| 173 | - |
|
| 174 | - if (!isset($this->tmp[$fullPathHash]) || (!@\file_exists($fullPath) || !@\is_writable($fullPath))) { |
|
| 175 | - if (!@\file_exists($fullPath)) { |
|
| 176 | - if (@mkdir($fullPath, $this->getDefaultChmod(), true) === false && !\is_dir($fullPath)) { |
|
| 177 | - throw new PhpfastcacheIOException('The directory ' . $fullPath . ' could not be created.'); |
|
| 178 | - } |
|
| 179 | - } elseif (!@\is_writable($fullPath) && !@\chmod($fullPath, $this->getDefaultChmod()) && $this->getConfig()->isAutoTmpFallback()) { |
|
| 180 | - /** |
|
| 181 | - * Switch back to tmp dir |
|
| 182 | - * again if the path is not writable |
|
| 183 | - */ |
|
| 184 | - $fullPath = $fullPathTmp; |
|
| 185 | - if (!@\file_exists($fullPath) && @\mkdir($fullPath, $this->getDefaultChmod(), true) && !\is_dir($fullPath)) { |
|
| 186 | - throw new PhpfastcacheIOException('The directory ' . $fullPath . ' could not be created.'); |
|
| 187 | - } |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * In case there is no directory |
|
| 192 | - * writable including the temporary |
|
| 193 | - * one, we must throw an exception |
|
| 194 | - */ |
|
| 195 | - if (!@\file_exists($fullPath) || !@\is_writable($fullPath)) { |
|
| 196 | - throw new PhpfastcacheIOException( |
|
| 197 | - 'Path "' . $fullPath . '" is not writable, please set a chmod 0777 or any writable permission and make sure to make use of an absolute path !' |
|
| 198 | - ); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - $this->tmp[$fullPathHash] = $fullPath; |
|
| 202 | - } |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - /** |
|
| 206 | - * @param string $filename |
|
| 207 | - * @return string |
|
| 208 | - */ |
|
| 209 | - protected static function cleanFileName(string $filename): string |
|
| 210 | - { |
|
| 211 | - $regex = [ |
|
| 212 | - '/[\?\[\]\/\\\=\<\>\:\;\,\'\"\&\$\#\*\(\)\|\~\`\!\{\}]/', |
|
| 213 | - '/\.$/', |
|
| 214 | - '/^\./', |
|
| 215 | - ]; |
|
| 216 | - $replace = ['-', '', '']; |
|
| 217 | - |
|
| 218 | - return \trim(\preg_replace($regex, $replace, \trim($filename)), '-'); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * @return int |
|
| 223 | - */ |
|
| 224 | - protected function getDefaultChmod(): int |
|
| 225 | - { |
|
| 226 | - if (!$this->getConfig()->getDefaultChmod()) { |
|
| 227 | - return 0777; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - return $this->getConfig()->getDefaultChmod(); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * @param string $keyword |
|
| 235 | - * @return string |
|
| 236 | - */ |
|
| 237 | - protected function encodeFilename(string $keyword): string |
|
| 238 | - { |
|
| 239 | - return $this->getConfig()->getDefaultFileNameHashFunction()($keyword); |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * @param string $file |
|
| 244 | - * @return string |
|
| 245 | - * @throws PhpfastcacheIOException |
|
| 246 | - */ |
|
| 247 | - protected function readFile(string $file): string |
|
| 248 | - { |
|
| 249 | - if (!\is_readable($file)) { |
|
| 250 | - throw new PhpfastcacheIOException("Cannot read file located at: $file"); |
|
| 251 | - } |
|
| 252 | - if (\function_exists('file_get_contents')) { |
|
| 253 | - return (string)\file_get_contents($file); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - $string = ''; |
|
| 257 | - |
|
| 258 | - $fileHandle = @\fopen($file, 'rb'); |
|
| 259 | - while (!\feof($fileHandle)) { |
|
| 260 | - $line = \fgets($fileHandle); |
|
| 261 | - $string .= $line; |
|
| 262 | - } |
|
| 263 | - \fclose($fileHandle); |
|
| 264 | - |
|
| 265 | - return $string; |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /******************** |
|
| 33 | + use TaggableCacheItemPoolTrait; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @var array<string, string> |
|
| 37 | + */ |
|
| 38 | + public array $tmp = []; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Provide a generic getStats() method |
|
| 42 | + * for files-based drivers |
|
| 43 | + * @return DriverStatistic |
|
| 44 | + * @throws PhpfastcacheIOException |
|
| 45 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 46 | + */ |
|
| 47 | + public function getStats(): DriverStatistic |
|
| 48 | + { |
|
| 49 | + $stat = new DriverStatistic(); |
|
| 50 | + $path = $this->getFilePath(false); |
|
| 51 | + |
|
| 52 | + if (!is_dir($path)) { |
|
| 53 | + throw new PhpfastcacheIOException("Can't read PATH:" . $path); |
|
| 54 | + } |
|
| 55 | + $stat->setSize(Directory::dirSize($path)) |
|
| 56 | + ->setInfo('Number of files used to build the cache: ' . Directory::getFileCount($path)) |
|
| 57 | + ->setRawData( |
|
| 58 | + [ |
|
| 59 | + 'tmp' => $this->tmp, |
|
| 60 | + ] |
|
| 61 | + ); |
|
| 62 | + |
|
| 63 | + if ($this->getConfig()->isUseStaticItemCaching()) { |
|
| 64 | + $stat->setData(implode(', ', \array_keys($this->itemInstances))); |
|
| 65 | + } else { |
|
| 66 | + $stat->setData('No data available since static item caching option (useStaticItemCaching) is disabled.'); |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + return $stat; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * @param string|bool $keyword |
|
| 74 | + * @param bool $skip |
|
| 75 | + * @return string |
|
| 76 | + * @throws PhpfastcacheIOException |
|
| 77 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 78 | + */ |
|
| 79 | + protected function getFilePath(string|bool $keyword, bool $skip = false): string |
|
| 80 | + { |
|
| 81 | + $path = $this->getPath(); |
|
| 82 | + |
|
| 83 | + if ($keyword === false) { |
|
| 84 | + return $path; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + $filename = $this->encodeFilename($keyword); |
|
| 88 | + $folder = \substr($filename, 0, 2) . DIRECTORY_SEPARATOR . \substr($filename, 2, 2); |
|
| 89 | + $path = \rtrim($path, '/\\') . DIRECTORY_SEPARATOR . $folder; |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Skip Create Sub Folders; |
|
| 93 | + */ |
|
| 94 | + if (!$skip && !\is_dir($path) && @!\mkdir($path, $this->getDefaultChmod(), true) && !\is_dir($path)) { |
|
| 95 | + throw new PhpfastcacheIOException( |
|
| 96 | + 'Path "' . $path . '" is not writable, please set a chmod 0777 or any writable permission and make sure to make use of an absolute path !' |
|
| 97 | + ); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + return $path . \DIRECTORY_SEPARATOR . $filename . '.' . $this->getConfig()->getCacheFileExtension(); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @param bool $readonly |
|
| 105 | + * @return string |
|
| 106 | + * @throws PhpfastcacheIOException |
|
| 107 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 108 | + */ |
|
| 109 | + public function getPath(bool $readonly = false): string |
|
| 110 | + { |
|
| 111 | + $tmpDir = \rtrim(\ini_get('upload_tmp_dir') ?: \sys_get_temp_dir(), '\\/') . DIRECTORY_SEPARATOR . 'phpfastcache'; |
|
| 112 | + $httpHost = $this->getConfig()->getSuperGlobalAccessor()('SERVER', 'HTTP_HOST'); |
|
| 113 | + $securityKey = $this->buildSecurityKey($httpHost); |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Extends the temporary directory |
|
| 117 | + * with the security key and the driver name |
|
| 118 | + */ |
|
| 119 | + $tmpDir = \rtrim($tmpDir, '/') . DIRECTORY_SEPARATOR; |
|
| 120 | + |
|
| 121 | + if (empty($this->getConfig()->getPath())) { |
|
| 122 | + $path = $tmpDir; |
|
| 123 | + } else { |
|
| 124 | + $path = \rtrim($this->getConfig()->getPath(), '/') . DIRECTORY_SEPARATOR; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + $pathSuffix = $securityKey . DIRECTORY_SEPARATOR . $this->getDriverName(); |
|
| 128 | + $fullPath = Directory::getAbsolutePath($path . $pathSuffix); |
|
| 129 | + $fullPathTmp = Directory::getAbsolutePath($tmpDir . $pathSuffix); |
|
| 130 | + |
|
| 131 | + $this->mkdir($fullPath, $fullPathTmp); |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * In readonly mode we only attempt |
|
| 135 | + * to verify if the directory exists |
|
| 136 | + * or not, if it does not then we |
|
| 137 | + * return the temp dir |
|
| 138 | + */ |
|
| 139 | + if ($readonly) { |
|
| 140 | + if ($this->getConfig()->isAutoTmpFallback() && (!@\file_exists($fullPath) || !@\is_writable($fullPath))) { |
|
| 141 | + return $fullPathTmp; |
|
| 142 | + } |
|
| 143 | + return $fullPath; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + return realpath($fullPath); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + protected function buildSecurityKey(?string $httpHost): string |
|
| 150 | + { |
|
| 151 | + $securityKey = $this->getConfig()->getSecurityKey(); |
|
| 152 | + if (!$securityKey || \mb_strtolower($securityKey) === 'auto') { |
|
| 153 | + if (isset($httpHost)) { |
|
| 154 | + $securityKey = \preg_replace('/^www./', '', \strtolower(\str_replace(':', '_', $httpHost))); |
|
| 155 | + } else { |
|
| 156 | + $securityKey = (SapiDetector::isWebScript() ? 'web' : 'cli'); |
|
| 157 | + } |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + if (!empty($securityKey)) { |
|
| 161 | + $securityKey .= '/'; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + return static::cleanFileName($securityKey); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * @throws PhpfastcacheIOException |
|
| 169 | + */ |
|
| 170 | + protected function mkdir(string $fullPath, string $fullPathTmp): void |
|
| 171 | + { |
|
| 172 | + $fullPathHash = $this->getConfig()->getDefaultFileNameHashFunction()($fullPath); |
|
| 173 | + |
|
| 174 | + if (!isset($this->tmp[$fullPathHash]) || (!@\file_exists($fullPath) || !@\is_writable($fullPath))) { |
|
| 175 | + if (!@\file_exists($fullPath)) { |
|
| 176 | + if (@mkdir($fullPath, $this->getDefaultChmod(), true) === false && !\is_dir($fullPath)) { |
|
| 177 | + throw new PhpfastcacheIOException('The directory ' . $fullPath . ' could not be created.'); |
|
| 178 | + } |
|
| 179 | + } elseif (!@\is_writable($fullPath) && !@\chmod($fullPath, $this->getDefaultChmod()) && $this->getConfig()->isAutoTmpFallback()) { |
|
| 180 | + /** |
|
| 181 | + * Switch back to tmp dir |
|
| 182 | + * again if the path is not writable |
|
| 183 | + */ |
|
| 184 | + $fullPath = $fullPathTmp; |
|
| 185 | + if (!@\file_exists($fullPath) && @\mkdir($fullPath, $this->getDefaultChmod(), true) && !\is_dir($fullPath)) { |
|
| 186 | + throw new PhpfastcacheIOException('The directory ' . $fullPath . ' could not be created.'); |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * In case there is no directory |
|
| 192 | + * writable including the temporary |
|
| 193 | + * one, we must throw an exception |
|
| 194 | + */ |
|
| 195 | + if (!@\file_exists($fullPath) || !@\is_writable($fullPath)) { |
|
| 196 | + throw new PhpfastcacheIOException( |
|
| 197 | + 'Path "' . $fullPath . '" is not writable, please set a chmod 0777 or any writable permission and make sure to make use of an absolute path !' |
|
| 198 | + ); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + $this->tmp[$fullPathHash] = $fullPath; |
|
| 202 | + } |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + /** |
|
| 206 | + * @param string $filename |
|
| 207 | + * @return string |
|
| 208 | + */ |
|
| 209 | + protected static function cleanFileName(string $filename): string |
|
| 210 | + { |
|
| 211 | + $regex = [ |
|
| 212 | + '/[\?\[\]\/\\\=\<\>\:\;\,\'\"\&\$\#\*\(\)\|\~\`\!\{\}]/', |
|
| 213 | + '/\.$/', |
|
| 214 | + '/^\./', |
|
| 215 | + ]; |
|
| 216 | + $replace = ['-', '', '']; |
|
| 217 | + |
|
| 218 | + return \trim(\preg_replace($regex, $replace, \trim($filename)), '-'); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * @return int |
|
| 223 | + */ |
|
| 224 | + protected function getDefaultChmod(): int |
|
| 225 | + { |
|
| 226 | + if (!$this->getConfig()->getDefaultChmod()) { |
|
| 227 | + return 0777; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + return $this->getConfig()->getDefaultChmod(); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * @param string $keyword |
|
| 235 | + * @return string |
|
| 236 | + */ |
|
| 237 | + protected function encodeFilename(string $keyword): string |
|
| 238 | + { |
|
| 239 | + return $this->getConfig()->getDefaultFileNameHashFunction()($keyword); |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * @param string $file |
|
| 244 | + * @return string |
|
| 245 | + * @throws PhpfastcacheIOException |
|
| 246 | + */ |
|
| 247 | + protected function readFile(string $file): string |
|
| 248 | + { |
|
| 249 | + if (!\is_readable($file)) { |
|
| 250 | + throw new PhpfastcacheIOException("Cannot read file located at: $file"); |
|
| 251 | + } |
|
| 252 | + if (\function_exists('file_get_contents')) { |
|
| 253 | + return (string)\file_get_contents($file); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + $string = ''; |
|
| 257 | + |
|
| 258 | + $fileHandle = @\fopen($file, 'rb'); |
|
| 259 | + while (!\feof($fileHandle)) { |
|
| 260 | + $line = \fgets($fileHandle); |
|
| 261 | + $string .= $line; |
|
| 262 | + } |
|
| 263 | + \fclose($fileHandle); |
|
| 264 | + |
|
| 265 | + return $string; |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /******************** |
|
| 269 | 269 | * |
| 270 | 270 | * PSR-6 Extended Methods |
| 271 | 271 | * |
| 272 | 272 | *******************/ |
| 273 | 273 | |
| 274 | - /** |
|
| 275 | - * @param string $file |
|
| 276 | - * @param string $data |
|
| 277 | - * @param bool $secureFileManipulation |
|
| 278 | - * @return bool |
|
| 279 | - * @throws PhpfastcacheIOException |
|
| 280 | - * @throws \Exception |
|
| 281 | - */ |
|
| 282 | - protected function writeFile(string $file, string $data, bool $secureFileManipulation = false): bool |
|
| 283 | - { |
|
| 284 | - $this->eventManager->dispatch(Event::CACHE_WRITE_FILE_ON_DISK, $this, $file, $secureFileManipulation); |
|
| 285 | - |
|
| 286 | - if ($secureFileManipulation) { |
|
| 287 | - $tmpFilename = Directory::getAbsolutePath( |
|
| 288 | - dirname($file) . \DIRECTORY_SEPARATOR . 'tmp_' . $this->getConfig()->getDefaultFileNameHashFunction()( |
|
| 289 | - \bin2hex(\random_bytes(16)) |
|
| 290 | - ) |
|
| 291 | - ) . '.' . $this->getConfig()->getCacheFileExtension() . \random_int(1000, 9999); |
|
| 292 | - |
|
| 293 | - $handle = \fopen($tmpFilename, 'w+b'); |
|
| 294 | - if (\is_resource($handle)) { |
|
| 295 | - \flock($handle, \LOCK_EX); |
|
| 296 | - $octetWritten = fwrite($handle, $data); |
|
| 297 | - \flock($handle, \LOCK_UN); |
|
| 298 | - \fclose($handle); |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - if (!\rename($tmpFilename, $file)) { |
|
| 302 | - throw new PhpfastcacheIOException(\sprintf('Failed to rename %s to %s', $tmpFilename, $file)); |
|
| 303 | - } |
|
| 304 | - } else { |
|
| 305 | - $handle = \fopen($file, 'w+b'); |
|
| 306 | - if (\is_resource($handle)) { |
|
| 307 | - $octetWritten = \fwrite($handle, $data); |
|
| 308 | - \fclose($handle); |
|
| 309 | - } |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - return (bool)($octetWritten ?? false); |
|
| 313 | - } |
|
| 274 | + /** |
|
| 275 | + * @param string $file |
|
| 276 | + * @param string $data |
|
| 277 | + * @param bool $secureFileManipulation |
|
| 278 | + * @return bool |
|
| 279 | + * @throws PhpfastcacheIOException |
|
| 280 | + * @throws \Exception |
|
| 281 | + */ |
|
| 282 | + protected function writeFile(string $file, string $data, bool $secureFileManipulation = false): bool |
|
| 283 | + { |
|
| 284 | + $this->eventManager->dispatch(Event::CACHE_WRITE_FILE_ON_DISK, $this, $file, $secureFileManipulation); |
|
| 285 | + |
|
| 286 | + if ($secureFileManipulation) { |
|
| 287 | + $tmpFilename = Directory::getAbsolutePath( |
|
| 288 | + dirname($file) . \DIRECTORY_SEPARATOR . 'tmp_' . $this->getConfig()->getDefaultFileNameHashFunction()( |
|
| 289 | + \bin2hex(\random_bytes(16)) |
|
| 290 | + ) |
|
| 291 | + ) . '.' . $this->getConfig()->getCacheFileExtension() . \random_int(1000, 9999); |
|
| 292 | + |
|
| 293 | + $handle = \fopen($tmpFilename, 'w+b'); |
|
| 294 | + if (\is_resource($handle)) { |
|
| 295 | + \flock($handle, \LOCK_EX); |
|
| 296 | + $octetWritten = fwrite($handle, $data); |
|
| 297 | + \flock($handle, \LOCK_UN); |
|
| 298 | + \fclose($handle); |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + if (!\rename($tmpFilename, $file)) { |
|
| 302 | + throw new PhpfastcacheIOException(\sprintf('Failed to rename %s to %s', $tmpFilename, $file)); |
|
| 303 | + } |
|
| 304 | + } else { |
|
| 305 | + $handle = \fopen($file, 'w+b'); |
|
| 306 | + if (\is_resource($handle)) { |
|
| 307 | + $octetWritten = \fwrite($handle, $data); |
|
| 308 | + \fclose($handle); |
|
| 309 | + } |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + return (bool)($octetWritten ?? false); |
|
| 313 | + } |
|
| 314 | 314 | } |
@@ -76,7 +76,7 @@ discard block |
||
| 76 | 76 | * @throws PhpfastcacheIOException |
| 77 | 77 | * @throws PhpfastcacheInvalidArgumentException |
| 78 | 78 | */ |
| 79 | - protected function getFilePath(string|bool $keyword, bool $skip = false): string |
|
| 79 | + protected function getFilePath(string | bool $keyword, bool $skip = false): string |
|
| 80 | 80 | { |
| 81 | 81 | $path = $this->getPath(); |
| 82 | 82 | |
@@ -250,7 +250,7 @@ discard block |
||
| 250 | 250 | throw new PhpfastcacheIOException("Cannot read file located at: $file"); |
| 251 | 251 | } |
| 252 | 252 | if (\function_exists('file_get_contents')) { |
| 253 | - return (string)\file_get_contents($file); |
|
| 253 | + return (string) \file_get_contents($file); |
|
| 254 | 254 | } |
| 255 | 255 | |
| 256 | 256 | $string = ''; |
@@ -309,6 +309,6 @@ discard block |
||
| 309 | 309 | } |
| 310 | 310 | } |
| 311 | 311 | |
| 312 | - return (bool)($octetWritten ?? false); |
|
| 312 | + return (bool) ($octetWritten ?? false); |
|
| 313 | 313 | } |
| 314 | 314 | } |
@@ -62,7 +62,8 @@ discard block |
||
| 62 | 62 | |
| 63 | 63 | if ($this->getConfig()->isUseStaticItemCaching()) { |
| 64 | 64 | $stat->setData(implode(', ', \array_keys($this->itemInstances))); |
| 65 | - } else { |
|
| 65 | + } |
|
| 66 | + else { |
|
| 66 | 67 | $stat->setData('No data available since static item caching option (useStaticItemCaching) is disabled.'); |
| 67 | 68 | } |
| 68 | 69 | |
@@ -120,7 +121,8 @@ discard block |
||
| 120 | 121 | |
| 121 | 122 | if (empty($this->getConfig()->getPath())) { |
| 122 | 123 | $path = $tmpDir; |
| 123 | - } else { |
|
| 124 | + } |
|
| 125 | + else { |
|
| 124 | 126 | $path = \rtrim($this->getConfig()->getPath(), '/') . DIRECTORY_SEPARATOR; |
| 125 | 127 | } |
| 126 | 128 | |
@@ -152,7 +154,8 @@ discard block |
||
| 152 | 154 | if (!$securityKey || \mb_strtolower($securityKey) === 'auto') { |
| 153 | 155 | if (isset($httpHost)) { |
| 154 | 156 | $securityKey = \preg_replace('/^www./', '', \strtolower(\str_replace(':', '_', $httpHost))); |
| 155 | - } else { |
|
| 157 | + } |
|
| 158 | + else { |
|
| 156 | 159 | $securityKey = (SapiDetector::isWebScript() ? 'web' : 'cli'); |
| 157 | 160 | } |
| 158 | 161 | } |
@@ -176,7 +179,8 @@ discard block |
||
| 176 | 179 | if (@mkdir($fullPath, $this->getDefaultChmod(), true) === false && !\is_dir($fullPath)) { |
| 177 | 180 | throw new PhpfastcacheIOException('The directory ' . $fullPath . ' could not be created.'); |
| 178 | 181 | } |
| 179 | - } elseif (!@\is_writable($fullPath) && !@\chmod($fullPath, $this->getDefaultChmod()) && $this->getConfig()->isAutoTmpFallback()) { |
|
| 182 | + } |
|
| 183 | + elseif (!@\is_writable($fullPath) && !@\chmod($fullPath, $this->getDefaultChmod()) && $this->getConfig()->isAutoTmpFallback()) { |
|
| 180 | 184 | /** |
| 181 | 185 | * Switch back to tmp dir |
| 182 | 186 | * again if the path is not writable |
@@ -301,7 +305,8 @@ discard block |
||
| 301 | 305 | if (!\rename($tmpFilename, $file)) { |
| 302 | 306 | throw new PhpfastcacheIOException(\sprintf('Failed to rename %s to %s', $tmpFilename, $file)); |
| 303 | 307 | } |
| 304 | - } else { |
|
| 308 | + } |
|
| 309 | + else { |
|
| 305 | 310 | $handle = \fopen($file, 'w+b'); |
| 306 | 311 | if (\is_resource($handle)) { |
| 307 | 312 | $octetWritten = \fwrite($handle, $data); |
@@ -36,196 +36,196 @@ |
||
| 36 | 36 | */ |
| 37 | 37 | interface ExtendedCacheItemPoolInterface extends CacheItemPoolInterface, EventManagerDispatcherInterface, ClassNamespaceResolverInterface, TaggableCacheItemPoolInterface |
| 38 | 38 | { |
| 39 | - public const DRIVER_CHECK_FAILURE = '%s is not installed or is misconfigured, cannot continue. |
|
| 39 | + public const DRIVER_CHECK_FAILURE = '%s is not installed or is misconfigured, cannot continue. |
|
| 40 | 40 | Also, please verify the suggested dependencies in composer because as of the V6, 3rd party libraries are no longer required.%s'; |
| 41 | 41 | |
| 42 | - public const DRIVER_CONNECT_FAILURE = '%s failed to connect with the following error message: "%s" line %d in %s.'; |
|
| 43 | - |
|
| 44 | - public const DRIVER_KEY_WRAPPER_INDEX = 'k'; |
|
| 45 | - |
|
| 46 | - public const DRIVER_DATA_WRAPPER_INDEX = 'd'; |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Expiration date Index |
|
| 50 | - */ |
|
| 51 | - public const DRIVER_EDATE_WRAPPER_INDEX = 'e'; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Creation date Index |
|
| 55 | - */ |
|
| 56 | - public const DRIVER_CDATE_WRAPPER_INDEX = 'c'; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Modification date Index |
|
| 60 | - */ |
|
| 61 | - public const DRIVER_MDATE_WRAPPER_INDEX = 'm'; |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Hard-limit count of items returns by getAllItems() |
|
| 65 | - */ |
|
| 66 | - public const MAX_ALL_KEYS_COUNT = 9999; |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Return the config class name |
|
| 70 | - * @return string |
|
| 71 | - */ |
|
| 72 | - public static function getConfigClass(): string; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Return the item class name |
|
| 76 | - * @return string |
|
| 77 | - */ |
|
| 78 | - public static function getItemClass(): string; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @param string $key |
|
| 82 | - * @return string |
|
| 83 | - */ |
|
| 84 | - public function getEncodedKey(string $key): string; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * @return ConfigurationOptionInterface |
|
| 88 | - */ |
|
| 89 | - public function getConfig(): ConfigurationOptionInterface; |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * @return ConfigurationOptionInterface |
|
| 93 | - */ |
|
| 94 | - public function getDefaultConfig(): ConfigurationOptionInterface; |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * @return string |
|
| 98 | - */ |
|
| 99 | - public function getDriverName(): string; |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @return mixed |
|
| 103 | - */ |
|
| 104 | - public function getInstanceId(): string; |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * [Phpfastcache phpDoc Override] |
|
| 108 | - * Returns a Cache Item representing the specified key. |
|
| 109 | - * |
|
| 110 | - * This method must always return a CacheItemInterface object, even in case of |
|
| 111 | - * a cache miss. It MUST NOT return null. |
|
| 112 | - * |
|
| 113 | - * @param string $key |
|
| 114 | - * The key for which to return the corresponding Cache Item. |
|
| 115 | - * |
|
| 116 | - * @return ExtendedCacheItemInterface |
|
| 117 | - * The corresponding Cache Item. |
|
| 118 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 119 | - * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
| 120 | - * MUST be thrown. |
|
| 121 | - * |
|
| 122 | - */ |
|
| 123 | - public function getItem(string $key): ExtendedCacheItemInterface; |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * [Phpfastcache phpDoc Override] |
|
| 127 | - * Returns a traversable set of cache items. |
|
| 128 | - * |
|
| 129 | - * @param string[] $keys |
|
| 130 | - * An indexed array of keys of items to retrieve. |
|
| 131 | - * |
|
| 132 | - * @return iterable<ExtendedCacheItemInterface> |
|
| 133 | - * A traversable collection of Cache Items keyed by the cache keys of |
|
| 134 | - * each item. A Cache item will be returned for each key, even if that |
|
| 135 | - * key is not found. However, if no keys are specified then an empty |
|
| 136 | - * traversable MUST be returned instead. |
|
| 137 | - * @throws InvalidArgumentException |
|
| 138 | - * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
| 139 | - * MUST be thrown. |
|
| 140 | - * |
|
| 141 | - */ |
|
| 142 | - public function getItems(array $keys = []): iterable; |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * Returns the WHOLE cache as a traversable set of cache items. |
|
| 146 | - * A hard-limit of 9999 items is defined internally to prevent |
|
| 147 | - * serious performances issues of your application. |
|
| 148 | - * @see ExtendedCacheItemPoolInterface::MAX_ALL_KEYS_COUNT |
|
| 149 | - * |
|
| 150 | - * @param string $pattern |
|
| 151 | - * An optional pattern supported by a limited range of drivers. |
|
| 152 | - * If this parameter is unsupported by the driver, a PhpfastcacheInvalidArgumentException will be thrown. |
|
| 153 | - * |
|
| 154 | - * @return iterable<ExtendedCacheItemInterface> |
|
| 155 | - * A traversable collection of Cache Items keyed by the cache keys of |
|
| 156 | - * each item. However, if no keys are returned by the backend then an empty |
|
| 157 | - * traversable WILL be returned instead. |
|
| 158 | - * |
|
| 159 | - * @throws PhpfastcacheInvalidArgumentException If the driver does not support the $pattern argument |
|
| 160 | - * @throws PhpfastcacheUnsupportedMethodException If the driver does not permit to list all the keys through this implementation. |
|
| 161 | - */ |
|
| 162 | - public function getAllItems(string $pattern = ''): iterable; |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Returns A json string that represents an array of items. |
|
| 166 | - * |
|
| 167 | - * @param array<string> $keys An indexed array of keys of items to retrieve. |
|
| 168 | - * @param int $options \json_encode() options |
|
| 169 | - * @param int $depth \json_encode() depth |
|
| 170 | - * |
|
| 171 | - * @return string |
|
| 172 | - * @throws InvalidArgumentException |
|
| 173 | - * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
| 174 | - * MUST be thrown. |
|
| 175 | - * |
|
| 176 | - */ |
|
| 177 | - public function getItemsAsJsonString(array $keys = [], int $options = \JSON_THROW_ON_ERROR, int $depth = 512): string; |
|
| 178 | - |
|
| 179 | - public function setItem(CacheItemInterface $item): static; |
|
| 180 | - |
|
| 181 | - public function getStats(): DriverStatistic; |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Get a quick help guide |
|
| 185 | - * about the current driver |
|
| 186 | - */ |
|
| 187 | - public function getHelp(): string; |
|
| 188 | - |
|
| 189 | - public function detachItem(CacheItemInterface $item): static; |
|
| 190 | - |
|
| 191 | - public function detachAllItems(): static; |
|
| 192 | - |
|
| 193 | - public function attachItem(CacheItemInterface $item): static; |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Returns true if the item exists, is attached and the Spl Hash matches |
|
| 197 | - * Returns false if the item exists, is attached and the Spl Hash mismatches |
|
| 198 | - * Returns null if the item does not exist |
|
| 199 | - * |
|
| 200 | - * @param CacheItemInterface $item |
|
| 201 | - * @return bool |
|
| 202 | - * @throws PhpfastcacheLogicException |
|
| 203 | - */ |
|
| 204 | - public function isAttached(CacheItemInterface $item): bool; |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Persists a cache item immediately. |
|
| 208 | - * |
|
| 209 | - * @param ExtendedCacheItemInterface|CacheItemInterface $item |
|
| 210 | - * The cache item to save. |
|
| 211 | - * |
|
| 212 | - * @return bool |
|
| 213 | - * True if the item was successfully persisted. False if there was an error. |
|
| 214 | - */ |
|
| 215 | - public function save(ExtendedCacheItemInterface|CacheItemInterface $item): bool; |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Save multiple items, possible uses: |
|
| 219 | - * saveMultiple([$item1, $item2, $item3]); |
|
| 220 | - * saveMultiple($item1, $item2, $item3); |
|
| 221 | - * |
|
| 222 | - * @param ExtendedCacheItemInterface[] $items |
|
| 223 | - * @return bool |
|
| 224 | - */ |
|
| 225 | - public function saveMultiple(ExtendedCacheItemInterface ...$items): bool; |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * @return DriverIO |
|
| 229 | - */ |
|
| 230 | - public function getIO(): DriverIO; |
|
| 42 | + public const DRIVER_CONNECT_FAILURE = '%s failed to connect with the following error message: "%s" line %d in %s.'; |
|
| 43 | + |
|
| 44 | + public const DRIVER_KEY_WRAPPER_INDEX = 'k'; |
|
| 45 | + |
|
| 46 | + public const DRIVER_DATA_WRAPPER_INDEX = 'd'; |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Expiration date Index |
|
| 50 | + */ |
|
| 51 | + public const DRIVER_EDATE_WRAPPER_INDEX = 'e'; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Creation date Index |
|
| 55 | + */ |
|
| 56 | + public const DRIVER_CDATE_WRAPPER_INDEX = 'c'; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Modification date Index |
|
| 60 | + */ |
|
| 61 | + public const DRIVER_MDATE_WRAPPER_INDEX = 'm'; |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Hard-limit count of items returns by getAllItems() |
|
| 65 | + */ |
|
| 66 | + public const MAX_ALL_KEYS_COUNT = 9999; |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Return the config class name |
|
| 70 | + * @return string |
|
| 71 | + */ |
|
| 72 | + public static function getConfigClass(): string; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Return the item class name |
|
| 76 | + * @return string |
|
| 77 | + */ |
|
| 78 | + public static function getItemClass(): string; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @param string $key |
|
| 82 | + * @return string |
|
| 83 | + */ |
|
| 84 | + public function getEncodedKey(string $key): string; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * @return ConfigurationOptionInterface |
|
| 88 | + */ |
|
| 89 | + public function getConfig(): ConfigurationOptionInterface; |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * @return ConfigurationOptionInterface |
|
| 93 | + */ |
|
| 94 | + public function getDefaultConfig(): ConfigurationOptionInterface; |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * @return string |
|
| 98 | + */ |
|
| 99 | + public function getDriverName(): string; |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @return mixed |
|
| 103 | + */ |
|
| 104 | + public function getInstanceId(): string; |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * [Phpfastcache phpDoc Override] |
|
| 108 | + * Returns a Cache Item representing the specified key. |
|
| 109 | + * |
|
| 110 | + * This method must always return a CacheItemInterface object, even in case of |
|
| 111 | + * a cache miss. It MUST NOT return null. |
|
| 112 | + * |
|
| 113 | + * @param string $key |
|
| 114 | + * The key for which to return the corresponding Cache Item. |
|
| 115 | + * |
|
| 116 | + * @return ExtendedCacheItemInterface |
|
| 117 | + * The corresponding Cache Item. |
|
| 118 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 119 | + * If the $key string is not a legal value a phpfastcacheInvalidArgumentException |
|
| 120 | + * MUST be thrown. |
|
| 121 | + * |
|
| 122 | + */ |
|
| 123 | + public function getItem(string $key): ExtendedCacheItemInterface; |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * [Phpfastcache phpDoc Override] |
|
| 127 | + * Returns a traversable set of cache items. |
|
| 128 | + * |
|
| 129 | + * @param string[] $keys |
|
| 130 | + * An indexed array of keys of items to retrieve. |
|
| 131 | + * |
|
| 132 | + * @return iterable<ExtendedCacheItemInterface> |
|
| 133 | + * A traversable collection of Cache Items keyed by the cache keys of |
|
| 134 | + * each item. A Cache item will be returned for each key, even if that |
|
| 135 | + * key is not found. However, if no keys are specified then an empty |
|
| 136 | + * traversable MUST be returned instead. |
|
| 137 | + * @throws InvalidArgumentException |
|
| 138 | + * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
| 139 | + * MUST be thrown. |
|
| 140 | + * |
|
| 141 | + */ |
|
| 142 | + public function getItems(array $keys = []): iterable; |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * Returns the WHOLE cache as a traversable set of cache items. |
|
| 146 | + * A hard-limit of 9999 items is defined internally to prevent |
|
| 147 | + * serious performances issues of your application. |
|
| 148 | + * @see ExtendedCacheItemPoolInterface::MAX_ALL_KEYS_COUNT |
|
| 149 | + * |
|
| 150 | + * @param string $pattern |
|
| 151 | + * An optional pattern supported by a limited range of drivers. |
|
| 152 | + * If this parameter is unsupported by the driver, a PhpfastcacheInvalidArgumentException will be thrown. |
|
| 153 | + * |
|
| 154 | + * @return iterable<ExtendedCacheItemInterface> |
|
| 155 | + * A traversable collection of Cache Items keyed by the cache keys of |
|
| 156 | + * each item. However, if no keys are returned by the backend then an empty |
|
| 157 | + * traversable WILL be returned instead. |
|
| 158 | + * |
|
| 159 | + * @throws PhpfastcacheInvalidArgumentException If the driver does not support the $pattern argument |
|
| 160 | + * @throws PhpfastcacheUnsupportedMethodException If the driver does not permit to list all the keys through this implementation. |
|
| 161 | + */ |
|
| 162 | + public function getAllItems(string $pattern = ''): iterable; |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Returns A json string that represents an array of items. |
|
| 166 | + * |
|
| 167 | + * @param array<string> $keys An indexed array of keys of items to retrieve. |
|
| 168 | + * @param int $options \json_encode() options |
|
| 169 | + * @param int $depth \json_encode() depth |
|
| 170 | + * |
|
| 171 | + * @return string |
|
| 172 | + * @throws InvalidArgumentException |
|
| 173 | + * If any of the keys in $keys are not a legal value a phpfastcacheInvalidArgumentException |
|
| 174 | + * MUST be thrown. |
|
| 175 | + * |
|
| 176 | + */ |
|
| 177 | + public function getItemsAsJsonString(array $keys = [], int $options = \JSON_THROW_ON_ERROR, int $depth = 512): string; |
|
| 178 | + |
|
| 179 | + public function setItem(CacheItemInterface $item): static; |
|
| 180 | + |
|
| 181 | + public function getStats(): DriverStatistic; |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Get a quick help guide |
|
| 185 | + * about the current driver |
|
| 186 | + */ |
|
| 187 | + public function getHelp(): string; |
|
| 188 | + |
|
| 189 | + public function detachItem(CacheItemInterface $item): static; |
|
| 190 | + |
|
| 191 | + public function detachAllItems(): static; |
|
| 192 | + |
|
| 193 | + public function attachItem(CacheItemInterface $item): static; |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Returns true if the item exists, is attached and the Spl Hash matches |
|
| 197 | + * Returns false if the item exists, is attached and the Spl Hash mismatches |
|
| 198 | + * Returns null if the item does not exist |
|
| 199 | + * |
|
| 200 | + * @param CacheItemInterface $item |
|
| 201 | + * @return bool |
|
| 202 | + * @throws PhpfastcacheLogicException |
|
| 203 | + */ |
|
| 204 | + public function isAttached(CacheItemInterface $item): bool; |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Persists a cache item immediately. |
|
| 208 | + * |
|
| 209 | + * @param ExtendedCacheItemInterface|CacheItemInterface $item |
|
| 210 | + * The cache item to save. |
|
| 211 | + * |
|
| 212 | + * @return bool |
|
| 213 | + * True if the item was successfully persisted. False if there was an error. |
|
| 214 | + */ |
|
| 215 | + public function save(ExtendedCacheItemInterface|CacheItemInterface $item): bool; |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Save multiple items, possible uses: |
|
| 219 | + * saveMultiple([$item1, $item2, $item3]); |
|
| 220 | + * saveMultiple($item1, $item2, $item3); |
|
| 221 | + * |
|
| 222 | + * @param ExtendedCacheItemInterface[] $items |
|
| 223 | + * @return bool |
|
| 224 | + */ |
|
| 225 | + public function saveMultiple(ExtendedCacheItemInterface ...$items): bool; |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * @return DriverIO |
|
| 229 | + */ |
|
| 230 | + public function getIO(): DriverIO; |
|
| 231 | 231 | } |
@@ -212,7 +212,7 @@ |
||
| 212 | 212 | * @return bool |
| 213 | 213 | * True if the item was successfully persisted. False if there was an error. |
| 214 | 214 | */ |
| 215 | - public function save(ExtendedCacheItemInterface|CacheItemInterface $item): bool; |
|
| 215 | + public function save(ExtendedCacheItemInterface | CacheItemInterface $item): bool; |
|
| 216 | 216 | |
| 217 | 217 | /** |
| 218 | 218 | * Save multiple items, possible uses: |
@@ -30,442 +30,442 @@ |
||
| 30 | 30 | */ |
| 31 | 31 | trait TaggableCacheItemPoolTrait |
| 32 | 32 | { |
| 33 | - use ExtendedCacheItemPoolTrait; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @inheritDoc |
|
| 37 | - * @throws PhpfastcacheCoreException |
|
| 38 | - * @throws PhpfastcacheDriverException |
|
| 39 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 40 | - * @throws PhpfastcacheLogicException |
|
| 41 | - */ |
|
| 42 | - public function getItemsByTagsAsJsonString( |
|
| 43 | - array $tagNames, |
|
| 44 | - int $option = \JSON_THROW_ON_ERROR, |
|
| 45 | - int $depth = 512, |
|
| 46 | - int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE |
|
| 47 | - ): string { |
|
| 48 | - return \json_encode( |
|
| 49 | - \array_map( |
|
| 50 | - static fn(CacheItemInterface $item) => $item->get(), |
|
| 51 | - \array_values($this->getItemsByTags($tagNames, $strategy)) |
|
| 52 | - ), |
|
| 53 | - $option, |
|
| 54 | - $depth, |
|
| 55 | - ); |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @inheritDoc |
|
| 60 | - * @throws PhpfastcacheCoreException |
|
| 61 | - * @throws PhpfastcacheDriverException |
|
| 62 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 63 | - * @throws PhpfastcacheLogicException |
|
| 64 | - */ |
|
| 65 | - public function getItemsByTags(array $tagNames, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): array |
|
| 66 | - { |
|
| 67 | - $items = []; |
|
| 68 | - foreach (\array_unique($tagNames) as $tagName) { |
|
| 69 | - $items[] = $this->fetchItemsByTagFromBackend($tagName); |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - $items = \array_merge([], ...$items); |
|
| 73 | - |
|
| 74 | - switch ($strategy) { |
|
| 75 | - case TaggableCacheItemPoolInterface::TAG_STRATEGY_ALL: |
|
| 76 | - foreach ($items as $key => $item) { |
|
| 77 | - if (\array_diff($tagNames, $item->getTags())) { |
|
| 78 | - unset($items[$key]); |
|
| 79 | - } |
|
| 80 | - } |
|
| 81 | - break; |
|
| 82 | - |
|
| 83 | - case TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY: |
|
| 84 | - foreach ($items as $key => $item) { |
|
| 85 | - if (\array_diff($tagNames, $item->getTags()) || \array_diff($item->getTags(), $tagNames)) { |
|
| 86 | - unset($items[$key]); |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - break; |
|
| 90 | - } |
|
| 91 | - return $items; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * @param string $tagName |
|
| 96 | - * @return ExtendedCacheItemInterface[] |
|
| 97 | - * @throws PhpfastcacheCoreException |
|
| 98 | - * @throws PhpfastcacheDriverException |
|
| 99 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 100 | - * @throws PhpfastcacheLogicException |
|
| 101 | - */ |
|
| 102 | - protected function fetchItemsByTagFromBackend(string $tagName): array |
|
| 103 | - { |
|
| 104 | - $driverResponse = $this->getItem($this->getTagKey($tagName)); |
|
| 105 | - |
|
| 106 | - if ($driverResponse->isHit()) { |
|
| 107 | - $tagsItems = (array)$driverResponse->get(); |
|
| 108 | - /** |
|
| 109 | - * getItems() may provide expired item(s) |
|
| 110 | - * themselves provided by a cache of item |
|
| 111 | - * keys based stored the tag item. |
|
| 112 | - * Therefore, we pass a filter callback |
|
| 113 | - * to remove the expired Item(s) provided by |
|
| 114 | - * the item keys passed through getItems() |
|
| 115 | - * |
|
| 116 | - * #headache |
|
| 117 | - */ |
|
| 118 | - return \array_filter( |
|
| 119 | - $this->getItems(\array_unique(\array_keys($tagsItems))), |
|
| 120 | - static fn (ExtendedCacheItemInterface $item) => $item->isHit(), |
|
| 121 | - ); |
|
| 122 | - } |
|
| 123 | - return []; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * @param string $key |
|
| 128 | - * @return string |
|
| 129 | - */ |
|
| 130 | - protected function getTagKey(string $key): string |
|
| 131 | - { |
|
| 132 | - return TaggableCacheItemPoolInterface::DRIVER_TAGS_KEY_PREFIX . $key; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * @inheritDoc |
|
| 137 | - * @throws PhpfastcacheCoreException |
|
| 138 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 139 | - * @throws PhpfastcacheLogicException |
|
| 140 | - * @throws PhpfastcacheDriverException |
|
| 141 | - */ |
|
| 142 | - public function deleteItemsByTags(array $tagNames, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 143 | - { |
|
| 144 | - $return = true; |
|
| 145 | - |
|
| 146 | - foreach ($this->getItemsByTags($tagNames, $strategy) as $item) { |
|
| 147 | - $result = $this->deleteItem($item->getKey()); |
|
| 148 | - if ($result !== true) { |
|
| 149 | - $return = $result; |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - return $return; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * @inheritDoc |
|
| 158 | - * @throws PhpfastcacheCoreException |
|
| 159 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 160 | - * @throws PhpfastcacheLogicException |
|
| 161 | - * @throws PhpfastcacheDriverException |
|
| 162 | - */ |
|
| 163 | - public function deleteItemsByTag(string $tagName, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 164 | - { |
|
| 165 | - $return = true; |
|
| 166 | - foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 167 | - $result = $this->deleteItem($item->getKey()); |
|
| 168 | - if ($result !== true) { |
|
| 169 | - $return = $result; |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - return $return; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * @inheritDoc |
|
| 178 | - * @throws PhpfastcacheCoreException |
|
| 179 | - * @throws PhpfastcacheDriverException |
|
| 180 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 181 | - * @throws PhpfastcacheLogicException |
|
| 182 | - */ |
|
| 183 | - public function getItemsByTag(string $tagName, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): array |
|
| 184 | - { |
|
| 185 | - $items = $this->fetchItemsByTagFromBackend($tagName); |
|
| 186 | - |
|
| 187 | - if ($strategy === TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY) { |
|
| 188 | - foreach ($items as $key => $item) { |
|
| 189 | - if (\array_diff($item->getTags(), [$tagName])) { |
|
| 190 | - unset($items[$key]); |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - } |
|
| 194 | - return $items; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * @inheritDoc |
|
| 199 | - * @throws PhpfastcacheCoreException |
|
| 200 | - * @throws PhpfastcacheDriverException |
|
| 201 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 202 | - * @throws PhpfastcacheLogicException |
|
| 203 | - * @throws \ReflectionException |
|
| 204 | - */ |
|
| 205 | - public function incrementItemsByTags(array $tagNames, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 206 | - { |
|
| 207 | - $return = true; |
|
| 208 | - foreach ($tagNames as $tagName) { |
|
| 209 | - $result = $this->incrementItemsByTag($tagName, $step, $strategy); |
|
| 210 | - if ($result !== true) { |
|
| 211 | - $return = $result; |
|
| 212 | - } |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - return $return; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * @inheritDoc |
|
| 220 | - * @throws PhpfastcacheCoreException |
|
| 221 | - * @throws PhpfastcacheDriverException |
|
| 222 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 223 | - * @throws PhpfastcacheLogicException |
|
| 224 | - * @throws \ReflectionException |
|
| 225 | - */ |
|
| 226 | - public function incrementItemsByTag(string $tagName, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 227 | - { |
|
| 228 | - foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 229 | - $item->increment($step); |
|
| 230 | - $this->saveDeferred($item); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - return $this->commit(); |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * @inheritDoc |
|
| 238 | - * @throws PhpfastcacheCoreException |
|
| 239 | - * @throws PhpfastcacheDriverException |
|
| 240 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 241 | - * @throws PhpfastcacheLogicException |
|
| 242 | - * @throws \ReflectionException |
|
| 243 | - */ |
|
| 244 | - public function decrementItemsByTags(array $tagNames, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 245 | - { |
|
| 246 | - $return = true; |
|
| 247 | - foreach ($tagNames as $tagName) { |
|
| 248 | - $result = $this->decrementItemsByTag($tagName, $step, $strategy); |
|
| 249 | - if ($result !== true) { |
|
| 250 | - $return = $result; |
|
| 251 | - } |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - return $return; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - /** |
|
| 258 | - * @inheritDoc |
|
| 259 | - * @throws PhpfastcacheCoreException |
|
| 260 | - * @throws PhpfastcacheDriverException |
|
| 261 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 262 | - * @throws PhpfastcacheLogicException |
|
| 263 | - * @throws \ReflectionException |
|
| 264 | - */ |
|
| 265 | - public function decrementItemsByTag(string $tagName, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 266 | - { |
|
| 267 | - foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 268 | - $item->decrement($step); |
|
| 269 | - $this->saveDeferred($item); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - return $this->commit(); |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * @inheritDoc |
|
| 277 | - * @throws PhpfastcacheCoreException |
|
| 278 | - * @throws PhpfastcacheDriverException |
|
| 279 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 280 | - * @throws PhpfastcacheLogicException |
|
| 281 | - * @throws \ReflectionException |
|
| 282 | - */ |
|
| 283 | - public function appendItemsByTags(array $tagNames, array|string $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 284 | - { |
|
| 285 | - $return = true; |
|
| 286 | - foreach ($tagNames as $tagName) { |
|
| 287 | - $result = $this->appendItemsByTag($tagName, $data, $strategy); |
|
| 288 | - if ($result !== true) { |
|
| 289 | - $return = $result; |
|
| 290 | - } |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - return $return; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * @inheritDoc |
|
| 298 | - * @throws PhpfastcacheCoreException |
|
| 299 | - * @throws PhpfastcacheDriverException |
|
| 300 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 301 | - * @throws PhpfastcacheLogicException |
|
| 302 | - * @throws \ReflectionException |
|
| 303 | - */ |
|
| 304 | - public function appendItemsByTag(string $tagName, array|string $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 305 | - { |
|
| 306 | - foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 307 | - $item->append($data); |
|
| 308 | - $this->saveDeferred($item); |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - return $this->commit(); |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - /** |
|
| 315 | - * @inheritDoc |
|
| 316 | - * @throws PhpfastcacheCoreException |
|
| 317 | - * @throws PhpfastcacheDriverException |
|
| 318 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 319 | - * @throws PhpfastcacheLogicException |
|
| 320 | - * @throws \ReflectionException |
|
| 321 | - */ |
|
| 322 | - public function prependItemsByTags(array $tagNames, array|string $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 323 | - { |
|
| 324 | - $return = true; |
|
| 325 | - foreach ($tagNames as $tagName) { |
|
| 326 | - $result = $this->prependItemsByTag($tagName, $data, $strategy); |
|
| 327 | - if ($result !== true) { |
|
| 328 | - $return = $result; |
|
| 329 | - } |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - return $return; |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * @inheritDoc |
|
| 337 | - * @throws PhpfastcacheCoreException |
|
| 338 | - * @throws PhpfastcacheDriverException |
|
| 339 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 340 | - * @throws PhpfastcacheLogicException |
|
| 341 | - * @throws \ReflectionException |
|
| 342 | - */ |
|
| 343 | - public function prependItemsByTag(string $tagName, array|string $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 344 | - { |
|
| 345 | - foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 346 | - $item->prepend($data); |
|
| 347 | - $this->saveDeferred($item); |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - return $this->commit(); |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - /** |
|
| 354 | - * @param array<mixed> $wrapper |
|
| 355 | - * @return string[] |
|
| 356 | - */ |
|
| 357 | - protected function driverUnwrapTags(array $wrapper): array |
|
| 358 | - { |
|
| 359 | - return $wrapper[TaggableCacheItemPoolInterface::DRIVER_TAGS_WRAPPER_INDEX]; |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * @param ExtendedCacheItemInterface $item |
|
| 364 | - * @throws PhpfastcacheCoreException |
|
| 365 | - * @throws PhpfastcacheDriverException |
|
| 366 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 367 | - * @throws PhpfastcacheLogicException |
|
| 368 | - */ |
|
| 369 | - protected function cleanItemTags(ExtendedCacheItemInterface $item): void |
|
| 370 | - { |
|
| 371 | - if (!empty($item->getTags()) || !empty($item->getRemovedTags())) { |
|
| 372 | - $this->driverWriteTags($item->removeTags($item->getTags())); |
|
| 373 | - } |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * @param ExtendedCacheItemInterface $item |
|
| 378 | - * @return bool |
|
| 379 | - * @throws PhpfastcacheCoreException |
|
| 380 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 381 | - * @throws PhpfastcacheLogicException |
|
| 382 | - * @throws PhpfastcacheDriverException |
|
| 383 | - */ |
|
| 384 | - protected function driverWriteTags(ExtendedCacheItemInterface $item): bool |
|
| 385 | - { |
|
| 386 | - /** |
|
| 387 | - * Do not attempt to write tags |
|
| 388 | - * on tags item, it can lead |
|
| 389 | - * to an infinite recursive calls |
|
| 390 | - */ |
|
| 391 | - if (str_starts_with($item->getKey(), TaggableCacheItemPoolInterface::DRIVER_TAGS_KEY_PREFIX)) { |
|
| 392 | - throw new PhpfastcacheLogicException('Trying to set tag(s) to an Tag item index: ' . $item->getKey()); |
|
| 393 | - } |
|
| 394 | - |
|
| 395 | - if (!$item->getTags() && !$item->getRemovedTags()) { |
|
| 396 | - return true; |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - /** |
|
| 400 | - * @var $tagsItems ExtendedCacheItemInterface[] |
|
| 401 | - */ |
|
| 402 | - $tagsItems = $this->getItems($this->getTagKeys($item->getTags())); |
|
| 403 | - |
|
| 404 | - foreach ($tagsItems as $tagsItem) { |
|
| 405 | - $data = $tagsItem->get(); |
|
| 406 | - $expTimestamp = $item->getExpirationDate()->getTimestamp(); |
|
| 407 | - |
|
| 408 | - /** |
|
| 409 | - * Using the key will |
|
| 410 | - * avoid to use array_unique |
|
| 411 | - * that has slow performances |
|
| 412 | - */ |
|
| 413 | - $data = \array_merge((array)$data, [$item->getKey() => $expTimestamp]); |
|
| 414 | - $tagsItem->set($data); |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * Recalculate the expiration date |
|
| 418 | - * |
|
| 419 | - * If the $tagsItem does not have |
|
| 420 | - * any cache item references left |
|
| 421 | - * then remove it from tagsItems index |
|
| 422 | - */ |
|
| 423 | - $tagsItem->expiresAt((new DateTime())->setTimestamp(max($data))); |
|
| 424 | - |
|
| 425 | - $this->driverWrite($tagsItem); |
|
| 426 | - $tagsItem->setHit(true); |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - /** |
|
| 430 | - * Also update removed tags to |
|
| 431 | - * keep the index up to date |
|
| 432 | - */ |
|
| 433 | - $tagsItems = $this->getItems($this->getTagKeys($item->getRemovedTags())); |
|
| 434 | - |
|
| 435 | - foreach ($tagsItems as $tagsItem) { |
|
| 436 | - $data = (array)$tagsItem->get(); |
|
| 437 | - |
|
| 438 | - unset($data[$item->getKey()]); |
|
| 439 | - $tagsItem->set($data); |
|
| 440 | - |
|
| 441 | - /** |
|
| 442 | - * Recalculate the expiration date |
|
| 443 | - * |
|
| 444 | - * If the $tagsItem does not have |
|
| 445 | - * any cache item references left |
|
| 446 | - * then remove it from tagsItems index |
|
| 447 | - */ |
|
| 448 | - if (\count($data)) { |
|
| 449 | - $tagsItem->expiresAt((new DateTime())->setTimestamp(max($data))); |
|
| 450 | - $this->driverWrite($tagsItem); |
|
| 451 | - $tagsItem->setHit(true); |
|
| 452 | - } else { |
|
| 453 | - $this->deleteItem($tagsItem->getKey()); |
|
| 454 | - } |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - return true; |
|
| 458 | - } |
|
| 459 | - |
|
| 460 | - /** |
|
| 461 | - * @param array<string> $keys |
|
| 462 | - * @return array<string> |
|
| 463 | - */ |
|
| 464 | - protected function getTagKeys(array $keys): array |
|
| 465 | - { |
|
| 466 | - return \array_map( |
|
| 467 | - fn (string $key) => $this->getTagKey($key), |
|
| 468 | - $keys |
|
| 469 | - ); |
|
| 470 | - } |
|
| 33 | + use ExtendedCacheItemPoolTrait; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @inheritDoc |
|
| 37 | + * @throws PhpfastcacheCoreException |
|
| 38 | + * @throws PhpfastcacheDriverException |
|
| 39 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 40 | + * @throws PhpfastcacheLogicException |
|
| 41 | + */ |
|
| 42 | + public function getItemsByTagsAsJsonString( |
|
| 43 | + array $tagNames, |
|
| 44 | + int $option = \JSON_THROW_ON_ERROR, |
|
| 45 | + int $depth = 512, |
|
| 46 | + int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE |
|
| 47 | + ): string { |
|
| 48 | + return \json_encode( |
|
| 49 | + \array_map( |
|
| 50 | + static fn(CacheItemInterface $item) => $item->get(), |
|
| 51 | + \array_values($this->getItemsByTags($tagNames, $strategy)) |
|
| 52 | + ), |
|
| 53 | + $option, |
|
| 54 | + $depth, |
|
| 55 | + ); |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @inheritDoc |
|
| 60 | + * @throws PhpfastcacheCoreException |
|
| 61 | + * @throws PhpfastcacheDriverException |
|
| 62 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 63 | + * @throws PhpfastcacheLogicException |
|
| 64 | + */ |
|
| 65 | + public function getItemsByTags(array $tagNames, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): array |
|
| 66 | + { |
|
| 67 | + $items = []; |
|
| 68 | + foreach (\array_unique($tagNames) as $tagName) { |
|
| 69 | + $items[] = $this->fetchItemsByTagFromBackend($tagName); |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + $items = \array_merge([], ...$items); |
|
| 73 | + |
|
| 74 | + switch ($strategy) { |
|
| 75 | + case TaggableCacheItemPoolInterface::TAG_STRATEGY_ALL: |
|
| 76 | + foreach ($items as $key => $item) { |
|
| 77 | + if (\array_diff($tagNames, $item->getTags())) { |
|
| 78 | + unset($items[$key]); |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | + break; |
|
| 82 | + |
|
| 83 | + case TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY: |
|
| 84 | + foreach ($items as $key => $item) { |
|
| 85 | + if (\array_diff($tagNames, $item->getTags()) || \array_diff($item->getTags(), $tagNames)) { |
|
| 86 | + unset($items[$key]); |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + break; |
|
| 90 | + } |
|
| 91 | + return $items; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * @param string $tagName |
|
| 96 | + * @return ExtendedCacheItemInterface[] |
|
| 97 | + * @throws PhpfastcacheCoreException |
|
| 98 | + * @throws PhpfastcacheDriverException |
|
| 99 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 100 | + * @throws PhpfastcacheLogicException |
|
| 101 | + */ |
|
| 102 | + protected function fetchItemsByTagFromBackend(string $tagName): array |
|
| 103 | + { |
|
| 104 | + $driverResponse = $this->getItem($this->getTagKey($tagName)); |
|
| 105 | + |
|
| 106 | + if ($driverResponse->isHit()) { |
|
| 107 | + $tagsItems = (array)$driverResponse->get(); |
|
| 108 | + /** |
|
| 109 | + * getItems() may provide expired item(s) |
|
| 110 | + * themselves provided by a cache of item |
|
| 111 | + * keys based stored the tag item. |
|
| 112 | + * Therefore, we pass a filter callback |
|
| 113 | + * to remove the expired Item(s) provided by |
|
| 114 | + * the item keys passed through getItems() |
|
| 115 | + * |
|
| 116 | + * #headache |
|
| 117 | + */ |
|
| 118 | + return \array_filter( |
|
| 119 | + $this->getItems(\array_unique(\array_keys($tagsItems))), |
|
| 120 | + static fn (ExtendedCacheItemInterface $item) => $item->isHit(), |
|
| 121 | + ); |
|
| 122 | + } |
|
| 123 | + return []; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * @param string $key |
|
| 128 | + * @return string |
|
| 129 | + */ |
|
| 130 | + protected function getTagKey(string $key): string |
|
| 131 | + { |
|
| 132 | + return TaggableCacheItemPoolInterface::DRIVER_TAGS_KEY_PREFIX . $key; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * @inheritDoc |
|
| 137 | + * @throws PhpfastcacheCoreException |
|
| 138 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 139 | + * @throws PhpfastcacheLogicException |
|
| 140 | + * @throws PhpfastcacheDriverException |
|
| 141 | + */ |
|
| 142 | + public function deleteItemsByTags(array $tagNames, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 143 | + { |
|
| 144 | + $return = true; |
|
| 145 | + |
|
| 146 | + foreach ($this->getItemsByTags($tagNames, $strategy) as $item) { |
|
| 147 | + $result = $this->deleteItem($item->getKey()); |
|
| 148 | + if ($result !== true) { |
|
| 149 | + $return = $result; |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + return $return; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * @inheritDoc |
|
| 158 | + * @throws PhpfastcacheCoreException |
|
| 159 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 160 | + * @throws PhpfastcacheLogicException |
|
| 161 | + * @throws PhpfastcacheDriverException |
|
| 162 | + */ |
|
| 163 | + public function deleteItemsByTag(string $tagName, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 164 | + { |
|
| 165 | + $return = true; |
|
| 166 | + foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 167 | + $result = $this->deleteItem($item->getKey()); |
|
| 168 | + if ($result !== true) { |
|
| 169 | + $return = $result; |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + return $return; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * @inheritDoc |
|
| 178 | + * @throws PhpfastcacheCoreException |
|
| 179 | + * @throws PhpfastcacheDriverException |
|
| 180 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 181 | + * @throws PhpfastcacheLogicException |
|
| 182 | + */ |
|
| 183 | + public function getItemsByTag(string $tagName, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): array |
|
| 184 | + { |
|
| 185 | + $items = $this->fetchItemsByTagFromBackend($tagName); |
|
| 186 | + |
|
| 187 | + if ($strategy === TaggableCacheItemPoolInterface::TAG_STRATEGY_ONLY) { |
|
| 188 | + foreach ($items as $key => $item) { |
|
| 189 | + if (\array_diff($item->getTags(), [$tagName])) { |
|
| 190 | + unset($items[$key]); |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + } |
|
| 194 | + return $items; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * @inheritDoc |
|
| 199 | + * @throws PhpfastcacheCoreException |
|
| 200 | + * @throws PhpfastcacheDriverException |
|
| 201 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 202 | + * @throws PhpfastcacheLogicException |
|
| 203 | + * @throws \ReflectionException |
|
| 204 | + */ |
|
| 205 | + public function incrementItemsByTags(array $tagNames, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 206 | + { |
|
| 207 | + $return = true; |
|
| 208 | + foreach ($tagNames as $tagName) { |
|
| 209 | + $result = $this->incrementItemsByTag($tagName, $step, $strategy); |
|
| 210 | + if ($result !== true) { |
|
| 211 | + $return = $result; |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + return $return; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * @inheritDoc |
|
| 220 | + * @throws PhpfastcacheCoreException |
|
| 221 | + * @throws PhpfastcacheDriverException |
|
| 222 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 223 | + * @throws PhpfastcacheLogicException |
|
| 224 | + * @throws \ReflectionException |
|
| 225 | + */ |
|
| 226 | + public function incrementItemsByTag(string $tagName, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 227 | + { |
|
| 228 | + foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 229 | + $item->increment($step); |
|
| 230 | + $this->saveDeferred($item); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + return $this->commit(); |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * @inheritDoc |
|
| 238 | + * @throws PhpfastcacheCoreException |
|
| 239 | + * @throws PhpfastcacheDriverException |
|
| 240 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 241 | + * @throws PhpfastcacheLogicException |
|
| 242 | + * @throws \ReflectionException |
|
| 243 | + */ |
|
| 244 | + public function decrementItemsByTags(array $tagNames, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 245 | + { |
|
| 246 | + $return = true; |
|
| 247 | + foreach ($tagNames as $tagName) { |
|
| 248 | + $result = $this->decrementItemsByTag($tagName, $step, $strategy); |
|
| 249 | + if ($result !== true) { |
|
| 250 | + $return = $result; |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + return $return; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + /** |
|
| 258 | + * @inheritDoc |
|
| 259 | + * @throws PhpfastcacheCoreException |
|
| 260 | + * @throws PhpfastcacheDriverException |
|
| 261 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 262 | + * @throws PhpfastcacheLogicException |
|
| 263 | + * @throws \ReflectionException |
|
| 264 | + */ |
|
| 265 | + public function decrementItemsByTag(string $tagName, int $step = 1, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 266 | + { |
|
| 267 | + foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 268 | + $item->decrement($step); |
|
| 269 | + $this->saveDeferred($item); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + return $this->commit(); |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * @inheritDoc |
|
| 277 | + * @throws PhpfastcacheCoreException |
|
| 278 | + * @throws PhpfastcacheDriverException |
|
| 279 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 280 | + * @throws PhpfastcacheLogicException |
|
| 281 | + * @throws \ReflectionException |
|
| 282 | + */ |
|
| 283 | + public function appendItemsByTags(array $tagNames, array|string $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 284 | + { |
|
| 285 | + $return = true; |
|
| 286 | + foreach ($tagNames as $tagName) { |
|
| 287 | + $result = $this->appendItemsByTag($tagName, $data, $strategy); |
|
| 288 | + if ($result !== true) { |
|
| 289 | + $return = $result; |
|
| 290 | + } |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + return $return; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * @inheritDoc |
|
| 298 | + * @throws PhpfastcacheCoreException |
|
| 299 | + * @throws PhpfastcacheDriverException |
|
| 300 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 301 | + * @throws PhpfastcacheLogicException |
|
| 302 | + * @throws \ReflectionException |
|
| 303 | + */ |
|
| 304 | + public function appendItemsByTag(string $tagName, array|string $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 305 | + { |
|
| 306 | + foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 307 | + $item->append($data); |
|
| 308 | + $this->saveDeferred($item); |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + return $this->commit(); |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + /** |
|
| 315 | + * @inheritDoc |
|
| 316 | + * @throws PhpfastcacheCoreException |
|
| 317 | + * @throws PhpfastcacheDriverException |
|
| 318 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 319 | + * @throws PhpfastcacheLogicException |
|
| 320 | + * @throws \ReflectionException |
|
| 321 | + */ |
|
| 322 | + public function prependItemsByTags(array $tagNames, array|string $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 323 | + { |
|
| 324 | + $return = true; |
|
| 325 | + foreach ($tagNames as $tagName) { |
|
| 326 | + $result = $this->prependItemsByTag($tagName, $data, $strategy); |
|
| 327 | + if ($result !== true) { |
|
| 328 | + $return = $result; |
|
| 329 | + } |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + return $return; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * @inheritDoc |
|
| 337 | + * @throws PhpfastcacheCoreException |
|
| 338 | + * @throws PhpfastcacheDriverException |
|
| 339 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 340 | + * @throws PhpfastcacheLogicException |
|
| 341 | + * @throws \ReflectionException |
|
| 342 | + */ |
|
| 343 | + public function prependItemsByTag(string $tagName, array|string $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 344 | + { |
|
| 345 | + foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
|
| 346 | + $item->prepend($data); |
|
| 347 | + $this->saveDeferred($item); |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + return $this->commit(); |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + /** |
|
| 354 | + * @param array<mixed> $wrapper |
|
| 355 | + * @return string[] |
|
| 356 | + */ |
|
| 357 | + protected function driverUnwrapTags(array $wrapper): array |
|
| 358 | + { |
|
| 359 | + return $wrapper[TaggableCacheItemPoolInterface::DRIVER_TAGS_WRAPPER_INDEX]; |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * @param ExtendedCacheItemInterface $item |
|
| 364 | + * @throws PhpfastcacheCoreException |
|
| 365 | + * @throws PhpfastcacheDriverException |
|
| 366 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 367 | + * @throws PhpfastcacheLogicException |
|
| 368 | + */ |
|
| 369 | + protected function cleanItemTags(ExtendedCacheItemInterface $item): void |
|
| 370 | + { |
|
| 371 | + if (!empty($item->getTags()) || !empty($item->getRemovedTags())) { |
|
| 372 | + $this->driverWriteTags($item->removeTags($item->getTags())); |
|
| 373 | + } |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * @param ExtendedCacheItemInterface $item |
|
| 378 | + * @return bool |
|
| 379 | + * @throws PhpfastcacheCoreException |
|
| 380 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 381 | + * @throws PhpfastcacheLogicException |
|
| 382 | + * @throws PhpfastcacheDriverException |
|
| 383 | + */ |
|
| 384 | + protected function driverWriteTags(ExtendedCacheItemInterface $item): bool |
|
| 385 | + { |
|
| 386 | + /** |
|
| 387 | + * Do not attempt to write tags |
|
| 388 | + * on tags item, it can lead |
|
| 389 | + * to an infinite recursive calls |
|
| 390 | + */ |
|
| 391 | + if (str_starts_with($item->getKey(), TaggableCacheItemPoolInterface::DRIVER_TAGS_KEY_PREFIX)) { |
|
| 392 | + throw new PhpfastcacheLogicException('Trying to set tag(s) to an Tag item index: ' . $item->getKey()); |
|
| 393 | + } |
|
| 394 | + |
|
| 395 | + if (!$item->getTags() && !$item->getRemovedTags()) { |
|
| 396 | + return true; |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + /** |
|
| 400 | + * @var $tagsItems ExtendedCacheItemInterface[] |
|
| 401 | + */ |
|
| 402 | + $tagsItems = $this->getItems($this->getTagKeys($item->getTags())); |
|
| 403 | + |
|
| 404 | + foreach ($tagsItems as $tagsItem) { |
|
| 405 | + $data = $tagsItem->get(); |
|
| 406 | + $expTimestamp = $item->getExpirationDate()->getTimestamp(); |
|
| 407 | + |
|
| 408 | + /** |
|
| 409 | + * Using the key will |
|
| 410 | + * avoid to use array_unique |
|
| 411 | + * that has slow performances |
|
| 412 | + */ |
|
| 413 | + $data = \array_merge((array)$data, [$item->getKey() => $expTimestamp]); |
|
| 414 | + $tagsItem->set($data); |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * Recalculate the expiration date |
|
| 418 | + * |
|
| 419 | + * If the $tagsItem does not have |
|
| 420 | + * any cache item references left |
|
| 421 | + * then remove it from tagsItems index |
|
| 422 | + */ |
|
| 423 | + $tagsItem->expiresAt((new DateTime())->setTimestamp(max($data))); |
|
| 424 | + |
|
| 425 | + $this->driverWrite($tagsItem); |
|
| 426 | + $tagsItem->setHit(true); |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + /** |
|
| 430 | + * Also update removed tags to |
|
| 431 | + * keep the index up to date |
|
| 432 | + */ |
|
| 433 | + $tagsItems = $this->getItems($this->getTagKeys($item->getRemovedTags())); |
|
| 434 | + |
|
| 435 | + foreach ($tagsItems as $tagsItem) { |
|
| 436 | + $data = (array)$tagsItem->get(); |
|
| 437 | + |
|
| 438 | + unset($data[$item->getKey()]); |
|
| 439 | + $tagsItem->set($data); |
|
| 440 | + |
|
| 441 | + /** |
|
| 442 | + * Recalculate the expiration date |
|
| 443 | + * |
|
| 444 | + * If the $tagsItem does not have |
|
| 445 | + * any cache item references left |
|
| 446 | + * then remove it from tagsItems index |
|
| 447 | + */ |
|
| 448 | + if (\count($data)) { |
|
| 449 | + $tagsItem->expiresAt((new DateTime())->setTimestamp(max($data))); |
|
| 450 | + $this->driverWrite($tagsItem); |
|
| 451 | + $tagsItem->setHit(true); |
|
| 452 | + } else { |
|
| 453 | + $this->deleteItem($tagsItem->getKey()); |
|
| 454 | + } |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + return true; |
|
| 458 | + } |
|
| 459 | + |
|
| 460 | + /** |
|
| 461 | + * @param array<string> $keys |
|
| 462 | + * @return array<string> |
|
| 463 | + */ |
|
| 464 | + protected function getTagKeys(array $keys): array |
|
| 465 | + { |
|
| 466 | + return \array_map( |
|
| 467 | + fn (string $key) => $this->getTagKey($key), |
|
| 468 | + $keys |
|
| 469 | + ); |
|
| 470 | + } |
|
| 471 | 471 | } |
@@ -104,7 +104,7 @@ discard block |
||
| 104 | 104 | $driverResponse = $this->getItem($this->getTagKey($tagName)); |
| 105 | 105 | |
| 106 | 106 | if ($driverResponse->isHit()) { |
| 107 | - $tagsItems = (array)$driverResponse->get(); |
|
| 107 | + $tagsItems = (array) $driverResponse->get(); |
|
| 108 | 108 | /** |
| 109 | 109 | * getItems() may provide expired item(s) |
| 110 | 110 | * themselves provided by a cache of item |
@@ -280,7 +280,7 @@ discard block |
||
| 280 | 280 | * @throws PhpfastcacheLogicException |
| 281 | 281 | * @throws \ReflectionException |
| 282 | 282 | */ |
| 283 | - public function appendItemsByTags(array $tagNames, array|string $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 283 | + public function appendItemsByTags(array $tagNames, array | string $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 284 | 284 | { |
| 285 | 285 | $return = true; |
| 286 | 286 | foreach ($tagNames as $tagName) { |
@@ -301,7 +301,7 @@ discard block |
||
| 301 | 301 | * @throws PhpfastcacheLogicException |
| 302 | 302 | * @throws \ReflectionException |
| 303 | 303 | */ |
| 304 | - public function appendItemsByTag(string $tagName, array|string $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 304 | + public function appendItemsByTag(string $tagName, array | string $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 305 | 305 | { |
| 306 | 306 | foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
| 307 | 307 | $item->append($data); |
@@ -319,7 +319,7 @@ discard block |
||
| 319 | 319 | * @throws PhpfastcacheLogicException |
| 320 | 320 | * @throws \ReflectionException |
| 321 | 321 | */ |
| 322 | - public function prependItemsByTags(array $tagNames, array|string $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 322 | + public function prependItemsByTags(array $tagNames, array | string $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 323 | 323 | { |
| 324 | 324 | $return = true; |
| 325 | 325 | foreach ($tagNames as $tagName) { |
@@ -340,7 +340,7 @@ discard block |
||
| 340 | 340 | * @throws PhpfastcacheLogicException |
| 341 | 341 | * @throws \ReflectionException |
| 342 | 342 | */ |
| 343 | - public function prependItemsByTag(string $tagName, array|string $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 343 | + public function prependItemsByTag(string $tagName, array | string $data, int $strategy = TaggableCacheItemPoolInterface::TAG_STRATEGY_ONE): bool |
|
| 344 | 344 | { |
| 345 | 345 | foreach ($this->getItemsByTag($tagName, $strategy) as $item) { |
| 346 | 346 | $item->prepend($data); |
@@ -410,7 +410,7 @@ discard block |
||
| 410 | 410 | * avoid to use array_unique |
| 411 | 411 | * that has slow performances |
| 412 | 412 | */ |
| 413 | - $data = \array_merge((array)$data, [$item->getKey() => $expTimestamp]); |
|
| 413 | + $data = \array_merge((array) $data, [$item->getKey() => $expTimestamp]); |
|
| 414 | 414 | $tagsItem->set($data); |
| 415 | 415 | |
| 416 | 416 | /** |
@@ -433,7 +433,7 @@ discard block |
||
| 433 | 433 | $tagsItems = $this->getItems($this->getTagKeys($item->getRemovedTags())); |
| 434 | 434 | |
| 435 | 435 | foreach ($tagsItems as $tagsItem) { |
| 436 | - $data = (array)$tagsItem->get(); |
|
| 436 | + $data = (array) $tagsItem->get(); |
|
| 437 | 437 | |
| 438 | 438 | unset($data[$item->getKey()]); |
| 439 | 439 | $tagsItem->set($data); |
@@ -449,7 +449,8 @@ |
||
| 449 | 449 | $tagsItem->expiresAt((new DateTime())->setTimestamp(max($data))); |
| 450 | 450 | $this->driverWrite($tagsItem); |
| 451 | 451 | $tagsItem->setHit(true); |
| 452 | - } else { |
|
| 452 | + } |
|
| 453 | + else { |
|
| 453 | 454 | $this->deleteItem($tagsItem->getKey()); |
| 454 | 455 | } |
| 455 | 456 | } |
@@ -39,286 +39,286 @@ |
||
| 39 | 39 | |
| 40 | 40 | trait DriverBaseTrait |
| 41 | 41 | { |
| 42 | - use DriverPoolAbstractTrait; |
|
| 43 | - use ClassNamespaceResolverTrait; |
|
| 44 | - use EventManagerDispatcherTrait; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @var string[] |
|
| 48 | - */ |
|
| 49 | - protected static array $cacheItemClasses = []; |
|
| 50 | - |
|
| 51 | - protected ConfigurationOptionInterface $config; |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * @var object|null |
|
| 55 | - */ |
|
| 56 | - protected ?object $instance; |
|
| 57 | - |
|
| 58 | - protected string $driverName; |
|
| 59 | - |
|
| 60 | - protected string $instanceId; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Driver constructor. |
|
| 64 | - * @param ConfigurationOptionInterface $config |
|
| 65 | - * @param string $instanceId |
|
| 66 | - * @param EventManagerInterface $em |
|
| 67 | - * @throws PhpfastcacheCoreException |
|
| 68 | - * @throws PhpfastcacheDriverCheckException |
|
| 69 | - * @throws PhpfastcacheDriverConnectException |
|
| 70 | - * @throws PhpfastcacheIOException |
|
| 71 | - * @throws PhpfastcacheInvalidArgumentException |
|
| 72 | - */ |
|
| 73 | - public function __construct(#[\SensitiveParameter] ConfigurationOptionInterface $config, string $instanceId, EventManagerInterface $em) |
|
| 74 | - { |
|
| 75 | - $this->setEventManager($em->getScopedEventManager($this)); |
|
| 76 | - $this->setConfig($config); |
|
| 77 | - $this->instanceId = $instanceId; |
|
| 78 | - |
|
| 79 | - if (!$this->driverCheck()) { |
|
| 80 | - throw new PhpfastcacheDriverCheckException( |
|
| 81 | - \sprintf( |
|
| 82 | - ExtendedCacheItemPoolInterface::DRIVER_CHECK_FAILURE, |
|
| 83 | - $this->getDriverName(), |
|
| 84 | - $this->getHelp() ? " Additionally, {$this->getHelp()}" : '' |
|
| 85 | - ) |
|
| 86 | - ); |
|
| 87 | - } |
|
| 88 | - $this->eventManager->dispatch(Event::CACHE_DRIVER_CHECKED, $this); |
|
| 89 | - |
|
| 90 | - try { |
|
| 91 | - $this->driverConnect(); |
|
| 92 | - $config->lock($this); // Lock the config only after a successful driver connection. |
|
| 93 | - $this->eventManager->dispatch(Event::CACHE_DRIVER_CONNECTED, $this, $this->instance ?? null); |
|
| 94 | - } catch (Throwable $e) { |
|
| 95 | - throw new PhpfastcacheDriverConnectException( |
|
| 96 | - sprintf( |
|
| 97 | - ExtendedCacheItemPoolInterface::DRIVER_CONNECT_FAILURE, |
|
| 98 | - $this->getDriverName(), |
|
| 99 | - $e->getMessage(), |
|
| 100 | - $e->getLine() ?: 'unknown line', |
|
| 101 | - $e->getFile() ?: 'unknown file', |
|
| 102 | - ), |
|
| 103 | - 0, |
|
| 104 | - $e |
|
| 105 | - ); |
|
| 106 | - } |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @return string |
|
| 111 | - */ |
|
| 112 | - public function getDriverName(): string |
|
| 113 | - { |
|
| 114 | - if (!isset($this->driverName)) { |
|
| 115 | - $this->driverName = \ucfirst(\substr(\strrchr((new ReflectionObject($this))->getNamespaceName(), '\\'), 1)); |
|
| 116 | - } |
|
| 117 | - return $this->driverName; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * @return ConfigurationOptionInterface |
|
| 122 | - */ |
|
| 123 | - public function getDefaultConfig(): ConfigurationOptionInterface |
|
| 124 | - { |
|
| 125 | - $className = $this::getConfigClass(); |
|
| 126 | - |
|
| 127 | - return new $className(); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * @return string |
|
| 132 | - */ |
|
| 133 | - public static function getConfigClass(): string |
|
| 134 | - { |
|
| 135 | - $localConfigClass = \substr(static::class, 0, \strrpos(static::class, '\\')) . '\Config'; |
|
| 136 | - if (\class_exists($localConfigClass) && \is_a($localConfigClass, ConfigurationOption::class, true)) { |
|
| 137 | - return $localConfigClass; |
|
| 138 | - } |
|
| 139 | - return ConfigurationOption::class; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - public static function getItemClass(): string |
|
| 143 | - { |
|
| 144 | - if (!isset(self::$cacheItemClasses[static::class])) { |
|
| 145 | - self::$cacheItemClasses[static::class] = self::getClassNamespace() . '\\' . 'Item'; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - return self::$cacheItemClasses[static::class]; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * @inheritDoc |
|
| 153 | - */ |
|
| 154 | - public function getEncodedKey(string $key): string |
|
| 155 | - { |
|
| 156 | - $keyHashFunction = $this->getConfig()->getDefaultKeyHashFunction(); |
|
| 157 | - |
|
| 158 | - if ($keyHashFunction) { |
|
| 159 | - if (\is_callable($keyHashFunction)) { |
|
| 160 | - return $keyHashFunction($key); |
|
| 161 | - } |
|
| 162 | - throw new PhpfastcacheLogicException('Unable to build the encoded key (defaultKeyHashFunction is not callable)'); |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - return $key; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * @param ExtendedCacheItemInterface $item |
|
| 172 | - * @param bool $stringifyDate |
|
| 173 | - * @return array<string, mixed> |
|
| 174 | - * @throws PhpfastcacheLogicException |
|
| 175 | - */ |
|
| 176 | - public function driverPreWrap(ExtendedCacheItemInterface $item, bool $stringifyDate = false): array |
|
| 177 | - { |
|
| 178 | - $wrap = [ |
|
| 179 | - ExtendedCacheItemPoolInterface::DRIVER_KEY_WRAPPER_INDEX => $item->getKey(), // Stored but not really used, allow you to quickly identify the cache key |
|
| 180 | - ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX => $item->_getData(), |
|
| 181 | - ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX => $item->getExpirationDate(), |
|
| 182 | - TaggableCacheItemPoolInterface::DRIVER_TAGS_WRAPPER_INDEX => $item->getTags(), |
|
| 183 | - ]; |
|
| 184 | - |
|
| 185 | - if ($this->getConfig()->isItemDetailedDate()) { |
|
| 186 | - $wrap[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = new DateTime();// Always on the latest date |
|
| 187 | - /** |
|
| 188 | - * If the creation date exists |
|
| 189 | - * reuse it else set a new Date |
|
| 190 | - */ |
|
| 191 | - $wrap[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = $item->getCreationDate(); |
|
| 192 | - } else { |
|
| 193 | - $wrap[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = null; |
|
| 194 | - $wrap[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = null; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - if ($stringifyDate) { |
|
| 198 | - \array_walk($wrap, static function (mixed &$value, string $key): void { |
|
| 199 | - if ($value instanceof DateTimeInterface && $key !== ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX) { |
|
| 200 | - $value = $value->format(DateTimeInterface::W3C); |
|
| 201 | - } |
|
| 202 | - }); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - return $wrap; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * @return ConfigurationOptionInterface |
|
| 210 | - */ |
|
| 211 | - public function getConfig(): ConfigurationOptionInterface |
|
| 212 | - { |
|
| 213 | - return $this->config; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * @param ConfigurationOptionInterface $config |
|
| 218 | - * @return static |
|
| 219 | - */ |
|
| 220 | - public function setConfig(ConfigurationOptionInterface $config): static |
|
| 221 | - { |
|
| 222 | - $this->config = $config; |
|
| 223 | - |
|
| 224 | - return $this; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * @param array<string, mixed> $wrapper |
|
| 229 | - * @return mixed |
|
| 230 | - * @throws \Exception |
|
| 231 | - */ |
|
| 232 | - public function driverUnwrapData(array $wrapper): mixed |
|
| 233 | - { |
|
| 234 | - return $wrapper[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX]; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * @param array<string, mixed> $wrapper |
|
| 239 | - * @return DateTimeInterface |
|
| 240 | - */ |
|
| 241 | - public function driverUnwrapEdate(array $wrapper): \DateTimeInterface |
|
| 242 | - { |
|
| 243 | - if ($wrapper[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] instanceof \DateTimeInterface) { |
|
| 244 | - return $wrapper[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - return DateTime::createFromFormat(\DateTimeInterface::W3C, $wrapper[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]); |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - /** |
|
| 251 | - * @param array<string, mixed> $wrapper |
|
| 252 | - * @return DateTimeInterface|null |
|
| 253 | - */ |
|
| 254 | - public function driverUnwrapCdate(array $wrapper): ?\DateTimeInterface |
|
| 255 | - { |
|
| 256 | - if ($wrapper[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] instanceof \DateTimeInterface) { |
|
| 257 | - return $wrapper[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]; |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - return DateTime::createFromFormat(\DateTimeInterface::W3C, $wrapper[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - /** |
|
| 264 | - * @param array<string, mixed> $wrapper |
|
| 265 | - * @return DateTimeInterface|null |
|
| 266 | - */ |
|
| 267 | - public function driverUnwrapMdate(array $wrapper): ?\DateTimeInterface |
|
| 268 | - { |
|
| 269 | - if ($wrapper[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] instanceof \DateTimeInterface) { |
|
| 270 | - return $wrapper[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - return DateTime::createFromFormat(\DateTimeInterface::W3C, $wrapper[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]); |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * @return string |
|
| 278 | - */ |
|
| 279 | - public function getInstanceId(): string |
|
| 280 | - { |
|
| 281 | - return $this->instanceId; |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * Encode data types such as object/array |
|
| 286 | - * for driver that does not support |
|
| 287 | - * non-scalar value |
|
| 288 | - * @param mixed $data |
|
| 289 | - * @return string |
|
| 290 | - */ |
|
| 291 | - protected function encode(mixed $data): string |
|
| 292 | - { |
|
| 293 | - return \serialize($data); |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * Decode data stored in the cache |
|
| 298 | - * for driver that does not support |
|
| 299 | - * non-scalar value storage. |
|
| 300 | - * @param string|null $value |
|
| 301 | - * @return array<string, mixed>|null |
|
| 302 | - * @throws PhpfastcacheDriverException |
|
| 303 | - */ |
|
| 304 | - protected function decode(?string $value): ?array |
|
| 305 | - { |
|
| 306 | - $decoded = $this->unserialize($value); |
|
| 307 | - |
|
| 308 | - if ($decoded === null || is_array($decoded)) { |
|
| 309 | - return $decoded; |
|
| 310 | - } |
|
| 311 | - throw new PhpfastcacheCorruptedDataException( |
|
| 312 | - sprintf( |
|
| 313 | - 'Failed to unserialize data from the cache, expected array or null but got "%s". Stored data may be corrupted.', |
|
| 314 | - gettype($decoded) |
|
| 315 | - ), |
|
| 316 | - $value |
|
| 317 | - ); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - protected function unserialize(?string $value): mixed |
|
| 321 | - { |
|
| 322 | - return $value ? \unserialize($value, ['allowed_classes' => true]) : null; |
|
| 323 | - } |
|
| 42 | + use DriverPoolAbstractTrait; |
|
| 43 | + use ClassNamespaceResolverTrait; |
|
| 44 | + use EventManagerDispatcherTrait; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @var string[] |
|
| 48 | + */ |
|
| 49 | + protected static array $cacheItemClasses = []; |
|
| 50 | + |
|
| 51 | + protected ConfigurationOptionInterface $config; |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * @var object|null |
|
| 55 | + */ |
|
| 56 | + protected ?object $instance; |
|
| 57 | + |
|
| 58 | + protected string $driverName; |
|
| 59 | + |
|
| 60 | + protected string $instanceId; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Driver constructor. |
|
| 64 | + * @param ConfigurationOptionInterface $config |
|
| 65 | + * @param string $instanceId |
|
| 66 | + * @param EventManagerInterface $em |
|
| 67 | + * @throws PhpfastcacheCoreException |
|
| 68 | + * @throws PhpfastcacheDriverCheckException |
|
| 69 | + * @throws PhpfastcacheDriverConnectException |
|
| 70 | + * @throws PhpfastcacheIOException |
|
| 71 | + * @throws PhpfastcacheInvalidArgumentException |
|
| 72 | + */ |
|
| 73 | + public function __construct(#[\SensitiveParameter] ConfigurationOptionInterface $config, string $instanceId, EventManagerInterface $em) |
|
| 74 | + { |
|
| 75 | + $this->setEventManager($em->getScopedEventManager($this)); |
|
| 76 | + $this->setConfig($config); |
|
| 77 | + $this->instanceId = $instanceId; |
|
| 78 | + |
|
| 79 | + if (!$this->driverCheck()) { |
|
| 80 | + throw new PhpfastcacheDriverCheckException( |
|
| 81 | + \sprintf( |
|
| 82 | + ExtendedCacheItemPoolInterface::DRIVER_CHECK_FAILURE, |
|
| 83 | + $this->getDriverName(), |
|
| 84 | + $this->getHelp() ? " Additionally, {$this->getHelp()}" : '' |
|
| 85 | + ) |
|
| 86 | + ); |
|
| 87 | + } |
|
| 88 | + $this->eventManager->dispatch(Event::CACHE_DRIVER_CHECKED, $this); |
|
| 89 | + |
|
| 90 | + try { |
|
| 91 | + $this->driverConnect(); |
|
| 92 | + $config->lock($this); // Lock the config only after a successful driver connection. |
|
| 93 | + $this->eventManager->dispatch(Event::CACHE_DRIVER_CONNECTED, $this, $this->instance ?? null); |
|
| 94 | + } catch (Throwable $e) { |
|
| 95 | + throw new PhpfastcacheDriverConnectException( |
|
| 96 | + sprintf( |
|
| 97 | + ExtendedCacheItemPoolInterface::DRIVER_CONNECT_FAILURE, |
|
| 98 | + $this->getDriverName(), |
|
| 99 | + $e->getMessage(), |
|
| 100 | + $e->getLine() ?: 'unknown line', |
|
| 101 | + $e->getFile() ?: 'unknown file', |
|
| 102 | + ), |
|
| 103 | + 0, |
|
| 104 | + $e |
|
| 105 | + ); |
|
| 106 | + } |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @return string |
|
| 111 | + */ |
|
| 112 | + public function getDriverName(): string |
|
| 113 | + { |
|
| 114 | + if (!isset($this->driverName)) { |
|
| 115 | + $this->driverName = \ucfirst(\substr(\strrchr((new ReflectionObject($this))->getNamespaceName(), '\\'), 1)); |
|
| 116 | + } |
|
| 117 | + return $this->driverName; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * @return ConfigurationOptionInterface |
|
| 122 | + */ |
|
| 123 | + public function getDefaultConfig(): ConfigurationOptionInterface |
|
| 124 | + { |
|
| 125 | + $className = $this::getConfigClass(); |
|
| 126 | + |
|
| 127 | + return new $className(); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * @return string |
|
| 132 | + */ |
|
| 133 | + public static function getConfigClass(): string |
|
| 134 | + { |
|
| 135 | + $localConfigClass = \substr(static::class, 0, \strrpos(static::class, '\\')) . '\Config'; |
|
| 136 | + if (\class_exists($localConfigClass) && \is_a($localConfigClass, ConfigurationOption::class, true)) { |
|
| 137 | + return $localConfigClass; |
|
| 138 | + } |
|
| 139 | + return ConfigurationOption::class; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + public static function getItemClass(): string |
|
| 143 | + { |
|
| 144 | + if (!isset(self::$cacheItemClasses[static::class])) { |
|
| 145 | + self::$cacheItemClasses[static::class] = self::getClassNamespace() . '\\' . 'Item'; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + return self::$cacheItemClasses[static::class]; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * @inheritDoc |
|
| 153 | + */ |
|
| 154 | + public function getEncodedKey(string $key): string |
|
| 155 | + { |
|
| 156 | + $keyHashFunction = $this->getConfig()->getDefaultKeyHashFunction(); |
|
| 157 | + |
|
| 158 | + if ($keyHashFunction) { |
|
| 159 | + if (\is_callable($keyHashFunction)) { |
|
| 160 | + return $keyHashFunction($key); |
|
| 161 | + } |
|
| 162 | + throw new PhpfastcacheLogicException('Unable to build the encoded key (defaultKeyHashFunction is not callable)'); |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + return $key; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * @param ExtendedCacheItemInterface $item |
|
| 172 | + * @param bool $stringifyDate |
|
| 173 | + * @return array<string, mixed> |
|
| 174 | + * @throws PhpfastcacheLogicException |
|
| 175 | + */ |
|
| 176 | + public function driverPreWrap(ExtendedCacheItemInterface $item, bool $stringifyDate = false): array |
|
| 177 | + { |
|
| 178 | + $wrap = [ |
|
| 179 | + ExtendedCacheItemPoolInterface::DRIVER_KEY_WRAPPER_INDEX => $item->getKey(), // Stored but not really used, allow you to quickly identify the cache key |
|
| 180 | + ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX => $item->_getData(), |
|
| 181 | + ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX => $item->getExpirationDate(), |
|
| 182 | + TaggableCacheItemPoolInterface::DRIVER_TAGS_WRAPPER_INDEX => $item->getTags(), |
|
| 183 | + ]; |
|
| 184 | + |
|
| 185 | + if ($this->getConfig()->isItemDetailedDate()) { |
|
| 186 | + $wrap[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = new DateTime();// Always on the latest date |
|
| 187 | + /** |
|
| 188 | + * If the creation date exists |
|
| 189 | + * reuse it else set a new Date |
|
| 190 | + */ |
|
| 191 | + $wrap[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = $item->getCreationDate(); |
|
| 192 | + } else { |
|
| 193 | + $wrap[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = null; |
|
| 194 | + $wrap[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = null; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + if ($stringifyDate) { |
|
| 198 | + \array_walk($wrap, static function (mixed &$value, string $key): void { |
|
| 199 | + if ($value instanceof DateTimeInterface && $key !== ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX) { |
|
| 200 | + $value = $value->format(DateTimeInterface::W3C); |
|
| 201 | + } |
|
| 202 | + }); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + return $wrap; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * @return ConfigurationOptionInterface |
|
| 210 | + */ |
|
| 211 | + public function getConfig(): ConfigurationOptionInterface |
|
| 212 | + { |
|
| 213 | + return $this->config; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * @param ConfigurationOptionInterface $config |
|
| 218 | + * @return static |
|
| 219 | + */ |
|
| 220 | + public function setConfig(ConfigurationOptionInterface $config): static |
|
| 221 | + { |
|
| 222 | + $this->config = $config; |
|
| 223 | + |
|
| 224 | + return $this; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * @param array<string, mixed> $wrapper |
|
| 229 | + * @return mixed |
|
| 230 | + * @throws \Exception |
|
| 231 | + */ |
|
| 232 | + public function driverUnwrapData(array $wrapper): mixed |
|
| 233 | + { |
|
| 234 | + return $wrapper[ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX]; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * @param array<string, mixed> $wrapper |
|
| 239 | + * @return DateTimeInterface |
|
| 240 | + */ |
|
| 241 | + public function driverUnwrapEdate(array $wrapper): \DateTimeInterface |
|
| 242 | + { |
|
| 243 | + if ($wrapper[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX] instanceof \DateTimeInterface) { |
|
| 244 | + return $wrapper[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + return DateTime::createFromFormat(\DateTimeInterface::W3C, $wrapper[ExtendedCacheItemPoolInterface::DRIVER_EDATE_WRAPPER_INDEX]); |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + /** |
|
| 251 | + * @param array<string, mixed> $wrapper |
|
| 252 | + * @return DateTimeInterface|null |
|
| 253 | + */ |
|
| 254 | + public function driverUnwrapCdate(array $wrapper): ?\DateTimeInterface |
|
| 255 | + { |
|
| 256 | + if ($wrapper[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] instanceof \DateTimeInterface) { |
|
| 257 | + return $wrapper[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]; |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + return DateTime::createFromFormat(\DateTimeInterface::W3C, $wrapper[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX]); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + /** |
|
| 264 | + * @param array<string, mixed> $wrapper |
|
| 265 | + * @return DateTimeInterface|null |
|
| 266 | + */ |
|
| 267 | + public function driverUnwrapMdate(array $wrapper): ?\DateTimeInterface |
|
| 268 | + { |
|
| 269 | + if ($wrapper[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] instanceof \DateTimeInterface) { |
|
| 270 | + return $wrapper[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + return DateTime::createFromFormat(\DateTimeInterface::W3C, $wrapper[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX]); |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * @return string |
|
| 278 | + */ |
|
| 279 | + public function getInstanceId(): string |
|
| 280 | + { |
|
| 281 | + return $this->instanceId; |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * Encode data types such as object/array |
|
| 286 | + * for driver that does not support |
|
| 287 | + * non-scalar value |
|
| 288 | + * @param mixed $data |
|
| 289 | + * @return string |
|
| 290 | + */ |
|
| 291 | + protected function encode(mixed $data): string |
|
| 292 | + { |
|
| 293 | + return \serialize($data); |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * Decode data stored in the cache |
|
| 298 | + * for driver that does not support |
|
| 299 | + * non-scalar value storage. |
|
| 300 | + * @param string|null $value |
|
| 301 | + * @return array<string, mixed>|null |
|
| 302 | + * @throws PhpfastcacheDriverException |
|
| 303 | + */ |
|
| 304 | + protected function decode(?string $value): ?array |
|
| 305 | + { |
|
| 306 | + $decoded = $this->unserialize($value); |
|
| 307 | + |
|
| 308 | + if ($decoded === null || is_array($decoded)) { |
|
| 309 | + return $decoded; |
|
| 310 | + } |
|
| 311 | + throw new PhpfastcacheCorruptedDataException( |
|
| 312 | + sprintf( |
|
| 313 | + 'Failed to unserialize data from the cache, expected array or null but got "%s". Stored data may be corrupted.', |
|
| 314 | + gettype($decoded) |
|
| 315 | + ), |
|
| 316 | + $value |
|
| 317 | + ); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + protected function unserialize(?string $value): mixed |
|
| 321 | + { |
|
| 322 | + return $value ? \unserialize($value, ['allowed_classes' => true]) : null; |
|
| 323 | + } |
|
| 324 | 324 | } |
@@ -183,7 +183,7 @@ discard block |
||
| 183 | 183 | ]; |
| 184 | 184 | |
| 185 | 185 | if ($this->getConfig()->isItemDetailedDate()) { |
| 186 | - $wrap[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = new DateTime();// Always on the latest date |
|
| 186 | + $wrap[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = new DateTime(); // Always on the latest date |
|
| 187 | 187 | /** |
| 188 | 188 | * If the creation date exists |
| 189 | 189 | * reuse it else set a new Date |
@@ -195,7 +195,7 @@ discard block |
||
| 195 | 195 | } |
| 196 | 196 | |
| 197 | 197 | if ($stringifyDate) { |
| 198 | - \array_walk($wrap, static function (mixed &$value, string $key): void { |
|
| 198 | + \array_walk($wrap, static function(mixed &$value, string $key): void { |
|
| 199 | 199 | if ($value instanceof DateTimeInterface && $key !== ExtendedCacheItemPoolInterface::DRIVER_DATA_WRAPPER_INDEX) { |
| 200 | 200 | $value = $value->format(DateTimeInterface::W3C); |
| 201 | 201 | } |
@@ -70,8 +70,7 @@ discard block |
||
| 70 | 70 | * @throws PhpfastcacheIOException |
| 71 | 71 | * @throws PhpfastcacheInvalidArgumentException |
| 72 | 72 | */ |
| 73 | - public function __construct(#[\SensitiveParameter] ConfigurationOptionInterface $config, string $instanceId, EventManagerInterface $em) |
|
| 74 | - { |
|
| 73 | + public function __construct(#[\SensitiveParameter] ConfigurationOptionInterface $config, string $instanceId, EventManagerInterface $em) { |
|
| 75 | 74 | $this->setEventManager($em->getScopedEventManager($this)); |
| 76 | 75 | $this->setConfig($config); |
| 77 | 76 | $this->instanceId = $instanceId; |
@@ -91,7 +90,8 @@ discard block |
||
| 91 | 90 | $this->driverConnect(); |
| 92 | 91 | $config->lock($this); // Lock the config only after a successful driver connection. |
| 93 | 92 | $this->eventManager->dispatch(Event::CACHE_DRIVER_CONNECTED, $this, $this->instance ?? null); |
| 94 | - } catch (Throwable $e) { |
|
| 93 | + } |
|
| 94 | + catch (Throwable $e) { |
|
| 95 | 95 | throw new PhpfastcacheDriverConnectException( |
| 96 | 96 | sprintf( |
| 97 | 97 | ExtendedCacheItemPoolInterface::DRIVER_CONNECT_FAILURE, |
@@ -189,7 +189,8 @@ discard block |
||
| 189 | 189 | * reuse it else set a new Date |
| 190 | 190 | */ |
| 191 | 191 | $wrap[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = $item->getCreationDate(); |
| 192 | - } else { |
|
| 192 | + } |
|
| 193 | + else { |
|
| 193 | 194 | $wrap[ExtendedCacheItemPoolInterface::DRIVER_MDATE_WRAPPER_INDEX] = null; |
| 194 | 195 | $wrap[ExtendedCacheItemPoolInterface::DRIVER_CDATE_WRAPPER_INDEX] = null; |
| 195 | 196 | } |