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

156 lines
4.1 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;
2016-01-18 14:59:30 +00:00
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\ArrayTrait;
2016-01-18 14:59:30 +00:00
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class ArrayAdapter implements AdapterInterface, LoggerAwareInterface, ResettableInterface
2016-01-18 14:59:30 +00:00
{
use ArrayTrait;
2016-01-25 19:16:47 +00:00
2016-01-18 14:59:30 +00:00
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)
{
$isHit = $this->hasItem($key);
try {
if (!$isHit) {
$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)) {
2016-08-03 15:45:27 +01:00
$this->values[$key] = $value = null;
$isHit = false;
}
} catch (\Exception $e) {
CacheItem::log($this->logger, 'Failed to unserialize key "{key}"', array('key' => $key, 'exception' => $e));
$this->values[$key] = $value = null;
$isHit = false;
2016-02-02 15:39:10 +00:00
}
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
}
return $this->generateItems($keys, time(), $this->createCacheItem);
2016-01-18 14:59:30 +00:00
}
/**
* {@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;
}
}
if (null === $expiry && 0 < $item["\0*\0defaultLifetime"]) {
$expiry = time() + $item["\0*\0defaultLifetime"];
}
2016-01-18 14:59:30 +00:00
$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;
}
}