This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Cache/Adapter/ArrayAdapter.php

194 lines
4.6 KiB
PHP
Raw Normal View History

2016-01-18 14:59:30 +00:00
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Cache\Adapter;
use Psr\Cache\CacheItemInterface;
2016-01-25 19:16:47 +00:00
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
2016-01-18 14:59:30 +00:00
use Symfony\Component\Cache\CacheItem;
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
2016-01-27 08:06:20 +00:00
class ArrayAdapter implements AdapterInterface, LoggerAwareInterface
2016-01-18 14:59:30 +00:00
{
2016-01-25 19:16:47 +00:00
use LoggerAwareTrait;
2016-02-02 15:39:10 +00:00
private $storeSerialized;
2016-01-18 14:59:30 +00:00
private $values = array();
private $expiries = array();
private $createCacheItem;
2016-02-02 15:39:10 +00:00
/**
* @param int $defaultLifetime
2016-06-29 06:42:25 +01:00
* @param bool $storeSerialized Disabling serialization can lead to cache corruptions when storing mutable values but increases performance otherwise
2016-02-02 15:39:10 +00:00
*/
public function __construct($defaultLifetime = 0, $storeSerialized = true)
2016-01-18 14:59:30 +00:00
{
2016-02-02 15:39:10 +00:00
$this->storeSerialized = $storeSerialized;
2016-01-18 14:59:30 +00:00
$this->createCacheItem = \Closure::bind(
function ($key, $value, $isHit) use ($defaultLifetime) {
$item = new CacheItem();
$item->key = $key;
$item->value = $value;
$item->isHit = $isHit;
$item->defaultLifetime = $defaultLifetime;
return $item;
},
null,
2016-01-18 14:59:30 +00:00
CacheItem::class
);
}
/**
* {@inheritdoc}
*/
public function getItem($key)
{
2016-02-02 15:39:10 +00:00
if (!$isHit = $this->hasItem($key)) {
$value = null;
} elseif ($this->storeSerialized) {
$value = unserialize($this->values[$key]);
} else {
$value = $this->values[$key];
}
2016-01-18 14:59:30 +00:00
$f = $this->createCacheItem;
2016-02-02 15:39:10 +00:00
return $f($key, $value, $isHit);
2016-01-18 14:59:30 +00:00
}
/**
* {@inheritdoc}
*/
public function getItems(array $keys = array())
{
foreach ($keys as $key) {
CacheItem::validateKey($key);
2016-01-18 14:59:30 +00:00
}
2016-02-03 09:28:28 +00:00
return $this->generateItems($keys, time());
2016-01-18 14:59:30 +00:00
}
/**
* {@inheritdoc}
*/
public function hasItem($key)
{
CacheItem::validateKey($key);
return isset($this->expiries[$key]) && ($this->expiries[$key] >= time() || !$this->deleteItem($key));
2016-01-18 14:59:30 +00:00
}
/**
* {@inheritdoc}
*/
public function clear()
{
$this->values = $this->expiries = array();
return true;
}
/**
* {@inheritdoc}
*/
public function deleteItem($key)
{
CacheItem::validateKey($key);
unset($this->values[$key], $this->expiries[$key]);
2016-01-18 14:59:30 +00:00
return true;
}
/**
* {@inheritdoc}
*/
public function deleteItems(array $keys)
{
foreach ($keys as $key) {
$this->deleteItem($key);
}
return true;
}
/**
* {@inheritdoc}
*/
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"];
2016-01-18 14:59:30 +00:00
2016-02-03 09:28:28 +00:00
if (null !== $expiry && $expiry <= time()) {
$this->deleteItem($key);
2016-01-18 14:59:30 +00:00
return true;
}
2016-02-02 15:39:10 +00:00
if ($this->storeSerialized) {
2016-01-18 14:59:30 +00:00
try {
2016-02-02 15:39:10 +00:00
$value = serialize($value);
2016-01-18 14:59:30 +00:00
} catch (\Exception $e) {
2016-01-25 19:16:47 +00:00
$type = is_object($value) ? get_class($value) : gettype($value);
2016-02-02 15:39:10 +00:00
CacheItem::log($this->logger, 'Failed to save key "{key}" ({type})', array('key' => $key, 'type' => $type, 'exception' => $e));
2016-01-18 14:59:30 +00:00
return false;
}
}
$this->values[$key] = $value;
2016-02-03 09:28:28 +00:00
$this->expiries[$key] = null !== $expiry ? $expiry : PHP_INT_MAX;
2016-01-18 14:59:30 +00:00
return true;
}
/**
* {@inheritdoc}
*/
public function saveDeferred(CacheItemInterface $item)
{
return $this->save($item);
}
/**
* {@inheritdoc}
*/
public function commit()
{
return true;
}
2016-02-03 09:28:28 +00:00
private function generateItems(array $keys, $now)
2016-01-19 09:13:56 +00:00
{
$f = $this->createCacheItem;
foreach ($keys as $key) {
2016-02-03 09:28:28 +00:00
if (!$isHit = isset($this->expiries[$key]) && ($this->expiries[$key] >= $now || !$this->deleteItem($key))) {
2016-02-02 15:39:10 +00:00
$value = null;
} elseif ($this->storeSerialized) {
$value = unserialize($this->values[$key]);
} else {
$value = $this->values[$key];
}
2016-01-19 09:13:56 +00:00
2016-02-02 15:39:10 +00:00
yield $key => $f($key, $value, $isHit);
2016-01-19 09:13:56 +00:00
}
}
2016-01-18 14:59:30 +00:00
}