123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407 |
- <?php
- namespace Symfony\Component\Cache\Adapter;
- use Psr\Cache\CacheItemInterface;
- use Psr\Log\LoggerAwareInterface;
- use Psr\Log\LoggerAwareTrait;
- use Symfony\Component\Cache\CacheItem;
- use Symfony\Component\Cache\Exception\InvalidArgumentException;
- use Symfony\Component\Cache\ResettableInterface;
- use Symfony\Contracts\Cache\CacheInterface;
- class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInterface, ResettableInterface
- {
- use LoggerAwareTrait;
- private $storeSerialized;
- private $values = [];
- private $expiries = [];
- private $defaultLifetime;
- private $maxLifetime;
- private $maxItems;
- private static $createCacheItem;
-
- public function __construct(int $defaultLifetime = 0, bool $storeSerialized = true, float $maxLifetime = 0, int $maxItems = 0)
- {
- if (0 > $maxLifetime) {
- throw new InvalidArgumentException(sprintf('Argument $maxLifetime must be positive, %F passed.', $maxLifetime));
- }
- if (0 > $maxItems) {
- throw new InvalidArgumentException(sprintf('Argument $maxItems must be a positive integer, %d passed.', $maxItems));
- }
- $this->defaultLifetime = $defaultLifetime;
- $this->storeSerialized = $storeSerialized;
- $this->maxLifetime = $maxLifetime;
- $this->maxItems = $maxItems;
- self::$createCacheItem ?? self::$createCacheItem = \Closure::bind(
- static function ($key, $value, $isHit) {
- $item = new CacheItem();
- $item->key = $key;
- $item->value = $value;
- $item->isHit = $isHit;
- return $item;
- },
- null,
- CacheItem::class
- );
- }
-
- public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
- {
- $item = $this->getItem($key);
- $metadata = $item->getMetadata();
-
- if (\INF === $beta || !$item->isHit()) {
- $save = true;
- $item->set($callback($item, $save));
- if ($save) {
- $this->save($item);
- }
- }
- return $item->get();
- }
-
- public function delete(string $key): bool
- {
- return $this->deleteItem($key);
- }
-
- public function hasItem($key)
- {
- if (\is_string($key) && isset($this->expiries[$key]) && $this->expiries[$key] > microtime(true)) {
- if ($this->maxItems) {
-
- $value = $this->values[$key];
- unset($this->values[$key]);
- $this->values[$key] = $value;
- }
- return true;
- }
- \assert('' !== CacheItem::validateKey($key));
- return isset($this->expiries[$key]) && !$this->deleteItem($key);
- }
-
- public function getItem($key)
- {
- if (!$isHit = $this->hasItem($key)) {
- $value = null;
- if (!$this->maxItems) {
-
- $this->values[$key] = null;
- }
- } else {
- $value = $this->storeSerialized ? $this->unfreeze($key, $isHit) : $this->values[$key];
- }
- return (self::$createCacheItem)($key, $value, $isHit);
- }
-
- public function getItems(array $keys = [])
- {
- \assert(self::validateKeys($keys));
- return $this->generateItems($keys, microtime(true), self::$createCacheItem);
- }
-
- public function deleteItem($key)
- {
- \assert('' !== CacheItem::validateKey($key));
- unset($this->values[$key], $this->expiries[$key]);
- return true;
- }
-
- public function deleteItems(array $keys)
- {
- foreach ($keys as $key) {
- $this->deleteItem($key);
- }
- return true;
- }
-
- public function save(CacheItemInterface $item)
- {
- if (!$item instanceof CacheItem) {
- return false;
- }
- $item = (array) $item;
- $key = $item["\0*\0key"];
- $value = $item["\0*\0value"];
- $expiry = $item["\0*\0expiry"];
- $now = microtime(true);
- if (null !== $expiry) {
- if (!$expiry) {
- $expiry = \PHP_INT_MAX;
- } elseif ($expiry <= $now) {
- $this->deleteItem($key);
- return true;
- }
- }
- if ($this->storeSerialized && null === $value = $this->freeze($value, $key)) {
- return false;
- }
- if (null === $expiry && 0 < $this->defaultLifetime) {
- $expiry = $this->defaultLifetime;
- $expiry = $now + ($expiry > ($this->maxLifetime ?: $expiry) ? $this->maxLifetime : $expiry);
- } elseif ($this->maxLifetime && (null === $expiry || $expiry > $now + $this->maxLifetime)) {
- $expiry = $now + $this->maxLifetime;
- }
- if ($this->maxItems) {
- unset($this->values[$key]);
-
- foreach ($this->values as $k => $v) {
- if ($this->expiries[$k] > $now && \count($this->values) < $this->maxItems) {
- break;
- }
- unset($this->values[$k], $this->expiries[$k]);
- }
- }
- $this->values[$key] = $value;
- $this->expiries[$key] = $expiry ?? \PHP_INT_MAX;
- return true;
- }
-
- public function saveDeferred(CacheItemInterface $item)
- {
- return $this->save($item);
- }
-
- public function commit()
- {
- return true;
- }
-
- public function clear(string $prefix = '')
- {
- if ('' !== $prefix) {
- $now = microtime(true);
- foreach ($this->values as $key => $value) {
- if (!isset($this->expiries[$key]) || $this->expiries[$key] <= $now || 0 === strpos($key, $prefix)) {
- unset($this->values[$key], $this->expiries[$key]);
- }
- }
- if ($this->values) {
- return true;
- }
- }
- $this->values = $this->expiries = [];
- return true;
- }
-
- public function getValues()
- {
- if (!$this->storeSerialized) {
- return $this->values;
- }
- $values = $this->values;
- foreach ($values as $k => $v) {
- if (null === $v || 'N;' === $v) {
- continue;
- }
- if (!\is_string($v) || !isset($v[2]) || ':' !== $v[1]) {
- $values[$k] = serialize($v);
- }
- }
- return $values;
- }
-
- public function reset()
- {
- $this->clear();
- }
- private function generateItems(array $keys, float $now, \Closure $f): \Generator
- {
- foreach ($keys as $i => $key) {
- if (!$isHit = isset($this->expiries[$key]) && ($this->expiries[$key] > $now || !$this->deleteItem($key))) {
- $value = null;
- if (!$this->maxItems) {
-
- $this->values[$key] = null;
- }
- } else {
- if ($this->maxItems) {
-
- $value = $this->values[$key];
- unset($this->values[$key]);
- $this->values[$key] = $value;
- }
- $value = $this->storeSerialized ? $this->unfreeze($key, $isHit) : $this->values[$key];
- }
- unset($keys[$i]);
- yield $key => $f($key, $value, $isHit);
- }
- foreach ($keys as $key) {
- yield $key => $f($key, null, false);
- }
- }
- private function freeze($value, string $key)
- {
- if (null === $value) {
- return 'N;';
- }
- if (\is_string($value)) {
-
- if ('N;' === $value || (isset($value[2]) && ':' === $value[1])) {
- return serialize($value);
- }
- } elseif (!\is_scalar($value)) {
- try {
- $serialized = serialize($value);
- } catch (\Exception $e) {
- unset($this->values[$key]);
- $type = get_debug_type($value);
- $message = sprintf('Failed to save key "{key}" of type %s: %s', $type, $e->getMessage());
- CacheItem::log($this->logger, $message, ['key' => $key, 'exception' => $e, 'cache-adapter' => get_debug_type($this)]);
- return;
- }
-
- if ('C' === $serialized[0] || 'O' === $serialized[0] || preg_match('/;[OCRr]:[1-9]/', $serialized)) {
- return $serialized;
- }
- }
- return $value;
- }
- private function unfreeze(string $key, bool &$isHit)
- {
- if ('N;' === $value = $this->values[$key]) {
- return null;
- }
- if (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
- try {
- $value = unserialize($value);
- } catch (\Exception $e) {
- CacheItem::log($this->logger, 'Failed to unserialize key "{key}": '.$e->getMessage(), ['key' => $key, 'exception' => $e, 'cache-adapter' => get_debug_type($this)]);
- $value = false;
- }
- if (false === $value) {
- $value = null;
- $isHit = false;
- if (!$this->maxItems) {
- $this->values[$key] = null;
- }
- }
- }
- return $value;
- }
- private function validateKeys(array $keys): bool
- {
- foreach ($keys as $key) {
- if (!\is_string($key) || !isset($this->expiries[$key])) {
- CacheItem::validateKey($key);
- }
- }
- return true;
- }
- }
|