123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- namespace Symfony\Component\Cache\Traits;
- use Psr\Log\LoggerAwareTrait;
- use Symfony\Component\Cache\CacheItem;
- trait ArrayTrait
- {
- use LoggerAwareTrait;
- private $storeSerialized;
- private $values = [];
- private $expiries = [];
-
- public function getValues()
- {
- return $this->values;
- }
-
- public function hasItem($key)
- {
- CacheItem::validateKey($key);
- return isset($this->expiries[$key]) && ($this->expiries[$key] > time() || !$this->deleteItem($key));
- }
-
- public function clear()
- {
- $this->values = $this->expiries = [];
- return true;
- }
-
- public function deleteItem($key)
- {
- CacheItem::validateKey($key);
- unset($this->values[$key], $this->expiries[$key]);
- return true;
- }
-
- public function reset()
- {
- $this->clear();
- }
- private function generateItems(array $keys, $now, $f)
- {
- foreach ($keys as $i => $key) {
- try {
- if (!$isHit = isset($this->expiries[$key]) && ($this->expiries[$key] > $now || !$this->deleteItem($key))) {
- $this->values[$key] = $value = null;
- } elseif (!$this->storeSerialized) {
- $value = $this->values[$key];
- } elseif ('b:0;' === $value = $this->values[$key]) {
- $value = false;
- } elseif (false === $value = unserialize($value)) {
- $this->values[$key] = $value = null;
- $isHit = false;
- }
- } catch (\Exception $e) {
- CacheItem::log($this->logger, 'Failed to unserialize key "{key}"', ['key' => $key, 'exception' => $e]);
- $this->values[$key] = $value = null;
- $isHit = false;
- }
- unset($keys[$i]);
- yield $key => $f($key, $value, $isHit);
- }
- foreach ($keys as $key) {
- yield $key => $f($key, null, false);
- }
- }
- }
|