diff --git a/Adapter/AbstractAdapter.php b/Adapter/AbstractAdapter.php index ac6a5609..46c35760 100644 --- a/Adapter/AbstractAdapter.php +++ b/Adapter/AbstractAdapter.php @@ -11,63 +11,80 @@ namespace Symfony\Component\Cache\Adapter; -use Psr\Cache\CacheItemInterface; use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerInterface; use Psr\Log\NullLogger; use Symfony\Component\Cache\CacheItem; use Symfony\Component\Cache\Exception\InvalidArgumentException; use Symfony\Component\Cache\ResettableInterface; -use Symfony\Component\Cache\Traits\AbstractTrait; +use Symfony\Component\Cache\Traits\AbstractAdapterTrait; +use Symfony\Component\Cache\Traits\ContractsTrait; +use Symfony\Contracts\Cache\CacheInterface; /** * @author Nicolas Grekas
*/ -abstract class AbstractAdapter implements AdapterInterface, LoggerAwareInterface, ResettableInterface +abstract class AbstractAdapter implements AdapterInterface, CacheInterface, LoggerAwareInterface, ResettableInterface { - use AbstractTrait; + /** + * @internal + */ + protected const NS_SEPARATOR = ':'; + + use AbstractAdapterTrait; + use ContractsTrait; private static $apcuSupported; private static $phpFilesSupported; - private $createCacheItem; - private $mergeByLifetime; - protected function __construct(string $namespace = '', int $defaultLifetime = 0) { - $this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace).':'; + $this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace).static::NS_SEPARATOR; if (null !== $this->maxIdLength && \strlen($namespace) > $this->maxIdLength - 24) { throw new InvalidArgumentException(sprintf('Namespace must be %d chars max, %d given ("%s")', $this->maxIdLength - 24, \strlen($namespace), $namespace)); } $this->createCacheItem = \Closure::bind( - function ($key, $value, $isHit) use ($defaultLifetime) { + static function ($key, $value, $isHit) use ($defaultLifetime) { $item = new CacheItem(); $item->key = $key; - $item->value = $value; + $item->value = $v = $value; $item->isHit = $isHit; $item->defaultLifetime = $defaultLifetime; + // Detect wrapped values that encode for their expiry and creation duration + // For compactness, these values are packed in the key of an array using + // magic numbers in the form 9D-..-..-..-..-00-..-..-..-5F + if (\is_array($v) && 1 === \count($v) && 10 === \strlen($k = key($v)) && "\x9D" === $k[0] && "\0" === $k[5] && "\x5F" === $k[9]) { + $item->value = $v[$k]; + $v = unpack('Ve/Nc', substr($k, 1, -1)); + $item->metadata[CacheItem::METADATA_EXPIRY] = $v['e'] + CacheItem::METADATA_EXPIRY_OFFSET; + $item->metadata[CacheItem::METADATA_CTIME] = $v['c']; + } return $item; }, null, CacheItem::class ); - $getId = \Closure::fromCallable(array($this, 'getId')); + $getId = \Closure::fromCallable([$this, 'getId']); $this->mergeByLifetime = \Closure::bind( - function ($deferred, $namespace, &$expiredIds) use ($getId) { - $byLifetime = array(); - $now = time(); - $expiredIds = array(); + static function ($deferred, $namespace, &$expiredIds) use ($getId) { + $byLifetime = []; + $now = microtime(true); + $expiredIds = []; foreach ($deferred as $key => $item) { $key = (string) $key; if (null === $item->expiry) { - $byLifetime[0 < $item->defaultLifetime ? $item->defaultLifetime : 0][$getId($key)] = $item->value; - } elseif ($item->expiry > $now) { - $byLifetime[$item->expiry - $now][$getId($key)] = $item->value; - } else { + $ttl = 0 < $item->defaultLifetime ? $item->defaultLifetime : 0; + } elseif (0 >= $ttl = (int) (0.1 + $item->expiry - $now)) { $expiredIds[] = $getId($key); + continue; } + if (isset(($metadata = $item->newMetadata)[CacheItem::METADATA_TAGS])) { + unset($metadata[CacheItem::METADATA_TAGS]); + } + // For compactness, expiry and creation duration are packed in the key of an array, using magic numbers as separators + $byLifetime[$ttl][$getId($key)] = $metadata ? ["\x9D".pack('VN', (int) (0.1 + $metadata[self::METADATA_EXPIRY] - self::METADATA_EXPIRY_OFFSET), $metadata[self::METADATA_CTIME])."\x5F" => $item->value] : $item->value; } return $byLifetime; @@ -78,141 +95,53 @@ function ($deferred, $namespace, &$expiredIds) use ($getId) { } /** - * @param string $namespace - * @param int $defaultLifetime - * @param string $version - * @param string $directory - * @param LoggerInterface|null $logger + * Returns the best possible adapter that your runtime supports. + * + * Using ApcuAdapter makes system caches compatible with read-only filesystems. + * + * @param string $namespace + * @param int $defaultLifetime + * @param string $version + * @param string $directory * * @return AdapterInterface */ public static function createSystemCache($namespace, $defaultLifetime, $version, $directory, LoggerInterface $logger = null) { - if (null === self::$apcuSupported) { - self::$apcuSupported = ApcuAdapter::isSupported(); - } - - if (!self::$apcuSupported && null === self::$phpFilesSupported) { - self::$phpFilesSupported = PhpFilesAdapter::isSupported(); + $opcache = new PhpFilesAdapter($namespace, $defaultLifetime, $directory, true); + if (null !== $logger) { + $opcache->setLogger($logger); } - if (self::$phpFilesSupported) { - $opcache = new PhpFilesAdapter($namespace, $defaultLifetime, $directory); - if (null !== $logger) { - $opcache->setLogger($logger); - } - + if (!self::$apcuSupported = self::$apcuSupported ?? ApcuAdapter::isSupported()) { return $opcache; } - $fs = new FilesystemAdapter($namespace, $defaultLifetime, $directory); - if (null !== $logger) { - $fs->setLogger($logger); - } - if (!self::$apcuSupported) { - return $fs; - } - $apcu = new ApcuAdapter($namespace, (int) $defaultLifetime / 5, $version); - if ('cli' === \PHP_SAPI && !ini_get('apc.enable_cli')) { + if ('cli' === \PHP_SAPI && !filter_var(ini_get('apc.enable_cli'), FILTER_VALIDATE_BOOLEAN)) { $apcu->setLogger(new NullLogger()); } elseif (null !== $logger) { $apcu->setLogger($logger); } - return new ChainAdapter(array($apcu, $fs)); + return new ChainAdapter([$apcu, $opcache]); } - public static function createConnection($dsn, array $options = array()) + public static function createConnection($dsn, array $options = []) { if (!\is_string($dsn)) { throw new InvalidArgumentException(sprintf('The %s() method expect argument #1 to be string, %s given.', __METHOD__, \gettype($dsn))); } - if (0 === strpos($dsn, 'redis://')) { + if (0 === strpos($dsn, 'redis:') || 0 === strpos($dsn, 'rediss:')) { return RedisAdapter::createConnection($dsn, $options); } - if (0 === strpos($dsn, 'memcached://')) { + if (0 === strpos($dsn, 'memcached:')) { return MemcachedAdapter::createConnection($dsn, $options); } throw new InvalidArgumentException(sprintf('Unsupported DSN: %s.', $dsn)); } - /** - * {@inheritdoc} - */ - public function getItem($key) - { - if ($this->deferred) { - $this->commit(); - } - $id = $this->getId($key); - - $f = $this->createCacheItem; - $isHit = false; - $value = null; - - try { - foreach ($this->doFetch(array($id)) as $value) { - $isHit = true; - } - } catch (\Exception $e) { - CacheItem::log($this->logger, 'Failed to fetch key "{key}"', array('key' => $key, 'exception' => $e)); - } - - return $f($key, $value, $isHit); - } - - /** - * {@inheritdoc} - */ - public function getItems(array $keys = array()) - { - if ($this->deferred) { - $this->commit(); - } - $ids = array(); - - foreach ($keys as $key) { - $ids[] = $this->getId($key); - } - try { - $items = $this->doFetch($ids); - } catch (\Exception $e) { - CacheItem::log($this->logger, 'Failed to fetch requested items', array('keys' => $keys, 'exception' => $e)); - $items = array(); - } - $ids = array_combine($ids, $keys); - - return $this->generateItems($items, $ids); - } - - /** - * {@inheritdoc} - */ - public function save(CacheItemInterface $item) - { - if (!$item instanceof CacheItem) { - return false; - } - $this->deferred[$item->getKey()] = $item; - - return $this->commit(); - } - - /** - * {@inheritdoc} - */ - public function saveDeferred(CacheItemInterface $item) - { - if (!$item instanceof CacheItem) { - return false; - } - $this->deferred[$item->getKey()] = $item; - - return true; - } - /** * {@inheritdoc} */ @@ -221,7 +150,7 @@ public function commit() $ok = true; $byLifetime = $this->mergeByLifetime; $byLifetime = $byLifetime($this->deferred, $this->namespace, $expiredIds); - $retry = $this->deferred = array(); + $retry = $this->deferred = []; if ($expiredIds) { $this->doDelete($expiredIds); @@ -231,7 +160,7 @@ public function commit() $e = $this->doSave($values, $lifetime); } catch (\Exception $e) { } - if (true === $e || array() === $e) { + if (true === $e || [] === $e) { continue; } if (\is_array($e) || 1 === \count($values)) { @@ -239,7 +168,8 @@ public function commit() $ok = false; $v = $values[$id]; $type = \is_object($v) ? \get_class($v) : \gettype($v); - CacheItem::log($this->logger, 'Failed to save key "{key}" ({type})', array('key' => substr($id, \strlen($this->namespace)), 'type' => $type, 'exception' => $e instanceof \Exception ? $e : null)); + $message = sprintf('Failed to save key "{key}" of type %s%s', $type, $e instanceof \Exception ? ': '.$e->getMessage() : '.'); + CacheItem::log($this->logger, $message, ['key' => substr($id, \strlen($this->namespace)), 'exception' => $e instanceof \Exception ? $e : null]); } } else { foreach ($values as $id => $v) { @@ -253,47 +183,19 @@ public function commit() foreach ($ids as $id) { try { $v = $byLifetime[$lifetime][$id]; - $e = $this->doSave(array($id => $v), $lifetime); + $e = $this->doSave([$id => $v], $lifetime); } catch (\Exception $e) { } - if (true === $e || array() === $e) { + if (true === $e || [] === $e) { continue; } $ok = false; $type = \is_object($v) ? \get_class($v) : \gettype($v); - CacheItem::log($this->logger, 'Failed to save key "{key}" ({type})', array('key' => substr($id, \strlen($this->namespace)), 'type' => $type, 'exception' => $e instanceof \Exception ? $e : null)); + $message = sprintf('Failed to save key "{key}" of type %s%s', $type, $e instanceof \Exception ? ': '.$e->getMessage() : '.'); + CacheItem::log($this->logger, $message, ['key' => substr($id, \strlen($this->namespace)), 'exception' => $e instanceof \Exception ? $e : null]); } } return $ok; } - - public function __destruct() - { - if ($this->deferred) { - $this->commit(); - } - } - - private function generateItems($items, &$keys) - { - $f = $this->createCacheItem; - - try { - foreach ($items as $id => $value) { - if (!isset($keys[$id])) { - $id = key($keys); - } - $key = $keys[$id]; - unset($keys[$id]); - yield $key => $f($key, $value, true); - } - } catch (\Exception $e) { - CacheItem::log($this->logger, 'Failed to fetch requested items', array('keys' => array_values($keys), 'exception' => $e)); - } - - foreach ($keys as $key) { - yield $key => $f($key, null, false); - } - } } diff --git a/Adapter/AbstractTagAwareAdapter.php b/Adapter/AbstractTagAwareAdapter.php new file mode 100644 index 00000000..13a4968a --- /dev/null +++ b/Adapter/AbstractTagAwareAdapter.php @@ -0,0 +1,309 @@ + + * + * 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\Log\LoggerAwareInterface; +use Symfony\Component\Cache\CacheItem; +use Symfony\Component\Cache\Exception\InvalidArgumentException; +use Symfony\Component\Cache\ResettableInterface; +use Symfony\Component\Cache\Traits\AbstractAdapterTrait; +use Symfony\Component\Cache\Traits\ContractsTrait; +use Symfony\Contracts\Cache\TagAwareCacheInterface; + +/** + * Abstract for native TagAware adapters. + * + * To keep info on tags, the tags are both serialized as part of cache value and provided as tag ids + * to Adapters on operations when needed for storage to doSave(), doDelete() & doInvalidate(). + * + * @author Nicolas Grekas
+ * @author André Rømcke
*/
-class ArrayAdapter implements AdapterInterface, LoggerAwareInterface, ResettableInterface
+class ArrayAdapter implements AdapterInterface, CacheInterface, LoggerAwareInterface, ResettableInterface
{
use ArrayTrait;
private $createCacheItem;
/**
- * @param int $defaultLifetime
* @param bool $storeSerialized Disabling serialization can lead to cache corruptions when storing mutable values but increases performance otherwise
*/
public function __construct(int $defaultLifetime = 0, bool $storeSerialized = true)
{
$this->storeSerialized = $storeSerialized;
$this->createCacheItem = \Closure::bind(
- function ($key, $value, $isHit) use ($defaultLifetime) {
+ static function ($key, $value, $isHit) use ($defaultLifetime) {
$item = new CacheItem();
$item->key = $key;
$item->value = $value;
@@ -48,27 +48,32 @@ function ($key, $value, $isHit) use ($defaultLifetime) {
);
}
+ /**
+ * {@inheritdoc}
+ */
+ public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
+ {
+ $item = $this->getItem($key);
+ $metadata = $item->getMetadata();
+
+ // ArrayAdapter works in memory, we don't care about stampede protection
+ if (INF === $beta || !$item->isHit()) {
+ $save = true;
+ $this->save($item->set($callback($item, $save)));
+ }
+
+ return $item->get();
+ }
+
/**
* {@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)) {
- $this->values[$key] = $value = null;
- $isHit = false;
- }
- } catch (\Exception $e) {
- CacheItem::log($this->logger, 'Failed to unserialize key "{key}"', array('key' => $key, 'exception' => $e));
+ if (!$isHit = $this->hasItem($key)) {
$this->values[$key] = $value = null;
- $isHit = false;
+ } else {
+ $value = $this->storeSerialized ? $this->unfreeze($key, $isHit) : $this->values[$key];
}
$f = $this->createCacheItem;
@@ -78,13 +83,15 @@ public function getItem($key)
/**
* {@inheritdoc}
*/
- public function getItems(array $keys = array())
+ public function getItems(array $keys = [])
{
foreach ($keys as $key) {
- CacheItem::validateKey($key);
+ if (!\is_string($key) || !isset($this->expiries[$key])) {
+ CacheItem::validateKey($key);
+ }
}
- return $this->generateItems($keys, time(), $this->createCacheItem);
+ return $this->generateItems($keys, microtime(true), $this->createCacheItem);
}
/**
@@ -112,23 +119,16 @@ public function save(CacheItemInterface $item)
$value = $item["\0*\0value"];
$expiry = $item["\0*\0expiry"];
- if (null !== $expiry && $expiry <= time()) {
+ if (null !== $expiry && $expiry <= microtime(true)) {
$this->deleteItem($key);
return true;
}
- if ($this->storeSerialized) {
- try {
- $value = serialize($value);
- } catch (\Exception $e) {
- $type = \is_object($value) ? \get_class($value) : \gettype($value);
- CacheItem::log($this->logger, 'Failed to save key "{key}" ({type})', array('key' => $key, 'type' => $type, 'exception' => $e));
-
- return false;
- }
+ if ($this->storeSerialized && null === $value = $this->freeze($value, $key)) {
+ return false;
}
if (null === $expiry && 0 < $item["\0*\0defaultLifetime"]) {
- $expiry = time() + $item["\0*\0defaultLifetime"];
+ $expiry = microtime(true) + $item["\0*\0defaultLifetime"];
}
$this->values[$key] = $value;
@@ -152,4 +152,12 @@ public function commit()
{
return true;
}
+
+ /**
+ * {@inheritdoc}
+ */
+ public function delete(string $key): bool
+ {
+ return $this->deleteItem($key);
+ }
}
diff --git a/Adapter/ChainAdapter.php b/Adapter/ChainAdapter.php
index db0da4fb..0217c801 100644
--- a/Adapter/ChainAdapter.php
+++ b/Adapter/ChainAdapter.php
@@ -17,6 +17,9 @@
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
+use Symfony\Component\Cache\Traits\ContractsTrait;
+use Symfony\Contracts\Cache\CacheInterface;
+use Symfony\Contracts\Service\ResetInterface;
/**
* Chains several adapters together.
@@ -26,9 +29,11 @@
*
* @author Kévin Dunglas
+ * @author André Rømcke
*/
-class PhpArrayAdapter implements AdapterInterface, PruneableInterface, ResettableInterface
+class PhpArrayAdapter implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
{
use PhpArrayTrait;
+ use ContractsTrait;
private $createCacheItem;
@@ -40,9 +43,8 @@ public function __construct(string $file, AdapterInterface $fallbackPool)
{
$this->file = $file;
$this->pool = $fallbackPool;
- $this->zendDetectUnicode = ini_get('zend.detect_unicode');
$this->createCacheItem = \Closure::bind(
- function ($key, $value, $isHit) {
+ static function ($key, $value, $isHit) {
$item = new CacheItem();
$item->key = $key;
$item->value = $value;
@@ -66,7 +68,7 @@ function ($key, $value, $isHit) {
public static function create($file, CacheItemPoolInterface $fallbackPool)
{
// Shared memory is available in PHP 7.0+ with OPCache enabled
- if (ini_get('opcache.enable')) {
+ if (filter_var(ini_get('opcache.enable'), FILTER_VALIDATE_BOOLEAN)) {
if (!$fallbackPool instanceof AdapterInterface) {
$fallbackPool = new ProxyAdapter($fallbackPool);
}
@@ -77,6 +79,39 @@ public static function create($file, CacheItemPoolInterface $fallbackPool)
return $fallbackPool;
}
+ /**
+ * {@inheritdoc}
+ */
+ public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
+ {
+ if (null === $this->values) {
+ $this->initialize();
+ }
+ if (!isset($this->keys[$key])) {
+ get_from_pool:
+ if ($this->pool instanceof CacheInterface) {
+ return $this->pool->get($key, $callback, $beta, $metadata);
+ }
+
+ return $this->doGet($this->pool, $key, $callback, $beta, $metadata);
+ }
+ $value = $this->values[$this->keys[$key]];
+
+ if ('N;' === $value) {
+ return null;
+ }
+ try {
+ if ($value instanceof \Closure) {
+ return $value();
+ }
+ } catch (\Throwable $e) {
+ unset($this->keys[$key]);
+ goto get_from_pool;
+ }
+
+ return $value;
+ }
+
/**
* {@inheritdoc}
*/
@@ -88,18 +123,18 @@ public function getItem($key)
if (null === $this->values) {
$this->initialize();
}
- if (!isset($this->values[$key])) {
+ if (!isset($this->keys[$key])) {
return $this->pool->getItem($key);
}
- $value = $this->values[$key];
+ $value = $this->values[$this->keys[$key]];
$isHit = true;
if ('N;' === $value) {
$value = null;
- } elseif (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
+ } elseif ($value instanceof \Closure) {
try {
- $value = unserialize($value);
+ $value = $value();
} catch (\Throwable $e) {
$value = null;
$isHit = false;
@@ -114,7 +149,7 @@ public function getItem($key)
/**
* {@inheritdoc}
*/
- public function getItems(array $keys = array())
+ public function getItems(array $keys = [])
{
foreach ($keys as $key) {
if (!\is_string($key)) {
@@ -140,7 +175,7 @@ public function hasItem($key)
$this->initialize();
}
- return isset($this->values[$key]) || $this->pool->hasItem($key);
+ return isset($this->keys[$key]) || $this->pool->hasItem($key);
}
/**
@@ -155,7 +190,7 @@ public function deleteItem($key)
$this->initialize();
}
- return !isset($this->values[$key]) && $this->pool->deleteItem($key);
+ return !isset($this->keys[$key]) && $this->pool->deleteItem($key);
}
/**
@@ -164,14 +199,14 @@ public function deleteItem($key)
public function deleteItems(array $keys)
{
$deleted = true;
- $fallbackKeys = array();
+ $fallbackKeys = [];
foreach ($keys as $key) {
if (!\is_string($key)) {
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', \is_object($key) ? \get_class($key) : \gettype($key)));
}
- if (isset($this->values[$key])) {
+ if (isset($this->keys[$key])) {
$deleted = false;
} else {
$fallbackKeys[] = $key;
@@ -197,7 +232,7 @@ public function save(CacheItemInterface $item)
$this->initialize();
}
- return !isset($this->values[$item->getKey()]) && $this->pool->save($item);
+ return !isset($this->keys[$item->getKey()]) && $this->pool->save($item);
}
/**
@@ -209,7 +244,7 @@ public function saveDeferred(CacheItemInterface $item)
$this->initialize();
}
- return !isset($this->values[$item->getKey()]) && $this->pool->saveDeferred($item);
+ return !isset($this->keys[$item->getKey()]) && $this->pool->saveDeferred($item);
}
/**
@@ -223,17 +258,17 @@ public function commit()
private function generateItems(array $keys): \Generator
{
$f = $this->createCacheItem;
- $fallbackKeys = array();
+ $fallbackKeys = [];
foreach ($keys as $key) {
- if (isset($this->values[$key])) {
- $value = $this->values[$key];
+ if (isset($this->keys[$key])) {
+ $value = $this->values[$this->keys[$key]];
if ('N;' === $value) {
yield $key => $f($key, null, true);
- } elseif (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
+ } elseif ($value instanceof \Closure) {
try {
- yield $key => $f($key, unserialize($value), true);
+ yield $key => $f($key, $value(), true);
} catch (\Throwable $e) {
yield $key => $f($key, null, false);
}
@@ -246,25 +281,23 @@ private function generateItems(array $keys): \Generator
}
if ($fallbackKeys) {
- foreach ($this->pool->getItems($fallbackKeys) as $key => $item) {
- yield $key => $item;
- }
+ yield from $this->pool->getItems($fallbackKeys);
}
}
/**
* @throws \ReflectionException When $class is not found and is required
*
- * @internal
+ * @internal to be removed in Symfony 5.0
*/
public static function throwOnRequiredClass($class)
{
$e = new \ReflectionException("Class $class does not exist");
- $trace = $e->getTrace();
- $autoloadFrame = array(
+ $trace = debug_backtrace();
+ $autoloadFrame = [
'function' => 'spl_autoload_call',
- 'args' => array($class),
- );
+ 'args' => [$class],
+ ];
$i = 1 + array_search($autoloadFrame, $trace, true);
if (isset($trace[$i]['function']) && !isset($trace[$i]['class'])) {
diff --git a/Adapter/PhpFilesAdapter.php b/Adapter/PhpFilesAdapter.php
index 41879df2..10938a0a 100644
--- a/Adapter/PhpFilesAdapter.php
+++ b/Adapter/PhpFilesAdapter.php
@@ -20,18 +20,19 @@ class PhpFilesAdapter extends AbstractAdapter implements PruneableInterface
use PhpFilesTrait;
/**
+ * @param $appendOnly Set to `true` to gain extra performance when the items stored in this pool never expire.
+ * Doing so is encouraged because it fits perfectly OPcache's memory model.
+ *
* @throws CacheException if OPcache is not enabled
*/
- public function __construct(string $namespace = '', int $defaultLifetime = 0, string $directory = null)
+ public function __construct(string $namespace = '', int $defaultLifetime = 0, string $directory = null, bool $appendOnly = false)
{
- if (!static::isSupported()) {
- throw new CacheException('OPcache is not enabled');
- }
+ $this->appendOnly = $appendOnly;
+ self::$startTime = self::$startTime ?? $_SERVER['REQUEST_TIME'] ?? time();
parent::__construct('', $defaultLifetime);
$this->init($namespace, $directory);
-
- $e = new \Exception();
- $this->includeHandler = function () use ($e) { throw $e; };
- $this->zendDetectUnicode = ini_get('zend.detect_unicode');
+ $this->includeHandler = static function ($type, $msg, $file, $line) {
+ throw new \ErrorException($msg, 0, $type, $file, $line);
+ };
}
}
diff --git a/Adapter/ProxyAdapter.php b/Adapter/ProxyAdapter.php
index 0d4fcc47..ffae53e8 100644
--- a/Adapter/ProxyAdapter.php
+++ b/Adapter/ProxyAdapter.php
@@ -16,18 +16,22 @@
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
+use Symfony\Component\Cache\Traits\ContractsTrait;
use Symfony\Component\Cache\Traits\ProxyTrait;
+use Symfony\Contracts\Cache\CacheInterface;
/**
* @author Nicolas Grekas
*/
-class ProxyAdapter implements AdapterInterface, PruneableInterface, ResettableInterface
+class ProxyAdapter implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
{
use ProxyTrait;
+ use ContractsTrait;
private $namespace;
private $namespaceLen;
private $createCacheItem;
+ private $setInnerItem;
private $poolHash;
public function __construct(CacheItemPoolInterface $pool, string $namespace = '', int $defaultLifetime = 0)
@@ -37,14 +41,31 @@ public function __construct(CacheItemPoolInterface $pool, string $namespace = ''
$this->namespace = '' === $namespace ? '' : CacheItem::validateKey($namespace);
$this->namespaceLen = \strlen($namespace);
$this->createCacheItem = \Closure::bind(
- function ($key, $innerItem) use ($defaultLifetime, $poolHash) {
+ static function ($key, $innerItem) use ($defaultLifetime, $poolHash) {
$item = new CacheItem();
$item->key = $key;
- $item->value = $innerItem->get();
+
+ if (null === $innerItem) {
+ return $item;
+ }
+
+ $item->value = $v = $innerItem->get();
$item->isHit = $innerItem->isHit();
- $item->defaultLifetime = $defaultLifetime;
$item->innerItem = $innerItem;
+ $item->defaultLifetime = $defaultLifetime;
$item->poolHash = $poolHash;
+
+ // Detect wrapped values that encode for their expiry and creation duration
+ // For compactness, these values are packed in the key of an array using
+ // magic numbers in the form 9D-..-..-..-..-00-..-..-..-5F
+ if (\is_array($v) && 1 === \count($v) && 10 === \strlen($k = key($v)) && "\x9D" === $k[0] && "\0" === $k[5] && "\x5F" === $k[9]) {
+ $item->value = $v[$k];
+ $v = unpack('Ve/Nc', substr($k, 1, -1));
+ $item->metadata[CacheItem::METADATA_EXPIRY] = $v['e'] + CacheItem::METADATA_EXPIRY_OFFSET;
+ $item->metadata[CacheItem::METADATA_CTIME] = $v['c'];
+ } elseif ($innerItem instanceof CacheItem) {
+ $item->metadata = $innerItem->metadata;
+ }
$innerItem->set(null);
return $item;
@@ -52,6 +73,43 @@ function ($key, $innerItem) use ($defaultLifetime, $poolHash) {
null,
CacheItem::class
);
+ $this->setInnerItem = \Closure::bind(
+ /**
+ * @param array $item A CacheItem cast to (array); accessing protected properties requires adding the "\0*\0" PHP prefix
+ */
+ static function (CacheItemInterface $innerItem, array $item) {
+ // Tags are stored separately, no need to account for them when considering this item's newly set metadata
+ if (isset(($metadata = $item["\0*\0newMetadata"])[CacheItem::METADATA_TAGS])) {
+ unset($metadata[CacheItem::METADATA_TAGS]);
+ }
+ if ($metadata) {
+ // For compactness, expiry and creation duration are packed in the key of an array, using magic numbers as separators
+ $item["\0*\0value"] = ["\x9D".pack('VN', (int) (0.1 + $metadata[self::METADATA_EXPIRY] - self::METADATA_EXPIRY_OFFSET), $metadata[self::METADATA_CTIME])."\x5F" => $item["\0*\0value"]];
+ }
+ $innerItem->set($item["\0*\0value"]);
+ $innerItem->expiresAt(null !== $item["\0*\0expiry"] ? \DateTime::createFromFormat('U.u', sprintf('%.6f', $item["\0*\0expiry"])) : null);
+ },
+ null,
+ CacheItem::class
+ );
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
+ {
+ if (!$this->pool instanceof CacheInterface) {
+ return $this->doGet($this, $key, $callback, $beta, $metadata);
+ }
+
+ return $this->pool->get($this->getId($key), function ($innerItem, bool &$save) use ($key, $callback) {
+ $item = ($this->createCacheItem)($key, $innerItem);
+ $item->set($value = $callback($item, $save));
+ ($this->setInnerItem)($innerItem, (array) $item);
+
+ return $value;
+ }, $beta, $metadata);
}
/**
@@ -68,7 +126,7 @@ public function getItem($key)
/**
* {@inheritdoc}
*/
- public function getItems(array $keys = array())
+ public function getItems(array $keys = [])
{
if ($this->namespaceLen) {
foreach ($keys as $i => $key) {
@@ -147,13 +205,22 @@ private function doSave(CacheItemInterface $item, $method)
return false;
}
$item = (array) $item;
- $expiry = $item["\0*\0expiry"];
- if (null === $expiry && 0 < $item["\0*\0defaultLifetime"]) {
- $expiry = time() + $item["\0*\0defaultLifetime"];
+ if (null === $item["\0*\0expiry"] && 0 < $item["\0*\0defaultLifetime"]) {
+ $item["\0*\0expiry"] = microtime(true) + $item["\0*\0defaultLifetime"];
}
- $innerItem = $item["\0*\0poolHash"] === $this->poolHash ? $item["\0*\0innerItem"] : $this->pool->getItem($this->namespace.$item["\0*\0key"]);
- $innerItem->set($item["\0*\0value"]);
- $innerItem->expiresAt(null !== $expiry ? \DateTime::createFromFormat('U', $expiry) : null);
+
+ if ($item["\0*\0poolHash"] === $this->poolHash && $item["\0*\0innerItem"]) {
+ $innerItem = $item["\0*\0innerItem"];
+ } elseif ($this->pool instanceof AdapterInterface) {
+ // this is an optimization specific for AdapterInterface implementations
+ // so we can save a round-trip to the backend by just creating a new item
+ $f = $this->createCacheItem;
+ $innerItem = $f($this->namespace.$item["\0*\0key"], null);
+ } else {
+ $innerItem = $this->pool->getItem($this->namespace.$item["\0*\0key"]);
+ }
+
+ ($this->setInnerItem)($innerItem, $item);
return $this->pool->$method($innerItem);
}
diff --git a/Adapter/Psr16Adapter.php b/Adapter/Psr16Adapter.php
new file mode 100644
index 00000000..bb388710
--- /dev/null
+++ b/Adapter/Psr16Adapter.php
@@ -0,0 +1,86 @@
+
+ *
+ * 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\SimpleCache\CacheInterface;
+use Symfony\Component\Cache\PruneableInterface;
+use Symfony\Component\Cache\ResettableInterface;
+use Symfony\Component\Cache\Traits\ProxyTrait;
+
+/**
+ * Turns a PSR-16 cache into a PSR-6 one.
+ *
+ * @author Nicolas Grekas
+ */
+class Psr16Adapter extends AbstractAdapter implements PruneableInterface, ResettableInterface
+{
+ /**
+ * @internal
+ */
+ protected const NS_SEPARATOR = '_';
+
+ use ProxyTrait;
+
+ private $miss;
+
+ public function __construct(CacheInterface $pool, string $namespace = '', int $defaultLifetime = 0)
+ {
+ parent::__construct($namespace, $defaultLifetime);
+
+ $this->pool = $pool;
+ $this->miss = new \stdClass();
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function doFetch(array $ids)
+ {
+ foreach ($this->pool->getMultiple($ids, $this->miss) as $key => $value) {
+ if ($this->miss !== $value) {
+ yield $key => $value;
+ }
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function doHave($id)
+ {
+ return $this->pool->has($id);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function doClear($namespace)
+ {
+ return $this->pool->clear();
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function doDelete(array $ids)
+ {
+ return $this->pool->deleteMultiple($ids);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function doSave(array $values, $lifetime)
+ {
+ return $this->pool->setMultiple($values, 0 === $lifetime ? null : $lifetime);
+ }
+}
diff --git a/Adapter/RedisAdapter.php b/Adapter/RedisAdapter.php
index 0bb76fcd..5c49f7af 100644
--- a/Adapter/RedisAdapter.php
+++ b/Adapter/RedisAdapter.php
@@ -11,6 +11,7 @@
namespace Symfony\Component\Cache\Adapter;
+use Symfony\Component\Cache\Marshaller\MarshallerInterface;
use Symfony\Component\Cache\Traits\RedisTrait;
class RedisAdapter extends AbstractAdapter
@@ -18,12 +19,12 @@ class RedisAdapter extends AbstractAdapter
use RedisTrait;
/**
- * @param \Redis|\RedisArray|\RedisCluster|\Predis\Client $redisClient The redis client
- * @param string $namespace The default namespace
- * @param int $defaultLifetime The default lifetime
+ * @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface $redisClient The redis client
+ * @param string $namespace The default namespace
+ * @param int $defaultLifetime The default lifetime
*/
- public function __construct($redisClient, string $namespace = '', int $defaultLifetime = 0)
+ public function __construct($redisClient, string $namespace = '', int $defaultLifetime = 0, MarshallerInterface $marshaller = null)
{
- $this->init($redisClient, $namespace, $defaultLifetime);
+ $this->init($redisClient, $namespace, $defaultLifetime, $marshaller);
}
}
diff --git a/Adapter/RedisTagAwareAdapter.php b/Adapter/RedisTagAwareAdapter.php
new file mode 100644
index 00000000..b83b09b0
--- /dev/null
+++ b/Adapter/RedisTagAwareAdapter.php
@@ -0,0 +1,212 @@
+
+ *
+ * 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 Predis;
+use Predis\Connection\Aggregate\ClusterInterface;
+use Predis\Response\Status;
+use Symfony\Component\Cache\CacheItem;
+use Symfony\Component\Cache\Exception\LogicException;
+use Symfony\Component\Cache\Marshaller\MarshallerInterface;
+use Symfony\Component\Cache\Traits\RedisTrait;
+
+/**
+ * Stores tag id <> cache id relationship as a Redis Set, lookup on invalidation using sPOP.
+ *
+ * Set (tag relation info) is stored without expiry (non-volatile), while cache always gets an expiry (volatile) even
+ * if not set by caller. Thus if you configure redis with the right eviction policy you can be safe this tag <> cache
+ * relationship survives eviction (cache cleanup when Redis runs out of memory).
+ *
+ * Requirements:
+ * - Server: Redis 3.2+
+ * - Client: PHP Redis 3.1.3+ OR Predis
+ * - Redis Server(s) configured with any `volatile-*` eviction policy, OR `noeviction` if it will NEVER fill up memory
+ *
+ * Design limitations:
+ * - Max 2 billion cache keys per cache tag
+ * E.g. If you use a "all" items tag for expiry instead of clear(), that limits you to 2 billion cache items as well
+ *
+ * @see https://redis.io/topics/lru-cache#eviction-policies Documentation for Redis eviction policies.
+ * @see https://redis.io/topics/data-types#sets Documentation for Redis Set datatype.
+ * @see https://redis.io/commands/spop Documentation for sPOP operation, capable of retriving AND emptying a Set at once.
+ *
+ * @author Nicolas Grekas
+ * @author André Rømcke
+ * @deprecated since Symfony 4.3, use Psr16Adapter instead.
*/
-class SimpleCacheAdapter extends AbstractAdapter implements PruneableInterface, ResettableInterface
+class SimpleCacheAdapter extends Psr16Adapter
{
- use ProxyTrait;
-
- private $miss;
-
- public function __construct(CacheInterface $pool, string $namespace = '', int $defaultLifetime = 0)
- {
- parent::__construct($namespace, $defaultLifetime);
-
- $this->pool = $pool;
- $this->miss = new \stdClass();
- }
-
- /**
- * {@inheritdoc}
- */
- protected function doFetch(array $ids)
- {
- foreach ($this->pool->getMultiple($ids, $this->miss) as $key => $value) {
- if ($this->miss !== $value) {
- yield $key => $value;
- }
- }
- }
-
- /**
- * {@inheritdoc}
- */
- protected function doHave($id)
- {
- return $this->pool->has($id);
- }
-
- /**
- * {@inheritdoc}
- */
- protected function doClear($namespace)
- {
- return $this->pool->clear();
- }
-
- /**
- * {@inheritdoc}
- */
- protected function doDelete(array $ids)
- {
- return $this->pool->deleteMultiple($ids);
- }
-
- /**
- * {@inheritdoc}
- */
- protected function doSave(array $values, $lifetime)
- {
- return $this->pool->setMultiple($values, 0 === $lifetime ? null : $lifetime);
- }
}
diff --git a/Adapter/TagAwareAdapter.php b/Adapter/TagAwareAdapter.php
index 9110511b..72fc526a 100644
--- a/Adapter/TagAwareAdapter.php
+++ b/Adapter/TagAwareAdapter.php
@@ -16,24 +16,27 @@
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
+use Symfony\Component\Cache\Traits\ContractsTrait;
use Symfony\Component\Cache\Traits\ProxyTrait;
+use Symfony\Contracts\Cache\TagAwareCacheInterface;
/**
* @author Nicolas Grekas
*/
-class TagAwareAdapter implements TagAwareAdapterInterface, PruneableInterface, ResettableInterface
+class TagAwareAdapter implements TagAwareAdapterInterface, TagAwareCacheInterface, PruneableInterface, ResettableInterface
{
const TAGS_PREFIX = "\0tags\0";
use ProxyTrait;
+ use ContractsTrait;
- private $deferred = array();
+ private $deferred = [];
private $createCacheItem;
private $setCacheItemTags;
private $getTagsByKey;
private $invalidateTags;
private $tags;
- private $knownTagVersions = array();
+ private $knownTagVersions = [];
private $knownTagVersionsTtl;
public function __construct(AdapterInterface $itemsPool, AdapterInterface $tagsPool = null, float $knownTagVersionsTtl = 0.15)
@@ -42,13 +45,12 @@ public function __construct(AdapterInterface $itemsPool, AdapterInterface $tagsP
$this->tags = $tagsPool ?: $itemsPool;
$this->knownTagVersionsTtl = $knownTagVersionsTtl;
$this->createCacheItem = \Closure::bind(
- function ($key, $value, CacheItem $protoItem) {
+ static function ($key, $value, CacheItem $protoItem) {
$item = new CacheItem();
$item->key = $key;
$item->value = $value;
$item->defaultLifetime = $protoItem->defaultLifetime;
$item->expiry = $protoItem->expiry;
- $item->innerItem = $protoItem->innerItem;
$item->poolHash = $protoItem->poolHash;
return $item;
@@ -57,13 +59,14 @@ function ($key, $value, CacheItem $protoItem) {
CacheItem::class
);
$this->setCacheItemTags = \Closure::bind(
- function (CacheItem $item, $key, array &$itemTags) {
+ static function (CacheItem $item, $key, array &$itemTags) {
+ $item->isTaggable = true;
if (!$item->isHit) {
return $item;
}
if (isset($itemTags[$key])) {
foreach ($itemTags[$key] as $tag => $version) {
- $item->prevTags[$tag] = $tag;
+ $item->metadata[CacheItem::METADATA_TAGS][$tag] = $tag;
}
unset($itemTags[$key]);
} else {
@@ -77,10 +80,10 @@ function (CacheItem $item, $key, array &$itemTags) {
CacheItem::class
);
$this->getTagsByKey = \Closure::bind(
- function ($deferred) {
- $tagsByKey = array();
+ static function ($deferred) {
+ $tagsByKey = [];
foreach ($deferred as $key => $item) {
- $tagsByKey[$key] = $item->tags;
+ $tagsByKey[$key] = $item->newMetadata[CacheItem::METADATA_TAGS] ?? [];
}
return $tagsByKey;
@@ -89,7 +92,7 @@ function ($deferred) {
CacheItem::class
);
$this->invalidateTags = \Closure::bind(
- function (AdapterInterface $tagsAdapter, array $tags) {
+ static function (AdapterInterface $tagsAdapter, array $tags) {
foreach ($tags as $v) {
$v->defaultLifetime = 0;
$v->expiry = null;
@@ -109,8 +112,8 @@ function (AdapterInterface $tagsAdapter, array $tags) {
public function invalidateTags(array $tags)
{
$ok = true;
- $tagsByKey = array();
- $invalidatedTags = array();
+ $tagsByKey = [];
+ $invalidatedTags = [];
foreach ($tags as $tag) {
CacheItem::validateKey($tag);
$invalidatedTags[$tag] = 0;
@@ -127,7 +130,7 @@ public function invalidateTags(array $tags)
$f = $this->getTagsByKey;
$tagsByKey = $f($items);
- $this->deferred = array();
+ $this->deferred = [];
}
$tagVersions = $this->getTagVersions($tagsByKey, $invalidatedTags);
@@ -157,11 +160,18 @@ public function hasItem($key)
if (!$this->pool->hasItem($key)) {
return false;
}
- if (!$itemTags = $this->pool->getItem(static::TAGS_PREFIX.$key)->get()) {
+
+ $itemTags = $this->pool->getItem(static::TAGS_PREFIX.$key);
+
+ if (!$itemTags->isHit()) {
+ return false;
+ }
+
+ if (!$itemTags = $itemTags->get()) {
return true;
}
- foreach ($this->getTagVersions(array($itemTags)) as $tag => $version) {
+ foreach ($this->getTagVersions([$itemTags]) as $tag => $version) {
if ($itemTags[$tag] !== $version && 1 !== $itemTags[$tag] - $version) {
return false;
}
@@ -175,20 +185,22 @@ public function hasItem($key)
*/
public function getItem($key)
{
- foreach ($this->getItems(array($key)) as $item) {
+ foreach ($this->getItems([$key]) as $item) {
return $item;
}
+
+ return null;
}
/**
* {@inheritdoc}
*/
- public function getItems(array $keys = array())
+ public function getItems(array $keys = [])
{
if ($this->deferred) {
$this->commit();
}
- $tagKeys = array();
+ $tagKeys = [];
foreach ($keys as $key) {
if ('' !== $key && \is_string($key)) {
@@ -213,7 +225,7 @@ public function getItems(array $keys = array())
*/
public function clear()
{
- $this->deferred = array();
+ $this->deferred = [];
return $this->pool->clear();
}
@@ -223,7 +235,7 @@ public function clear()
*/
public function deleteItem($key)
{
- return $this->deleteItems(array($key));
+ return $this->deleteItems([$key]);
}
/**
@@ -271,7 +283,17 @@ public function saveDeferred(CacheItemInterface $item)
*/
public function commit()
{
- return $this->invalidateTags(array());
+ return $this->invalidateTags([]);
+ }
+
+ public function __sleep()
+ {
+ throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
+ }
+
+ public function __wakeup()
+ {
+ throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
}
public function __destruct()
@@ -281,7 +303,7 @@ public function __destruct()
private function generateItems($items, array $tagKeys)
{
- $bufferedItems = $itemTags = array();
+ $bufferedItems = $itemTags = [];
$f = $this->setCacheItemTags;
foreach ($items as $key => $item) {
@@ -295,7 +317,10 @@ private function generateItems($items, array $tagKeys)
}
unset($tagKeys[$key]);
- $itemTags[$key] = $item->get() ?: array();
+
+ if ($item->isHit()) {
+ $itemTags[$key] = $item->get() ?: [];
+ }
if (!$tagKeys) {
$tagVersions = $this->getTagVersions($itemTags);
@@ -318,7 +343,7 @@ private function generateItems($items, array $tagKeys)
}
}
- private function getTagVersions(array $tagsByKey, array &$invalidatedTags = array())
+ private function getTagVersions(array $tagsByKey, array &$invalidatedTags = [])
{
$tagVersions = $invalidatedTags;
@@ -327,7 +352,7 @@ private function getTagVersions(array $tagsByKey, array &$invalidatedTags = arra
}
if (!$tagVersions) {
- return array();
+ return [];
}
if (!$fetchTagVersions = 1 !== \func_num_args()) {
@@ -341,7 +366,7 @@ private function getTagVersions(array $tagsByKey, array &$invalidatedTags = arra
}
$now = microtime(true);
- $tags = array();
+ $tags = [];
foreach ($tagVersions as $tag => $version) {
$tags[$tag.static::TAGS_PREFIX] = $tag;
if ($fetchTagVersions || !isset($this->knownTagVersions[$tag])) {
@@ -349,7 +374,7 @@ private function getTagVersions(array $tagsByKey, array &$invalidatedTags = arra
continue;
}
$version -= $this->knownTagVersions[$tag][1];
- if ((0 !== $version && 1 !== $version) || $this->knownTagVersionsTtl > $now - $this->knownTagVersions[$tag][0]) {
+ if ((0 !== $version && 1 !== $version) || $now - $this->knownTagVersions[$tag][0] >= $this->knownTagVersionsTtl) {
// reuse previously fetched tag versions up to the ttl, unless we are storing items or a potential miss arises
$fetchTagVersions = true;
} else {
@@ -366,7 +391,7 @@ private function getTagVersions(array $tagsByKey, array &$invalidatedTags = arra
if (isset($invalidatedTags[$tag])) {
$invalidatedTags[$tag] = $version->set(++$tagVersions[$tag]);
}
- $this->knownTagVersions[$tag] = array($now, $tagVersions[$tag]);
+ $this->knownTagVersions[$tag] = [$now, $tagVersions[$tag]];
}
return $tagVersions;
diff --git a/Adapter/TraceableAdapter.php b/Adapter/TraceableAdapter.php
index 98d0e526..660acf54 100644
--- a/Adapter/TraceableAdapter.php
+++ b/Adapter/TraceableAdapter.php
@@ -12,8 +12,11 @@
namespace Symfony\Component\Cache\Adapter;
use Psr\Cache\CacheItemInterface;
+use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
+use Symfony\Contracts\Cache\CacheInterface;
+use Symfony\Contracts\Service\ResetInterface;
/**
* An adapter that collects data about all cache calls.
@@ -22,16 +25,48 @@
* @author Tobias Nyholm
*/
-class TraceableAdapter implements AdapterInterface, PruneableInterface, ResettableInterface
+class TraceableAdapter implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
{
protected $pool;
- private $calls = array();
+ private $calls = [];
public function __construct(AdapterInterface $pool)
{
$this->pool = $pool;
}
+ /**
+ * {@inheritdoc}
+ */
+ public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
+ {
+ if (!$this->pool instanceof CacheInterface) {
+ throw new \BadMethodCallException(sprintf('Cannot call "%s::get()": this class doesn\'t implement "%s".', \get_class($this->pool), CacheInterface::class));
+ }
+
+ $isHit = true;
+ $callback = function (CacheItem $item, bool &$save) use ($callback, &$isHit) {
+ $isHit = $item->isHit();
+
+ return $callback($item, $save);
+ };
+
+ $event = $this->start(__FUNCTION__);
+ try {
+ $value = $this->pool->get($key, $callback, $beta, $metadata);
+ $event->result[$key] = \is_object($value) ? \get_class($value) : \gettype($value);
+ } finally {
+ $event->end = microtime(true);
+ }
+ if ($isHit) {
+ ++$event->hits;
+ } else {
+ ++$event->misses;
+ }
+
+ return $value;
+ }
+
/**
* {@inheritdoc}
*/
@@ -107,7 +142,7 @@ public function saveDeferred(CacheItemInterface $item)
/**
* {@inheritdoc}
*/
- public function getItems(array $keys = array())
+ public function getItems(array $keys = [])
{
$event = $this->start(__FUNCTION__);
try {
@@ -116,7 +151,7 @@ public function getItems(array $keys = array())
$event->end = microtime(true);
}
$f = function () use ($result, $event) {
- $event->result = array();
+ $event->result = [];
foreach ($result as $key => $item) {
if ($event->result[$key] = $item->isHit()) {
++$event->hits;
@@ -191,7 +226,7 @@ public function prune()
*/
public function reset()
{
- if (!$this->pool instanceof ResettableInterface) {
+ if (!$this->pool instanceof ResetInterface) {
return;
}
$event = $this->start(__FUNCTION__);
@@ -202,6 +237,19 @@ public function reset()
}
}
+ /**
+ * {@inheritdoc}
+ */
+ public function delete(string $key): bool
+ {
+ $event = $this->start(__FUNCTION__);
+ try {
+ return $event->result[$key] = $this->pool->deleteItem($key);
+ } finally {
+ $event->end = microtime(true);
+ }
+ }
+
public function getCalls()
{
return $this->calls;
@@ -209,7 +257,7 @@ public function getCalls()
public function clearCalls()
{
- $this->calls = array();
+ $this->calls = [];
}
protected function start($name)
diff --git a/Adapter/TraceableTagAwareAdapter.php b/Adapter/TraceableTagAwareAdapter.php
index de68955d..69461b8b 100644
--- a/Adapter/TraceableTagAwareAdapter.php
+++ b/Adapter/TraceableTagAwareAdapter.php
@@ -11,10 +11,12 @@
namespace Symfony\Component\Cache\Adapter;
+use Symfony\Contracts\Cache\TagAwareCacheInterface;
+
/**
* @author Robin Chalas
*/
-final class CacheItem implements CacheItemInterface
+final class CacheItem implements ItemInterface
{
+ private const METADATA_EXPIRY_OFFSET = 1527506807;
+
protected $key;
protected $value;
protected $isHit = false;
protected $expiry;
protected $defaultLifetime;
- protected $tags = array();
- protected $prevTags = array();
+ protected $metadata = [];
+ protected $newMetadata = [];
protected $innerItem;
protected $poolHash;
+ protected $isTaggable = false;
/**
* {@inheritdoc}
@@ -56,6 +60,8 @@ public function isHit()
/**
* {@inheritdoc}
+ *
+ * @return $this
*/
public function set($value)
{
@@ -66,13 +72,15 @@ public function set($value)
/**
* {@inheritdoc}
+ *
+ * @return $this
*/
public function expiresAt($expiration)
{
if (null === $expiration) {
- $this->expiry = $this->defaultLifetime > 0 ? time() + $this->defaultLifetime : null;
+ $this->expiry = $this->defaultLifetime > 0 ? microtime(true) + $this->defaultLifetime : null;
} elseif ($expiration instanceof \DateTimeInterface) {
- $this->expiry = (int) $expiration->format('U');
+ $this->expiry = (float) $expiration->format('U.u');
} else {
throw new InvalidArgumentException(sprintf('Expiration date must implement DateTimeInterface or be null, "%s" given', \is_object($expiration) ? \get_class($expiration) : \gettype($expiration)));
}
@@ -82,15 +90,17 @@ public function expiresAt($expiration)
/**
* {@inheritdoc}
+ *
+ * @return $this
*/
public function expiresAfter($time)
{
if (null === $time) {
- $this->expiry = $this->defaultLifetime > 0 ? time() + $this->defaultLifetime : null;
+ $this->expiry = $this->defaultLifetime > 0 ? microtime(true) + $this->defaultLifetime : null;
} elseif ($time instanceof \DateInterval) {
- $this->expiry = (int) \DateTime::createFromFormat('U', time())->add($time)->format('U');
+ $this->expiry = microtime(true) + \DateTime::createFromFormat('U', 0)->add($time)->format('U.u');
} elseif (\is_int($time)) {
- $this->expiry = $time + time();
+ $this->expiry = $time + microtime(true);
} else {
throw new InvalidArgumentException(sprintf('Expiration date must be an integer, a DateInterval or null, "%s" given', \is_object($time) ? \get_class($time) : \gettype($time)));
}
@@ -99,24 +109,21 @@ public function expiresAfter($time)
}
/**
- * Adds a tag to a cache item.
- *
- * @param string|string[] $tags A tag or array of tags
- *
- * @return static
- *
- * @throws InvalidArgumentException When $tag is not valid
+ * {@inheritdoc}
*/
- public function tag($tags)
+ public function tag($tags): ItemInterface
{
- if (!\is_array($tags)) {
- $tags = array($tags);
+ if (!$this->isTaggable) {
+ throw new LogicException(sprintf('Cache item "%s" comes from a non tag-aware pool: you cannot tag it.', $this->key));
+ }
+ if (!is_iterable($tags)) {
+ $tags = [$tags];
}
foreach ($tags as $tag) {
if (!\is_string($tag)) {
throw new InvalidArgumentException(sprintf('Cache tag must be string, "%s" given', \is_object($tag) ? \get_class($tag) : \gettype($tag)));
}
- if (isset($this->tags[$tag])) {
+ if (isset($this->newMetadata[self::METADATA_TAGS][$tag])) {
continue;
}
if ('' === $tag) {
@@ -125,20 +132,32 @@ public function tag($tags)
if (false !== strpbrk($tag, '{}()/\@:')) {
throw new InvalidArgumentException(sprintf('Cache tag "%s" contains reserved characters {}()/\@:', $tag));
}
- $this->tags[$tag] = $tag;
+ $this->newMetadata[self::METADATA_TAGS][$tag] = $tag;
}
return $this;
}
+ /**
+ * {@inheritdoc}
+ */
+ public function getMetadata(): array
+ {
+ return $this->metadata;
+ }
+
/**
* Returns the list of tags bound to the value coming from the pool storage if any.
*
* @return array
+ *
+ * @deprecated since Symfony 4.2, use the "getMetadata()" method instead.
*/
public function getPreviousTags()
{
- return $this->prevTags;
+ @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.2, use the "getMetadata()" method instead.', __METHOD__), E_USER_DEPRECATED);
+
+ return $this->metadata[self::METADATA_TAGS] ?? [];
}
/**
@@ -170,12 +189,12 @@ public static function validateKey($key)
*
* @internal
*/
- public static function log(LoggerInterface $logger = null, $message, $context = array())
+ public static function log(LoggerInterface $logger = null, $message, $context = [])
{
if ($logger) {
$logger->warning($message, $context);
} else {
- $replace = array();
+ $replace = [];
foreach ($context as $k => $v) {
if (is_scalar($v)) {
$replace['{'.$k.'}'] = $v;
diff --git a/DataCollector/CacheDataCollector.php b/DataCollector/CacheDataCollector.php
index 91763e5a..1ac70296 100644
--- a/DataCollector/CacheDataCollector.php
+++ b/DataCollector/CacheDataCollector.php
@@ -27,11 +27,10 @@ class CacheDataCollector extends DataCollector implements LateDataCollectorInter
/**
* @var TraceableAdapter[]
*/
- private $instances = array();
+ private $instances = [];
/**
- * @param string $name
- * @param TraceableAdapter $instance
+ * @param string $name
*/
public function addInstance($name, TraceableAdapter $instance)
{
@@ -43,8 +42,8 @@ public function addInstance($name, TraceableAdapter $instance)
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
{
- $empty = array('calls' => array(), 'config' => array(), 'options' => array(), 'statistics' => array());
- $this->data = array('instances' => $empty, 'total' => $empty);
+ $empty = ['calls' => [], 'config' => [], 'options' => [], 'statistics' => []];
+ $this->data = ['instances' => $empty, 'total' => $empty];
foreach ($this->instances as $name => $instance) {
$this->data['instances']['calls'][$name] = $instance->getCalls();
}
@@ -55,7 +54,7 @@ public function collect(Request $request, Response $response, \Exception $except
public function reset()
{
- $this->data = array();
+ $this->data = [];
foreach ($this->instances as $instance) {
$instance->clearCalls();
}
@@ -106,9 +105,9 @@ public function getCalls()
private function calculateStatistics(): array
{
- $statistics = array();
+ $statistics = [];
foreach ($this->data['instances']['calls'] as $name => $calls) {
- $statistics[$name] = array(
+ $statistics[$name] = [
'calls' => 0,
'time' => 0,
'reads' => 0,
@@ -116,12 +115,20 @@ private function calculateStatistics(): array
'deletes' => 0,
'hits' => 0,
'misses' => 0,
- );
+ ];
/** @var TraceableAdapterEvent $call */
foreach ($calls as $call) {
++$statistics[$name]['calls'];
$statistics[$name]['time'] += $call->end - $call->start;
- if ('getItem' === $call->name) {
+ if ('get' === $call->name) {
+ ++$statistics[$name]['reads'];
+ if ($call->hits) {
+ ++$statistics[$name]['hits'];
+ } else {
+ ++$statistics[$name]['misses'];
+ ++$statistics[$name]['writes'];
+ }
+ } elseif ('getItem' === $call->name) {
++$statistics[$name]['reads'];
if ($call->hits) {
++$statistics[$name]['hits'];
@@ -158,7 +165,7 @@ private function calculateStatistics(): array
private function calculateTotalStatistics(): array
{
$statistics = $this->getStatistics();
- $totals = array(
+ $totals = [
'calls' => 0,
'time' => 0,
'reads' => 0,
@@ -166,7 +173,7 @@ private function calculateTotalStatistics(): array
'deletes' => 0,
'hits' => 0,
'misses' => 0,
- );
+ ];
foreach ($statistics as $name => $values) {
foreach ($totals as $key => $value) {
$totals[$key] += $statistics[$name][$key];
diff --git a/DependencyInjection/CacheCollectorPass.php b/DependencyInjection/CacheCollectorPass.php
new file mode 100644
index 00000000..6193d347
--- /dev/null
+++ b/DependencyInjection/CacheCollectorPass.php
@@ -0,0 +1,72 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\DependencyInjection;
+
+use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
+use Symfony\Component\Cache\Adapter\TraceableAdapter;
+use Symfony\Component\Cache\Adapter\TraceableTagAwareAdapter;
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Definition;
+use Symfony\Component\DependencyInjection\Reference;
+
+/**
+ * Inject a data collector to all the cache services to be able to get detailed statistics.
+ *
+ * @author Tobias Nyholm
+ */
+class CachePoolClearerPass implements CompilerPassInterface
+{
+ private $cachePoolClearerTag;
+
+ public function __construct(string $cachePoolClearerTag = 'cache.pool.clearer')
+ {
+ $this->cachePoolClearerTag = $cachePoolClearerTag;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function process(ContainerBuilder $container)
+ {
+ $container->getParameterBag()->remove('cache.prefix.seed');
+
+ foreach ($container->findTaggedServiceIds($this->cachePoolClearerTag) as $id => $attr) {
+ $clearer = $container->getDefinition($id);
+ $pools = [];
+ foreach ($clearer->getArgument(0) as $name => $ref) {
+ if ($container->hasDefinition($ref)) {
+ $pools[$name] = new Reference($ref);
+ }
+ }
+ $clearer->replaceArgument(0, $pools);
+ }
+ }
+}
diff --git a/DependencyInjection/CachePoolPass.php b/DependencyInjection/CachePoolPass.php
new file mode 100644
index 00000000..eef9e75b
--- /dev/null
+++ b/DependencyInjection/CachePoolPass.php
@@ -0,0 +1,178 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\DependencyInjection;
+
+use Symfony\Component\Cache\Adapter\AbstractAdapter;
+use Symfony\Component\Cache\Adapter\ArrayAdapter;
+use Symfony\Component\DependencyInjection\ChildDefinition;
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Definition;
+use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
+use Symfony\Component\DependencyInjection\Reference;
+
+/**
+ * @author Nicolas Grekas
+ */
+class CachePoolPass implements CompilerPassInterface
+{
+ private $cachePoolTag;
+ private $kernelResetTag;
+ private $cacheClearerId;
+ private $cachePoolClearerTag;
+ private $cacheSystemClearerId;
+ private $cacheSystemClearerTag;
+
+ public function __construct(string $cachePoolTag = 'cache.pool', string $kernelResetTag = 'kernel.reset', string $cacheClearerId = 'cache.global_clearer', string $cachePoolClearerTag = 'cache.pool.clearer', string $cacheSystemClearerId = 'cache.system_clearer', string $cacheSystemClearerTag = 'kernel.cache_clearer')
+ {
+ $this->cachePoolTag = $cachePoolTag;
+ $this->kernelResetTag = $kernelResetTag;
+ $this->cacheClearerId = $cacheClearerId;
+ $this->cachePoolClearerTag = $cachePoolClearerTag;
+ $this->cacheSystemClearerId = $cacheSystemClearerId;
+ $this->cacheSystemClearerTag = $cacheSystemClearerTag;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function process(ContainerBuilder $container)
+ {
+ if ($container->hasParameter('cache.prefix.seed')) {
+ $seed = '.'.$container->getParameterBag()->resolveValue($container->getParameter('cache.prefix.seed'));
+ } else {
+ $seed = '_'.$container->getParameter('kernel.project_dir');
+ }
+ $seed .= '.'.$container->getParameter('kernel.container_class');
+
+ $pools = [];
+ $clearers = [];
+ $attributes = [
+ 'provider',
+ 'name',
+ 'namespace',
+ 'default_lifetime',
+ 'reset',
+ ];
+ foreach ($container->findTaggedServiceIds($this->cachePoolTag) as $id => $tags) {
+ $adapter = $pool = $container->getDefinition($id);
+ if ($pool->isAbstract()) {
+ continue;
+ }
+ $class = $adapter->getClass();
+ while ($adapter instanceof ChildDefinition) {
+ $adapter = $container->findDefinition($adapter->getParent());
+ $class = $class ?: $adapter->getClass();
+ if ($t = $adapter->getTag($this->cachePoolTag)) {
+ $tags[0] += $t[0];
+ }
+ }
+ $name = $tags[0]['name'] ?? $id;
+ if (!isset($tags[0]['namespace'])) {
+ $namespaceSeed = $seed;
+ if (null !== $class) {
+ $namespaceSeed .= '.'.$class;
+ }
+
+ $tags[0]['namespace'] = $this->getNamespace($namespaceSeed, $name);
+ }
+ if (isset($tags[0]['clearer'])) {
+ $clearer = $tags[0]['clearer'];
+ while ($container->hasAlias($clearer)) {
+ $clearer = (string) $container->getAlias($clearer);
+ }
+ } else {
+ $clearer = null;
+ }
+ unset($tags[0]['clearer'], $tags[0]['name']);
+
+ if (isset($tags[0]['provider'])) {
+ $tags[0]['provider'] = new Reference(static::getServiceProvider($container, $tags[0]['provider']));
+ }
+ $i = 0;
+ foreach ($attributes as $attr) {
+ if (!isset($tags[0][$attr])) {
+ // no-op
+ } elseif ('reset' === $attr) {
+ if ($tags[0][$attr]) {
+ $pool->addTag($this->kernelResetTag, ['method' => $tags[0][$attr]]);
+ }
+ } elseif ('namespace' !== $attr || ArrayAdapter::class !== $adapter->getClass()) {
+ $pool->replaceArgument($i++, $tags[0][$attr]);
+ }
+ unset($tags[0][$attr]);
+ }
+ if (!empty($tags[0])) {
+ throw new InvalidArgumentException(sprintf('Invalid "%s" tag for service "%s": accepted attributes are "clearer", "provider", "name", "namespace", "default_lifetime" and "reset", found "%s".', $this->cachePoolTag, $id, implode('", "', array_keys($tags[0]))));
+ }
+
+ if (null !== $clearer) {
+ $clearers[$clearer][$name] = new Reference($id, $container::IGNORE_ON_UNINITIALIZED_REFERENCE);
+ }
+
+ $pools[$name] = new Reference($id, $container::IGNORE_ON_UNINITIALIZED_REFERENCE);
+ }
+
+ $notAliasedCacheClearerId = $this->cacheClearerId;
+ while ($container->hasAlias($this->cacheClearerId)) {
+ $this->cacheClearerId = (string) $container->getAlias($this->cacheClearerId);
+ }
+ if ($container->hasDefinition($this->cacheClearerId)) {
+ $clearers[$notAliasedCacheClearerId] = $pools;
+ }
+
+ foreach ($clearers as $id => $pools) {
+ $clearer = $container->getDefinition($id);
+ if ($clearer instanceof ChildDefinition) {
+ $clearer->replaceArgument(0, $pools);
+ } else {
+ $clearer->setArgument(0, $pools);
+ }
+ $clearer->addTag($this->cachePoolClearerTag);
+
+ if ($this->cacheSystemClearerId === $id) {
+ $clearer->addTag($this->cacheSystemClearerTag);
+ }
+ }
+
+ if ($container->hasDefinition('console.command.cache_pool_list')) {
+ $container->getDefinition('console.command.cache_pool_list')->replaceArgument(0, array_keys($pools));
+ }
+ }
+
+ private function getNamespace($seed, $id)
+ {
+ return substr(str_replace('/', '-', base64_encode(hash('sha256', $id.$seed, true))), 0, 10);
+ }
+
+ /**
+ * @internal
+ */
+ public static function getServiceProvider(ContainerBuilder $container, $name)
+ {
+ $container->resolveEnvPlaceholders($name, null, $usedEnvs);
+
+ if ($usedEnvs || preg_match('#^[a-z]++:#', $name)) {
+ $dsn = $name;
+
+ if (!$container->hasDefinition($name = '.cache_connection.'.ContainerBuilder::hash($dsn))) {
+ $definition = new Definition(AbstractAdapter::class);
+ $definition->setPublic(false);
+ $definition->setFactory([AbstractAdapter::class, 'createConnection']);
+ $definition->setArguments([$dsn, ['lazy' => true]]);
+ $container->setDefinition($name, $definition);
+ }
+ }
+
+ return $name;
+ }
+}
diff --git a/DependencyInjection/CachePoolPrunerPass.php b/DependencyInjection/CachePoolPrunerPass.php
new file mode 100644
index 00000000..e5699623
--- /dev/null
+++ b/DependencyInjection/CachePoolPrunerPass.php
@@ -0,0 +1,60 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\DependencyInjection;
+
+use Symfony\Component\Cache\PruneableInterface;
+use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
+use Symfony\Component\DependencyInjection\Reference;
+
+/**
+ * @author Rob Frawley 2nd
@@ -39,7 +40,7 @@ public function prune()
*/
public function reset()
{
- if ($this->pool instanceof ResettableInterface) {
+ if ($this->pool instanceof ResetInterface) {
$this->pool->reset();
}
$this->setNamespace($this->getNamespace());
@@ -90,7 +91,7 @@ protected function doDelete($id)
*/
protected function doFlush()
{
- $this->pool->clear();
+ return $this->pool->clear();
}
/**
@@ -98,5 +99,6 @@ protected function doFlush()
*/
protected function doGetStats()
{
+ return null;
}
}
diff --git a/Exception/CacheException.php b/Exception/CacheException.php
index e87b2db8..d2e975b2 100644
--- a/Exception/CacheException.php
+++ b/Exception/CacheException.php
@@ -14,6 +14,12 @@
use Psr\Cache\CacheException as Psr6CacheInterface;
use Psr\SimpleCache\CacheException as SimpleCacheInterface;
-class CacheException extends \Exception implements Psr6CacheInterface, SimpleCacheInterface
-{
+if (interface_exists(SimpleCacheInterface::class)) {
+ class CacheException extends \Exception implements Psr6CacheInterface, SimpleCacheInterface
+ {
+ }
+} else {
+ class CacheException extends \Exception implements Psr6CacheInterface
+ {
+ }
}
diff --git a/Exception/InvalidArgumentException.php b/Exception/InvalidArgumentException.php
index 828bf3ed..7f9584a2 100644
--- a/Exception/InvalidArgumentException.php
+++ b/Exception/InvalidArgumentException.php
@@ -14,6 +14,12 @@
use Psr\Cache\InvalidArgumentException as Psr6CacheInterface;
use Psr\SimpleCache\InvalidArgumentException as SimpleCacheInterface;
-class InvalidArgumentException extends \InvalidArgumentException implements Psr6CacheInterface, SimpleCacheInterface
-{
+if (interface_exists(SimpleCacheInterface::class)) {
+ class InvalidArgumentException extends \InvalidArgumentException implements Psr6CacheInterface, SimpleCacheInterface
+ {
+ }
+} else {
+ class InvalidArgumentException extends \InvalidArgumentException implements Psr6CacheInterface
+ {
+ }
}
diff --git a/Exception/LogicException.php b/Exception/LogicException.php
new file mode 100644
index 00000000..9ffa7ed6
--- /dev/null
+++ b/Exception/LogicException.php
@@ -0,0 +1,25 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\Exception;
+
+use Psr\Cache\CacheException as Psr6CacheInterface;
+use Psr\SimpleCache\CacheException as SimpleCacheInterface;
+
+if (interface_exists(SimpleCacheInterface::class)) {
+ class LogicException extends \LogicException implements Psr6CacheInterface, SimpleCacheInterface
+ {
+ }
+} else {
+ class LogicException extends \LogicException implements Psr6CacheInterface
+ {
+ }
+}
diff --git a/LICENSE b/LICENSE
index fcd3fa76..3c464ca9 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,4 +1,4 @@
-Copyright (c) 2016-2018 Fabien Potencier
+Copyright (c) 2016-2019 Fabien Potencier
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
diff --git a/LockRegistry.php b/LockRegistry.php
new file mode 100644
index 00000000..9bb2bcac
--- /dev/null
+++ b/LockRegistry.php
@@ -0,0 +1,152 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache;
+
+use Psr\Log\LoggerInterface;
+use Symfony\Contracts\Cache\CacheInterface;
+use Symfony\Contracts\Cache\ItemInterface;
+
+/**
+ * LockRegistry is used internally by existing adapters to protect against cache stampede.
+ *
+ * It does so by wrapping the computation of items in a pool of locks.
+ * Foreach each apps, there can be at most 20 concurrent processes that
+ * compute items at the same time and only one per cache-key.
+ *
+ * @author Nicolas Grekas
+ */
+final class LockRegistry
+{
+ private static $openedFiles = [];
+ private static $lockedFiles = [];
+
+ /**
+ * The number of items in this list controls the max number of concurrent processes.
+ */
+ private static $files = [
+ __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'AbstractAdapter.php',
+ __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'AbstractTagAwareAdapter.php',
+ __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'AdapterInterface.php',
+ __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'ApcuAdapter.php',
+ __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'ArrayAdapter.php',
+ __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'ChainAdapter.php',
+ __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'DoctrineAdapter.php',
+ __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'FilesystemAdapter.php',
+ __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'FilesystemTagAwareAdapter.php',
+ __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'MemcachedAdapter.php',
+ __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'NullAdapter.php',
+ __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'PdoAdapter.php',
+ __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'PhpArrayAdapter.php',
+ __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'PhpFilesAdapter.php',
+ __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'ProxyAdapter.php',
+ __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'Psr16Adapter.php',
+ __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'RedisAdapter.php',
+ __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'RedisTagAwareAdapter.php',
+ __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'SimpleCacheAdapter.php',
+ __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'TagAwareAdapter.php',
+ __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'TagAwareAdapterInterface.php',
+ __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'TraceableAdapter.php',
+ __DIR__.\DIRECTORY_SEPARATOR.'Adapter'.\DIRECTORY_SEPARATOR.'TraceableTagAwareAdapter.php',
+ ];
+
+ /**
+ * Defines a set of existing files that will be used as keys to acquire locks.
+ *
+ * @return array The previously defined set of files
+ */
+ public static function setFiles(array $files): array
+ {
+ $previousFiles = self::$files;
+ self::$files = $files;
+
+ foreach (self::$openedFiles as $file) {
+ if ($file) {
+ flock($file, LOCK_UN);
+ fclose($file);
+ }
+ }
+ self::$openedFiles = self::$lockedFiles = [];
+
+ return $previousFiles;
+ }
+
+ public static function compute(callable $callback, ItemInterface $item, bool &$save, CacheInterface $pool, \Closure $setMetadata = null, LoggerInterface $logger = null)
+ {
+ $key = self::$files ? crc32($item->getKey()) % \count(self::$files) : -1;
+
+ if ($key < 0 || (self::$lockedFiles[$key] ?? false) || !$lock = self::open($key)) {
+ return $callback($item, $save);
+ }
+
+ while (true) {
+ try {
+ // race to get the lock in non-blocking mode
+ $locked = flock($lock, LOCK_EX | LOCK_NB, $wouldBlock);
+
+ if ($locked || !$wouldBlock) {
+ $logger && $logger->info(sprintf('Lock %s, now computing item "{key}"', $locked ? 'acquired' : 'not supported'), ['key' => $item->getKey()]);
+ self::$lockedFiles[$key] = true;
+
+ $value = $callback($item, $save);
+
+ if ($save) {
+ if ($setMetadata) {
+ $setMetadata($item);
+ }
+
+ $pool->save($item->set($value));
+ $save = false;
+ }
+
+ return $value;
+ }
+ // if we failed the race, retry locking in blocking mode to wait for the winner
+ $logger && $logger->info('Item "{key}" is locked, waiting for it to be released', ['key' => $item->getKey()]);
+ flock($lock, LOCK_SH);
+ } finally {
+ flock($lock, LOCK_UN);
+ unset(self::$lockedFiles[$key]);
+ }
+ static $signalingException, $signalingCallback;
+ $signalingException = $signalingException ?? unserialize("O:9:\"Exception\":1:{s:16:\"\0Exception\0trace\";a:0:{}}");
+ $signalingCallback = $signalingCallback ?? function () use ($signalingException) { throw $signalingException; };
+
+ try {
+ $value = $pool->get($item->getKey(), $signalingCallback, 0);
+ $logger && $logger->info('Item "{key}" retrieved after lock was released', ['key' => $item->getKey()]);
+ $save = false;
+
+ return $value;
+ } catch (\Exception $e) {
+ if ($signalingException !== $e) {
+ throw $e;
+ }
+ $logger && $logger->info('Item "{key}" not found while lock was released, now retrying', ['key' => $item->getKey()]);
+ }
+ }
+ }
+
+ private static function open(int $key)
+ {
+ if (null !== $h = self::$openedFiles[$key] ?? null) {
+ return $h;
+ }
+ set_error_handler(function () {});
+ try {
+ $h = fopen(self::$files[$key], 'r+');
+ } finally {
+ restore_error_handler();
+ }
+
+ return self::$openedFiles[$key] = $h ?: @fopen(self::$files[$key], 'r');
+ }
+}
diff --git a/Marshaller/DefaultMarshaller.php b/Marshaller/DefaultMarshaller.php
new file mode 100644
index 00000000..80560901
--- /dev/null
+++ b/Marshaller/DefaultMarshaller.php
@@ -0,0 +1,99 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\Marshaller;
+
+use Symfony\Component\Cache\Exception\CacheException;
+
+/**
+ * Serializes/unserializes values using igbinary_serialize() if available, serialize() otherwise.
+ *
+ * @author Nicolas Grekas
+ */
+class DefaultMarshaller implements MarshallerInterface
+{
+ private $useIgbinarySerialize = true;
+
+ public function __construct(bool $useIgbinarySerialize = null)
+ {
+ if (null === $useIgbinarySerialize) {
+ $useIgbinarySerialize = \extension_loaded('igbinary') && \PHP_VERSION_ID < 70400;
+ } elseif ($useIgbinarySerialize && (!\extension_loaded('igbinary') || \PHP_VERSION_ID >= 70400)) {
+ throw new CacheException('The "igbinary" PHP extension is not '.(\PHP_VERSION_ID >= 70400 ? 'compatible with PHP 7.4.' : 'loaded.'));
+ }
+ $this->useIgbinarySerialize = $useIgbinarySerialize;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function marshall(array $values, ?array &$failed): array
+ {
+ $serialized = $failed = [];
+
+ foreach ($values as $id => $value) {
+ try {
+ if ($this->useIgbinarySerialize) {
+ $serialized[$id] = igbinary_serialize($value);
+ } else {
+ $serialized[$id] = serialize($value);
+ }
+ } catch (\Exception $e) {
+ $failed[] = $id;
+ }
+ }
+
+ return $serialized;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function unmarshall(string $value)
+ {
+ if ('b:0;' === $value) {
+ return false;
+ }
+ if ('N;' === $value) {
+ return null;
+ }
+ static $igbinaryNull;
+ if ($value === ($igbinaryNull ?? $igbinaryNull = \extension_loaded('igbinary') && \PHP_VERSION_ID < 70400 ? igbinary_serialize(null) : false)) {
+ return null;
+ }
+ $unserializeCallbackHandler = ini_set('unserialize_callback_func', __CLASS__.'::handleUnserializeCallback');
+ try {
+ if (':' === ($value[1] ?? ':')) {
+ if (false !== $value = unserialize($value)) {
+ return $value;
+ }
+ } elseif (false === $igbinaryNull) {
+ throw new \RuntimeException('Failed to unserialize values, did you forget to install the "igbinary" extension?');
+ } elseif (null !== $value = igbinary_unserialize($value)) {
+ return $value;
+ }
+
+ throw new \DomainException(error_get_last() ? error_get_last()['message'] : 'Failed to unserialize values.');
+ } catch (\Error $e) {
+ throw new \ErrorException($e->getMessage(), $e->getCode(), E_ERROR, $e->getFile(), $e->getLine());
+ } finally {
+ ini_set('unserialize_callback_func', $unserializeCallbackHandler);
+ }
+ }
+
+ /**
+ * @internal
+ */
+ public static function handleUnserializeCallback($class)
+ {
+ throw new \DomainException('Class not found: '.$class);
+ }
+}
diff --git a/Marshaller/MarshallerInterface.php b/Marshaller/MarshallerInterface.php
new file mode 100644
index 00000000..cdd6c402
--- /dev/null
+++ b/Marshaller/MarshallerInterface.php
@@ -0,0 +1,40 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\Marshaller;
+
+/**
+ * Serializes/unserializes PHP values.
+ *
+ * Implementations of this interface MUST deal with errors carefully. They MUST
+ * also deal with forward and backward compatibility at the storage format level.
+ *
+ * @author Nicolas Grekas
+ */
+interface MarshallerInterface
+{
+ /**
+ * Serializes a list of values.
+ *
+ * When serialization fails for a specific value, no exception should be
+ * thrown. Instead, its key should be listed in $failed.
+ */
+ public function marshall(array $values, ?array &$failed): array;
+
+ /**
+ * Unserializes a single value and throws an exception if anything goes wrong.
+ *
+ * @return mixed
+ *
+ * @throws \Exception Whenever unserialization fails
+ */
+ public function unmarshall(string $value);
+}
diff --git a/Psr16Cache.php b/Psr16Cache.php
new file mode 100644
index 00000000..99111a45
--- /dev/null
+++ b/Psr16Cache.php
@@ -0,0 +1,263 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache;
+
+use Psr\Cache\CacheException as Psr6CacheException;
+use Psr\Cache\CacheItemPoolInterface;
+use Psr\SimpleCache\CacheException as SimpleCacheException;
+use Psr\SimpleCache\CacheInterface;
+use Symfony\Component\Cache\Adapter\AdapterInterface;
+use Symfony\Component\Cache\Exception\InvalidArgumentException;
+use Symfony\Component\Cache\Traits\ProxyTrait;
+
+/**
+ * Turns a PSR-6 cache into a PSR-16 one.
+ *
+ * @author Nicolas Grekas
+ */
+class Psr16Cache implements CacheInterface, PruneableInterface, ResettableInterface
+{
+ use ProxyTrait;
+
+ private const METADATA_EXPIRY_OFFSET = 1527506807;
+
+ private $createCacheItem;
+ private $cacheItemPrototype;
+
+ public function __construct(CacheItemPoolInterface $pool)
+ {
+ $this->pool = $pool;
+
+ if (!$pool instanceof AdapterInterface) {
+ return;
+ }
+ $cacheItemPrototype = &$this->cacheItemPrototype;
+ $createCacheItem = \Closure::bind(
+ static function ($key, $value, $allowInt = false) use (&$cacheItemPrototype) {
+ $item = clone $cacheItemPrototype;
+ $item->key = $allowInt && \is_int($key) ? (string) $key : CacheItem::validateKey($key);
+ $item->value = $value;
+ $item->isHit = false;
+
+ return $item;
+ },
+ null,
+ CacheItem::class
+ );
+ $this->createCacheItem = function ($key, $value, $allowInt = false) use ($createCacheItem) {
+ if (null === $this->cacheItemPrototype) {
+ $this->get($allowInt && \is_int($key) ? (string) $key : $key);
+ }
+ $this->createCacheItem = $createCacheItem;
+
+ return $createCacheItem($key, null, $allowInt)->set($value);
+ };
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function get($key, $default = null)
+ {
+ try {
+ $item = $this->pool->getItem($key);
+ } catch (SimpleCacheException $e) {
+ throw $e;
+ } catch (Psr6CacheException $e) {
+ throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
+ }
+ if (null === $this->cacheItemPrototype) {
+ $this->cacheItemPrototype = clone $item;
+ $this->cacheItemPrototype->set(null);
+ }
+
+ return $item->isHit() ? $item->get() : $default;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function set($key, $value, $ttl = null)
+ {
+ try {
+ if (null !== $f = $this->createCacheItem) {
+ $item = $f($key, $value);
+ } else {
+ $item = $this->pool->getItem($key)->set($value);
+ }
+ } catch (SimpleCacheException $e) {
+ throw $e;
+ } catch (Psr6CacheException $e) {
+ throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
+ }
+ if (null !== $ttl) {
+ $item->expiresAfter($ttl);
+ }
+
+ return $this->pool->save($item);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function delete($key)
+ {
+ try {
+ return $this->pool->deleteItem($key);
+ } catch (SimpleCacheException $e) {
+ throw $e;
+ } catch (Psr6CacheException $e) {
+ throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function clear()
+ {
+ return $this->pool->clear();
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getMultiple($keys, $default = null)
+ {
+ if ($keys instanceof \Traversable) {
+ $keys = iterator_to_array($keys, false);
+ } elseif (!\is_array($keys)) {
+ throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', \is_object($keys) ? \get_class($keys) : \gettype($keys)));
+ }
+
+ try {
+ $items = $this->pool->getItems($keys);
+ } catch (SimpleCacheException $e) {
+ throw $e;
+ } catch (Psr6CacheException $e) {
+ throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
+ }
+ $values = [];
+
+ if (!$this->pool instanceof AdapterInterface) {
+ foreach ($items as $key => $item) {
+ $values[$key] = $item->isHit() ? $item->get() : $default;
+ }
+
+ return $values;
+ }
+
+ foreach ($items as $key => $item) {
+ if (!$item->isHit()) {
+ $values[$key] = $default;
+ continue;
+ }
+ $values[$key] = $item->get();
+
+ if (!$metadata = $item->getMetadata()) {
+ continue;
+ }
+ unset($metadata[CacheItem::METADATA_TAGS]);
+
+ if ($metadata) {
+ $values[$key] = ["\x9D".pack('VN', (int) (0.1 + $metadata[CacheItem::METADATA_EXPIRY] - self::METADATA_EXPIRY_OFFSET), $metadata[CacheItem::METADATA_CTIME])."\x5F" => $values[$key]];
+ }
+ }
+
+ return $values;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function setMultiple($values, $ttl = null)
+ {
+ $valuesIsArray = \is_array($values);
+ if (!$valuesIsArray && !$values instanceof \Traversable) {
+ throw new InvalidArgumentException(sprintf('Cache values must be array or Traversable, "%s" given', \is_object($values) ? \get_class($values) : \gettype($values)));
+ }
+ $items = [];
+
+ try {
+ if (null !== $f = $this->createCacheItem) {
+ $valuesIsArray = false;
+ foreach ($values as $key => $value) {
+ $items[$key] = $f($key, $value, true);
+ }
+ } elseif ($valuesIsArray) {
+ $items = [];
+ foreach ($values as $key => $value) {
+ $items[] = (string) $key;
+ }
+ $items = $this->pool->getItems($items);
+ } else {
+ foreach ($values as $key => $value) {
+ if (\is_int($key)) {
+ $key = (string) $key;
+ }
+ $items[$key] = $this->pool->getItem($key)->set($value);
+ }
+ }
+ } catch (SimpleCacheException $e) {
+ throw $e;
+ } catch (Psr6CacheException $e) {
+ throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
+ }
+ $ok = true;
+
+ foreach ($items as $key => $item) {
+ if ($valuesIsArray) {
+ $item->set($values[$key]);
+ }
+ if (null !== $ttl) {
+ $item->expiresAfter($ttl);
+ }
+ $ok = $this->pool->saveDeferred($item) && $ok;
+ }
+
+ return $this->pool->commit() && $ok;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function deleteMultiple($keys)
+ {
+ if ($keys instanceof \Traversable) {
+ $keys = iterator_to_array($keys, false);
+ } elseif (!\is_array($keys)) {
+ throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', \is_object($keys) ? \get_class($keys) : \gettype($keys)));
+ }
+
+ try {
+ return $this->pool->deleteItems($keys);
+ } catch (SimpleCacheException $e) {
+ throw $e;
+ } catch (Psr6CacheException $e) {
+ throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function has($key)
+ {
+ try {
+ return $this->pool->hasItem($key);
+ } catch (SimpleCacheException $e) {
+ throw $e;
+ } catch (Psr6CacheException $e) {
+ throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
+ }
+ }
+}
diff --git a/ResettableInterface.php b/ResettableInterface.php
index 6be72861..7b0a853f 100644
--- a/ResettableInterface.php
+++ b/ResettableInterface.php
@@ -11,10 +11,11 @@
namespace Symfony\Component\Cache;
+use Symfony\Contracts\Service\ResetInterface;
+
/**
* Resets a pool's local state.
*/
-interface ResettableInterface
+interface ResettableInterface extends ResetInterface
{
- public function reset();
}
diff --git a/Simple/AbstractCache.php b/Simple/AbstractCache.php
index 08456f58..af46bf3b 100644
--- a/Simple/AbstractCache.php
+++ b/Simple/AbstractCache.php
@@ -12,17 +12,26 @@
namespace Symfony\Component\Cache\Simple;
use Psr\Log\LoggerAwareInterface;
-use Psr\SimpleCache\CacheInterface;
+use Psr\SimpleCache\CacheInterface as Psr16CacheInterface;
+use Symfony\Component\Cache\Adapter\AbstractAdapter;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\AbstractTrait;
+use Symfony\Contracts\Cache\CacheInterface;
+
+@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "%s" and type-hint for "%s" instead.', AbstractCache::class, AbstractAdapter::class, CacheInterface::class), E_USER_DEPRECATED);
/**
- * @author Nicolas Grekas
+ * @deprecated since Symfony 4.3, use AbstractAdapter and type-hint for CacheInterface instead.
*/
-abstract class AbstractCache implements CacheInterface, LoggerAwareInterface, ResettableInterface
+abstract class AbstractCache implements Psr16CacheInterface, LoggerAwareInterface, ResettableInterface
{
+ /**
+ * @internal
+ */
+ protected const NS_SEPARATOR = ':';
+
use AbstractTrait {
deleteItems as private;
AbstractTrait::deleteItem as delete;
@@ -48,11 +57,11 @@ public function get($key, $default = null)
$id = $this->getId($key);
try {
- foreach ($this->doFetch(array($id)) as $value) {
+ foreach ($this->doFetch([$id]) as $value) {
return $value;
}
} catch (\Exception $e) {
- CacheItem::log($this->logger, 'Failed to fetch key "{key}"', array('key' => $key, 'exception' => $e));
+ CacheItem::log($this->logger, 'Failed to fetch key "{key}": '.$e->getMessage(), ['key' => $key, 'exception' => $e]);
}
return $default;
@@ -65,7 +74,7 @@ public function set($key, $value, $ttl = null)
{
CacheItem::validateKey($key);
- return $this->setMultiple(array($key => $value), $ttl);
+ return $this->setMultiple([$key => $value], $ttl);
}
/**
@@ -78,7 +87,7 @@ public function getMultiple($keys, $default = null)
} elseif (!\is_array($keys)) {
throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', \is_object($keys) ? \get_class($keys) : \gettype($keys)));
}
- $ids = array();
+ $ids = [];
foreach ($keys as $key) {
$ids[] = $this->getId($key);
@@ -86,8 +95,8 @@ public function getMultiple($keys, $default = null)
try {
$values = $this->doFetch($ids);
} catch (\Exception $e) {
- CacheItem::log($this->logger, 'Failed to fetch requested values', array('keys' => $keys, 'exception' => $e));
- $values = array();
+ CacheItem::log($this->logger, 'Failed to fetch values: '.$e->getMessage(), ['keys' => $keys, 'exception' => $e]);
+ $values = [];
}
$ids = array_combine($ids, $keys);
@@ -102,7 +111,7 @@ public function setMultiple($values, $ttl = null)
if (!\is_array($values) && !$values instanceof \Traversable) {
throw new InvalidArgumentException(sprintf('Cache values must be array or Traversable, "%s" given', \is_object($values) ? \get_class($values) : \gettype($values)));
}
- $valuesById = array();
+ $valuesById = [];
foreach ($values as $key => $value) {
if (\is_int($key)) {
@@ -118,14 +127,15 @@ public function setMultiple($values, $ttl = null)
$e = $this->doSave($valuesById, $ttl);
} catch (\Exception $e) {
}
- if (true === $e || array() === $e) {
+ if (true === $e || [] === $e) {
return true;
}
- $keys = array();
+ $keys = [];
foreach (\is_array($e) ? $e : array_keys($valuesById) as $id) {
$keys[] = substr($id, \strlen($this->namespace));
}
- CacheItem::log($this->logger, 'Failed to save values', array('keys' => $keys, 'exception' => $e instanceof \Exception ? $e : null));
+ $message = 'Failed to save values'.($e instanceof \Exception ? ': '.$e->getMessage() : '.');
+ CacheItem::log($this->logger, $message, ['keys' => $keys, 'exception' => $e instanceof \Exception ? $e : null]);
return false;
}
@@ -171,7 +181,7 @@ private function generateValues($values, &$keys, $default)
yield $key => $value;
}
} catch (\Exception $e) {
- CacheItem::log($this->logger, 'Failed to fetch requested values', array('keys' => array_values($keys), 'exception' => $e));
+ CacheItem::log($this->logger, 'Failed to fetch values: '.$e->getMessage(), ['keys' => array_values($keys), 'exception' => $e]);
}
foreach ($keys as $key) {
diff --git a/Simple/ApcuCache.php b/Simple/ApcuCache.php
index 0877c394..c22771e8 100644
--- a/Simple/ApcuCache.php
+++ b/Simple/ApcuCache.php
@@ -13,6 +13,11 @@
use Symfony\Component\Cache\Traits\ApcuTrait;
+@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "%s" and type-hint for "%s" instead.', ApcuCache::class, ApcuAdapter::class, CacheInterface::class), E_USER_DEPRECATED);
+
+/**
+ * @deprecated since Symfony 4.3, use ApcuAdapter and type-hint for CacheInterface instead.
+ */
class ApcuCache extends AbstractCache
{
use ApcuTrait;
diff --git a/Simple/ArrayCache.php b/Simple/ArrayCache.php
index aadefe20..5cd228f6 100644
--- a/Simple/ArrayCache.php
+++ b/Simple/ArrayCache.php
@@ -12,16 +12,20 @@
namespace Symfony\Component\Cache\Simple;
use Psr\Log\LoggerAwareInterface;
-use Psr\SimpleCache\CacheInterface;
+use Psr\SimpleCache\CacheInterface as Psr16CacheInterface;
+use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\ArrayTrait;
+use Symfony\Contracts\Cache\CacheInterface;
+
+@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "%s" and type-hint for "%s" instead.', ArrayCache::class, ArrayAdapter::class, CacheInterface::class), E_USER_DEPRECATED);
/**
- * @author Nicolas Grekas
+ * @deprecated since Symfony 4.3, use ArrayAdapter and type-hint for CacheInterface instead.
*/
-class ArrayCache implements CacheInterface, LoggerAwareInterface, ResettableInterface
+class ArrayCache implements Psr16CacheInterface, LoggerAwareInterface, ResettableInterface
{
use ArrayTrait {
ArrayTrait::deleteItem as delete;
@@ -31,7 +35,6 @@ class ArrayCache implements CacheInterface, LoggerAwareInterface, ResettableInte
private $defaultLifetime;
/**
- * @param int $defaultLifetime
* @param bool $storeSerialized Disabling serialization can lead to cache corruptions when storing mutable values but increases performance otherwise
*/
public function __construct(int $defaultLifetime = 0, bool $storeSerialized = true)
@@ -45,9 +48,20 @@ public function __construct(int $defaultLifetime = 0, bool $storeSerialized = tr
*/
public function get($key, $default = null)
{
- foreach ($this->getMultiple(array($key), $default) as $v) {
- return $v;
+ if (!\is_string($key) || !isset($this->expiries[$key])) {
+ CacheItem::validateKey($key);
+ }
+ if (!$isHit = isset($this->expiries[$key]) && ($this->expiries[$key] > microtime(true) || !$this->delete($key))) {
+ $this->values[$key] = null;
+
+ return $default;
}
+ if (!$this->storeSerialized) {
+ return $this->values[$key];
+ }
+ $value = $this->unfreeze($key, $isHit);
+
+ return $isHit ? $value : $default;
}
/**
@@ -61,10 +75,12 @@ public function getMultiple($keys, $default = null)
throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', \is_object($keys) ? \get_class($keys) : \gettype($keys)));
}
foreach ($keys as $key) {
- CacheItem::validateKey($key);
+ if (!\is_string($key) || !isset($this->expiries[$key])) {
+ CacheItem::validateKey($key);
+ }
}
- return $this->generateItems($keys, time(), function ($k, $v, $hit) use ($default) { return $hit ? $v : $default; });
+ return $this->generateItems($keys, microtime(true), function ($k, $v, $hit) use ($default) { return $hit ? $v : $default; });
}
/**
@@ -87,9 +103,11 @@ public function deleteMultiple($keys)
*/
public function set($key, $value, $ttl = null)
{
- CacheItem::validateKey($key);
+ if (!\is_string($key)) {
+ CacheItem::validateKey($key);
+ }
- return $this->setMultiple(array($key => $value), $ttl);
+ return $this->setMultiple([$key => $value], $ttl);
}
/**
@@ -100,30 +118,23 @@ public function setMultiple($values, $ttl = null)
if (!\is_array($values) && !$values instanceof \Traversable) {
throw new InvalidArgumentException(sprintf('Cache values must be array or Traversable, "%s" given', \is_object($values) ? \get_class($values) : \gettype($values)));
}
- $valuesArray = array();
+ $valuesArray = [];
foreach ($values as $key => $value) {
- \is_int($key) || CacheItem::validateKey($key);
+ if (!\is_int($key) && !(\is_string($key) && isset($this->expiries[$key]))) {
+ CacheItem::validateKey($key);
+ }
$valuesArray[$key] = $value;
}
if (false === $ttl = $this->normalizeTtl($ttl)) {
return $this->deleteMultiple(array_keys($valuesArray));
}
- if ($this->storeSerialized) {
- foreach ($valuesArray as $key => $value) {
- try {
- $valuesArray[$key] = serialize($value);
- } catch (\Exception $e) {
- $type = \is_object($value) ? \get_class($value) : \gettype($value);
- CacheItem::log($this->logger, 'Failed to save key "{key}" ({type})', array('key' => $key, 'type' => $type, 'exception' => $e));
-
- return false;
- }
- }
- }
- $expiry = 0 < $ttl ? time() + $ttl : PHP_INT_MAX;
+ $expiry = 0 < $ttl ? microtime(true) + $ttl : PHP_INT_MAX;
foreach ($valuesArray as $key => $value) {
+ if ($this->storeSerialized && null === $value = $this->freeze($value, $key)) {
+ return false;
+ }
$this->values[$key] = $value;
$this->expiries[$key] = $expiry;
}
diff --git a/Simple/ChainCache.php b/Simple/ChainCache.php
index 38c0a9b3..a0122fde 100644
--- a/Simple/ChainCache.php
+++ b/Simple/ChainCache.php
@@ -11,10 +11,15 @@
namespace Symfony\Component\Cache\Simple;
-use Psr\SimpleCache\CacheInterface;
+use Psr\SimpleCache\CacheInterface as Psr16CacheInterface;
+use Symfony\Component\Cache\Adapter\ChainAdapter;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
+use Symfony\Contracts\Cache\CacheInterface;
+use Symfony\Contracts\Service\ResetInterface;
+
+@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "%s" and type-hint for "%s" instead.', ChainCache::class, ChainAdapter::class, CacheInterface::class), E_USER_DEPRECATED);
/**
* Chains several caches together.
@@ -22,18 +27,18 @@
* Cached items are fetched from the first cache having them in its data store.
* They are saved and deleted in all caches at once.
*
- * @author Nicolas Grekas
+ * @deprecated since Symfony 4.3, use ChainAdapter and type-hint for CacheInterface instead.
*/
-class ChainCache implements CacheInterface, PruneableInterface, ResettableInterface
+class ChainCache implements Psr16CacheInterface, PruneableInterface, ResettableInterface
{
private $miss;
- private $caches = array();
+ private $caches = [];
private $defaultLifetime;
private $cacheCount;
/**
- * @param CacheInterface[] $caches The ordered list of caches used to fetch cached items
- * @param int $defaultLifetime The lifetime of items propagated from lower caches to upper ones
+ * @param Psr16CacheInterface[] $caches The ordered list of caches used to fetch cached items
+ * @param int $defaultLifetime The lifetime of items propagated from lower caches to upper ones
*/
public function __construct(array $caches, int $defaultLifetime = 0)
{
@@ -42,8 +47,8 @@ public function __construct(array $caches, int $defaultLifetime = 0)
}
foreach ($caches as $cache) {
- if (!$cache instanceof CacheInterface) {
- throw new InvalidArgumentException(sprintf('The class "%s" does not implement the "%s" interface.', \get_class($cache), CacheInterface::class));
+ if (!$cache instanceof Psr16CacheInterface) {
+ throw new InvalidArgumentException(sprintf('The class "%s" does not implement the "%s" interface.', \get_class($cache), Psr16CacheInterface::class));
}
}
@@ -87,7 +92,7 @@ public function getMultiple($keys, $default = null)
private function generateItems($values, $cacheIndex, $miss, $default)
{
- $missing = array();
+ $missing = [];
$nextCacheIndex = $cacheIndex + 1;
$nextCache = isset($this->caches[$nextCacheIndex]) ? $this->caches[$nextCacheIndex] : null;
@@ -201,7 +206,7 @@ public function setMultiple($values, $ttl = null)
if ($values instanceof \Traversable) {
$valuesIterator = $values;
$values = function () use ($valuesIterator, &$values) {
- $generatedValues = array();
+ $generatedValues = [];
foreach ($valuesIterator as $key => $value) {
yield $key => $value;
@@ -244,7 +249,7 @@ public function prune()
public function reset()
{
foreach ($this->caches as $cache) {
- if ($cache instanceof ResettableInterface) {
+ if ($cache instanceof ResetInterface) {
$cache->reset();
}
}
diff --git a/Simple/DoctrineCache.php b/Simple/DoctrineCache.php
index 0ba701d7..6a6d0031 100644
--- a/Simple/DoctrineCache.php
+++ b/Simple/DoctrineCache.php
@@ -12,8 +12,15 @@
namespace Symfony\Component\Cache\Simple;
use Doctrine\Common\Cache\CacheProvider;
+use Symfony\Component\Cache\Adapter\DoctrineAdapter;
use Symfony\Component\Cache\Traits\DoctrineTrait;
+use Symfony\Contracts\Cache\CacheInterface;
+@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "%s" and type-hint for "%s" instead.', DoctrineCache::class, DoctrineAdapter::class, CacheInterface::class), E_USER_DEPRECATED);
+
+/**
+ * @deprecated since Symfony 4.3, use DoctrineAdapter and type-hint for CacheInterface instead.
+ */
class DoctrineCache extends AbstractCache
{
use DoctrineTrait;
diff --git a/Simple/FilesystemCache.php b/Simple/FilesystemCache.php
index 37b3d3fa..8891abd9 100644
--- a/Simple/FilesystemCache.php
+++ b/Simple/FilesystemCache.php
@@ -11,15 +11,25 @@
namespace Symfony\Component\Cache\Simple;
+use Symfony\Component\Cache\Adapter\FilesystemAdapter;
+use Symfony\Component\Cache\Marshaller\DefaultMarshaller;
+use Symfony\Component\Cache\Marshaller\MarshallerInterface;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\Traits\FilesystemTrait;
+use Symfony\Contracts\Cache\CacheInterface;
+@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "%s" and type-hint for "%s" instead.', FilesystemCache::class, FilesystemAdapter::class, CacheInterface::class), E_USER_DEPRECATED);
+
+/**
+ * @deprecated since Symfony 4.3, use FilesystemAdapter and type-hint for CacheInterface instead.
+ */
class FilesystemCache extends AbstractCache implements PruneableInterface
{
use FilesystemTrait;
- public function __construct(string $namespace = '', int $defaultLifetime = 0, string $directory = null)
+ public function __construct(string $namespace = '', int $defaultLifetime = 0, string $directory = null, MarshallerInterface $marshaller = null)
{
+ $this->marshaller = $marshaller ?? new DefaultMarshaller();
parent::__construct('', $defaultLifetime);
$this->init($namespace, $directory);
}
diff --git a/Simple/MemcachedCache.php b/Simple/MemcachedCache.php
index 0ff521b9..e1934119 100644
--- a/Simple/MemcachedCache.php
+++ b/Simple/MemcachedCache.php
@@ -11,16 +11,24 @@
namespace Symfony\Component\Cache\Simple;
+use Symfony\Component\Cache\Adapter\MemcachedAdapter;
+use Symfony\Component\Cache\Marshaller\MarshallerInterface;
use Symfony\Component\Cache\Traits\MemcachedTrait;
+use Symfony\Contracts\Cache\CacheInterface;
+@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "%s" and type-hint for "%s" instead.', MemcachedCache::class, MemcachedAdapter::class, CacheInterface::class), E_USER_DEPRECATED);
+
+/**
+ * @deprecated since Symfony 4.3, use MemcachedAdapter and type-hint for CacheInterface instead.
+ */
class MemcachedCache extends AbstractCache
{
use MemcachedTrait;
protected $maxIdLength = 250;
- public function __construct(\Memcached $client, string $namespace = '', int $defaultLifetime = 0)
+ public function __construct(\Memcached $client, string $namespace = '', int $defaultLifetime = 0, MarshallerInterface $marshaller = null)
{
- $this->init($client, $namespace, $defaultLifetime);
+ $this->init($client, $namespace, $defaultLifetime, $marshaller);
}
}
diff --git a/Simple/NullCache.php b/Simple/NullCache.php
index fa986aeb..c4760e1a 100644
--- a/Simple/NullCache.php
+++ b/Simple/NullCache.php
@@ -11,12 +11,16 @@
namespace Symfony\Component\Cache\Simple;
-use Psr\SimpleCache\CacheInterface;
+use Psr\SimpleCache\CacheInterface as Psr16CacheInterface;
+use Symfony\Component\Cache\Adapter\NullAdapter;
+use Symfony\Contracts\Cache\CacheInterface;
+
+@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "%s" and type-hint for "%s" instead.', NullCache::class, NullAdapter::class, CacheInterface::class), E_USER_DEPRECATED);
/**
- * @author Nicolas Grekas
+ * @deprecated since Symfony 4.3, use NullAdapter and type-hint for CacheInterface instead.
*/
-class NullCache implements CacheInterface
+class NullCache implements Psr16CacheInterface
{
/**
* {@inheritdoc}
diff --git a/Simple/PdoCache.php b/Simple/PdoCache.php
index 65b9879c..07849e93 100644
--- a/Simple/PdoCache.php
+++ b/Simple/PdoCache.php
@@ -11,9 +11,17 @@
namespace Symfony\Component\Cache\Simple;
+use Symfony\Component\Cache\Adapter\PdoAdapter;
+use Symfony\Component\Cache\Marshaller\MarshallerInterface;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\Traits\PdoTrait;
+use Symfony\Contracts\Cache\CacheInterface;
+@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "%s" and type-hint for "%s" instead.', PdoCache::class, PdoAdapter::class, CacheInterface::class), E_USER_DEPRECATED);
+
+/**
+ * @deprecated since Symfony 4.3, use PdoAdapter and type-hint for CacheInterface instead.
+ */
class PdoCache extends AbstractCache implements PruneableInterface
{
use PdoTrait;
@@ -25,6 +33,9 @@ class PdoCache extends AbstractCache implements PruneableInterface
* a Doctrine DBAL Connection or a DSN string that will be used to
* lazy-connect to the database when the cache is actually used.
*
+ * When a Doctrine DBAL Connection is passed, the cache table is created
+ * automatically when possible. Otherwise, use the createTable() method.
+ *
* List of available options:
* * db_table: The name of the table [default: cache_items]
* * db_id_col: The column where to store the cache id [default: item_id]
@@ -33,7 +44,7 @@ class PdoCache extends AbstractCache implements PruneableInterface
* * db_time_col: The column where to store the timestamp [default: item_time]
* * db_username: The username when lazy-connect [default: '']
* * db_password: The password when lazy-connect [default: '']
- * * db_connection_options: An array of driver-specific connection options [default: array()]
+ * * db_connection_options: An array of driver-specific connection options [default: []]
*
* @param \PDO|Connection|string $connOrDsn a \PDO or Connection instance or DSN string or null
*
@@ -41,8 +52,8 @@ class PdoCache extends AbstractCache implements PruneableInterface
* @throws InvalidArgumentException When PDO error mode is not PDO::ERRMODE_EXCEPTION
* @throws InvalidArgumentException When namespace contains invalid characters
*/
- public function __construct($connOrDsn, string $namespace = '', int $defaultLifetime = 0, array $options = array())
+ public function __construct($connOrDsn, string $namespace = '', int $defaultLifetime = 0, array $options = [], MarshallerInterface $marshaller = null)
{
- $this->init($connOrDsn, $namespace, $defaultLifetime, $options);
+ $this->init($connOrDsn, $namespace, $defaultLifetime, $options, $marshaller);
}
}
diff --git a/Simple/PhpArrayCache.php b/Simple/PhpArrayCache.php
index 5186ded0..56660935 100644
--- a/Simple/PhpArrayCache.php
+++ b/Simple/PhpArrayCache.php
@@ -11,32 +11,31 @@
namespace Symfony\Component\Cache\Simple;
-use Psr\SimpleCache\CacheInterface;
+use Psr\SimpleCache\CacheInterface as Psr16CacheInterface;
+use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
use Symfony\Component\Cache\Traits\PhpArrayTrait;
+use Symfony\Contracts\Cache\CacheInterface;
+
+@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "%s" and type-hint for "%s" instead.', PhpArrayCache::class, PhpArrayAdapter::class, CacheInterface::class), E_USER_DEPRECATED);
/**
- * Caches items at warm up time using a PHP array that is stored in shared memory by OPCache since PHP 7.0.
- * Warmed up items are read-only and run-time discovered items are cached using a fallback adapter.
- *
- * @author Titouan Galopin
+ * @deprecated since Symfony 4.3, use PhpArrayAdapter and type-hint for CacheInterface instead.
*/
-class PhpArrayCache implements CacheInterface, PruneableInterface, ResettableInterface
+class PhpArrayCache implements Psr16CacheInterface, PruneableInterface, ResettableInterface
{
use PhpArrayTrait;
/**
- * @param string $file The PHP file were values are cached
- * @param CacheInterface $fallbackPool A pool to fallback on when an item is not hit
+ * @param string $file The PHP file were values are cached
+ * @param Psr16CacheInterface $fallbackPool A pool to fallback on when an item is not hit
*/
- public function __construct(string $file, CacheInterface $fallbackPool)
+ public function __construct(string $file, Psr16CacheInterface $fallbackPool)
{
$this->file = $file;
$this->pool = $fallbackPool;
- $this->zendDetectUnicode = ini_get('zend.detect_unicode');
}
/**
@@ -44,12 +43,12 @@ public function __construct(string $file, CacheInterface $fallbackPool)
*
* @param string $file The PHP file were values are cached
*
- * @return CacheInterface
+ * @return Psr16CacheInterface
*/
- public static function create($file, CacheInterface $fallbackPool)
+ public static function create($file, Psr16CacheInterface $fallbackPool)
{
// Shared memory is available in PHP 7.0+ with OPCache enabled
- if (ini_get('opcache.enable')) {
+ if (filter_var(ini_get('opcache.enable'), FILTER_VALIDATE_BOOLEAN)) {
return new static($file, $fallbackPool);
}
@@ -67,18 +66,17 @@ public function get($key, $default = null)
if (null === $this->values) {
$this->initialize();
}
- if (!isset($this->values[$key])) {
+ if (!isset($this->keys[$key])) {
return $this->pool->get($key, $default);
}
-
- $value = $this->values[$key];
+ $value = $this->values[$this->keys[$key]];
if ('N;' === $value) {
return null;
}
- if (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
+ if ($value instanceof \Closure) {
try {
- return unserialize($value);
+ return $value();
} catch (\Throwable $e) {
return $default;
}
@@ -121,7 +119,7 @@ public function has($key)
$this->initialize();
}
- return isset($this->values[$key]) || $this->pool->has($key);
+ return isset($this->keys[$key]) || $this->pool->has($key);
}
/**
@@ -136,7 +134,7 @@ public function delete($key)
$this->initialize();
}
- return !isset($this->values[$key]) && $this->pool->delete($key);
+ return !isset($this->keys[$key]) && $this->pool->delete($key);
}
/**
@@ -149,14 +147,14 @@ public function deleteMultiple($keys)
}
$deleted = true;
- $fallbackKeys = array();
+ $fallbackKeys = [];
foreach ($keys as $key) {
if (!\is_string($key)) {
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', \is_object($key) ? \get_class($key) : \gettype($key)));
}
- if (isset($this->values[$key])) {
+ if (isset($this->keys[$key])) {
$deleted = false;
} else {
$fallbackKeys[] = $key;
@@ -185,7 +183,7 @@ public function set($key, $value, $ttl = null)
$this->initialize();
}
- return !isset($this->values[$key]) && $this->pool->set($key, $value, $ttl);
+ return !isset($this->keys[$key]) && $this->pool->set($key, $value, $ttl);
}
/**
@@ -198,14 +196,14 @@ public function setMultiple($values, $ttl = null)
}
$saved = true;
- $fallbackValues = array();
+ $fallbackValues = [];
foreach ($values as $key => $value) {
if (!\is_string($key) && !\is_int($key)) {
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', \is_object($key) ? \get_class($key) : \gettype($key)));
}
- if (isset($this->values[$key])) {
+ if (isset($this->keys[$key])) {
$saved = false;
} else {
$fallbackValues[$key] = $value;
@@ -221,17 +219,17 @@ public function setMultiple($values, $ttl = null)
private function generateItems(array $keys, $default)
{
- $fallbackKeys = array();
+ $fallbackKeys = [];
foreach ($keys as $key) {
- if (isset($this->values[$key])) {
- $value = $this->values[$key];
+ if (isset($this->keys[$key])) {
+ $value = $this->values[$this->keys[$key]];
if ('N;' === $value) {
yield $key => null;
- } elseif (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
+ } elseif ($value instanceof \Closure) {
try {
- yield $key => unserialize($value);
+ yield $key => $value();
} catch (\Throwable $e) {
yield $key => $default;
}
@@ -244,9 +242,7 @@ private function generateItems(array $keys, $default)
}
if ($fallbackKeys) {
- foreach ($this->pool->getMultiple($fallbackKeys, $default) as $key => $item) {
- yield $key => $item;
- }
+ yield from $this->pool->getMultiple($fallbackKeys, $default);
}
}
}
diff --git a/Simple/PhpFilesCache.php b/Simple/PhpFilesCache.php
index 77239c32..060244a0 100644
--- a/Simple/PhpFilesCache.php
+++ b/Simple/PhpFilesCache.php
@@ -11,27 +11,35 @@
namespace Symfony\Component\Cache\Simple;
+use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
use Symfony\Component\Cache\Exception\CacheException;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\Traits\PhpFilesTrait;
+use Symfony\Contracts\Cache\CacheInterface;
+@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "%s" and type-hint for "%s" instead.', PhpFilesCache::class, PhpFilesAdapter::class, CacheInterface::class), E_USER_DEPRECATED);
+
+/**
+ * @deprecated since Symfony 4.3, use PhpFilesAdapter and type-hint for CacheInterface instead.
+ */
class PhpFilesCache extends AbstractCache implements PruneableInterface
{
use PhpFilesTrait;
/**
+ * @param $appendOnly Set to `true` to gain extra performance when the items stored in this pool never expire.
+ * Doing so is encouraged because it fits perfectly OPcache's memory model.
+ *
* @throws CacheException if OPcache is not enabled
*/
- public function __construct(string $namespace = '', int $defaultLifetime = 0, string $directory = null)
+ public function __construct(string $namespace = '', int $defaultLifetime = 0, string $directory = null, bool $appendOnly = false)
{
- if (!static::isSupported()) {
- throw new CacheException('OPcache is not enabled');
- }
+ $this->appendOnly = $appendOnly;
+ self::$startTime = self::$startTime ?? $_SERVER['REQUEST_TIME'] ?? time();
parent::__construct('', $defaultLifetime);
$this->init($namespace, $directory);
-
- $e = new \Exception();
- $this->includeHandler = function () use ($e) { throw $e; };
- $this->zendDetectUnicode = ini_get('zend.detect_unicode');
+ $this->includeHandler = static function ($type, $msg, $file, $line) {
+ throw new \ErrorException($msg, 0, $type, $file, $line);
+ };
}
}
diff --git a/Simple/Psr6Cache.php b/Simple/Psr6Cache.php
index 482aa137..090f48c1 100644
--- a/Simple/Psr6Cache.php
+++ b/Simple/Psr6Cache.php
@@ -11,218 +11,13 @@
namespace Symfony\Component\Cache\Simple;
-use Psr\Cache\CacheException as Psr6CacheException;
-use Psr\Cache\CacheItemPoolInterface;
-use Psr\SimpleCache\CacheException as SimpleCacheException;
-use Psr\SimpleCache\CacheInterface;
-use Symfony\Component\Cache\Adapter\AbstractAdapter;
-use Symfony\Component\Cache\CacheItem;
-use Symfony\Component\Cache\Exception\InvalidArgumentException;
-use Symfony\Component\Cache\PruneableInterface;
-use Symfony\Component\Cache\ResettableInterface;
-use Symfony\Component\Cache\Traits\ProxyTrait;
+use Symfony\Component\Cache\Psr16Cache;
+
+@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "%s" instead.', Psr6Cache::class, Psr16Cache::class), E_USER_DEPRECATED);
/**
- * @author Nicolas Grekas
+ * @deprecated since Symfony 4.3, use Psr16Cache instead.
*/
-class Psr6Cache implements CacheInterface, PruneableInterface, ResettableInterface
+class Psr6Cache extends Psr16Cache
{
- use ProxyTrait;
-
- private $createCacheItem;
-
- public function __construct(CacheItemPoolInterface $pool)
- {
- $this->pool = $pool;
-
- if ($pool instanceof AbstractAdapter) {
- $this->createCacheItem = \Closure::bind(
- function ($key, $value, $allowInt = false) {
- if ($allowInt && \is_int($key)) {
- $key = (string) $key;
- } else {
- CacheItem::validateKey($key);
- }
- $f = $this->createCacheItem;
-
- return $f($key, $value, false);
- },
- $pool,
- AbstractAdapter::class
- );
- }
- }
-
- /**
- * {@inheritdoc}
- */
- public function get($key, $default = null)
- {
- try {
- $item = $this->pool->getItem($key);
- } catch (SimpleCacheException $e) {
- throw $e;
- } catch (Psr6CacheException $e) {
- throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
- }
-
- return $item->isHit() ? $item->get() : $default;
- }
-
- /**
- * {@inheritdoc}
- */
- public function set($key, $value, $ttl = null)
- {
- try {
- if (null !== $f = $this->createCacheItem) {
- $item = $f($key, $value);
- } else {
- $item = $this->pool->getItem($key)->set($value);
- }
- } catch (SimpleCacheException $e) {
- throw $e;
- } catch (Psr6CacheException $e) {
- throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
- }
- if (null !== $ttl) {
- $item->expiresAfter($ttl);
- }
-
- return $this->pool->save($item);
- }
-
- /**
- * {@inheritdoc}
- */
- public function delete($key)
- {
- try {
- return $this->pool->deleteItem($key);
- } catch (SimpleCacheException $e) {
- throw $e;
- } catch (Psr6CacheException $e) {
- throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
- }
- }
-
- /**
- * {@inheritdoc}
- */
- public function clear()
- {
- return $this->pool->clear();
- }
-
- /**
- * {@inheritdoc}
- */
- public function getMultiple($keys, $default = null)
- {
- if ($keys instanceof \Traversable) {
- $keys = iterator_to_array($keys, false);
- } elseif (!\is_array($keys)) {
- throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', \is_object($keys) ? \get_class($keys) : \gettype($keys)));
- }
-
- try {
- $items = $this->pool->getItems($keys);
- } catch (SimpleCacheException $e) {
- throw $e;
- } catch (Psr6CacheException $e) {
- throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
- }
- $values = array();
-
- foreach ($items as $key => $item) {
- $values[$key] = $item->isHit() ? $item->get() : $default;
- }
-
- return $values;
- }
-
- /**
- * {@inheritdoc}
- */
- public function setMultiple($values, $ttl = null)
- {
- $valuesIsArray = \is_array($values);
- if (!$valuesIsArray && !$values instanceof \Traversable) {
- throw new InvalidArgumentException(sprintf('Cache values must be array or Traversable, "%s" given', \is_object($values) ? \get_class($values) : \gettype($values)));
- }
- $items = array();
-
- try {
- if (null !== $f = $this->createCacheItem) {
- $valuesIsArray = false;
- foreach ($values as $key => $value) {
- $items[$key] = $f($key, $value, true);
- }
- } elseif ($valuesIsArray) {
- $items = array();
- foreach ($values as $key => $value) {
- $items[] = (string) $key;
- }
- $items = $this->pool->getItems($items);
- } else {
- foreach ($values as $key => $value) {
- if (\is_int($key)) {
- $key = (string) $key;
- }
- $items[$key] = $this->pool->getItem($key)->set($value);
- }
- }
- } catch (SimpleCacheException $e) {
- throw $e;
- } catch (Psr6CacheException $e) {
- throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
- }
- $ok = true;
-
- foreach ($items as $key => $item) {
- if ($valuesIsArray) {
- $item->set($values[$key]);
- }
- if (null !== $ttl) {
- $item->expiresAfter($ttl);
- }
- $ok = $this->pool->saveDeferred($item) && $ok;
- }
-
- return $this->pool->commit() && $ok;
- }
-
- /**
- * {@inheritdoc}
- */
- public function deleteMultiple($keys)
- {
- if ($keys instanceof \Traversable) {
- $keys = iterator_to_array($keys, false);
- } elseif (!\is_array($keys)) {
- throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', \is_object($keys) ? \get_class($keys) : \gettype($keys)));
- }
-
- try {
- return $this->pool->deleteItems($keys);
- } catch (SimpleCacheException $e) {
- throw $e;
- } catch (Psr6CacheException $e) {
- throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
- }
- }
-
- /**
- * {@inheritdoc}
- */
- public function has($key)
- {
- try {
- return $this->pool->hasItem($key);
- } catch (SimpleCacheException $e) {
- throw $e;
- } catch (Psr6CacheException $e) {
- throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
- }
- }
}
diff --git a/Simple/RedisCache.php b/Simple/RedisCache.php
index 45bb5ff7..9655a753 100644
--- a/Simple/RedisCache.php
+++ b/Simple/RedisCache.php
@@ -11,19 +11,25 @@
namespace Symfony\Component\Cache\Simple;
+use Symfony\Component\Cache\Adapter\RedisAdapter;
+use Symfony\Component\Cache\Marshaller\MarshallerInterface;
use Symfony\Component\Cache\Traits\RedisTrait;
+use Symfony\Contracts\Cache\CacheInterface;
+@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "%s" and type-hint for "%s" instead.', RedisCache::class, RedisAdapter::class, CacheInterface::class), E_USER_DEPRECATED);
+
+/**
+ * @deprecated since Symfony 4.3, use RedisAdapter and type-hint for CacheInterface instead.
+ */
class RedisCache extends AbstractCache
{
use RedisTrait;
/**
- * @param \Redis|\RedisArray|\RedisCluster|\Predis\Client $redisClient
- * @param string $namespace
- * @param int $defaultLifetime
+ * @param \Redis|\RedisArray|\RedisCluster|\Predis\ClientInterface $redisClient
*/
- public function __construct($redisClient, string $namespace = '', int $defaultLifetime = 0)
+ public function __construct($redisClient, string $namespace = '', int $defaultLifetime = 0, MarshallerInterface $marshaller = null)
{
- $this->init($redisClient, $namespace, $defaultLifetime);
+ $this->init($redisClient, $namespace, $defaultLifetime, $marshaller);
}
}
diff --git a/Simple/TraceableCache.php b/Simple/TraceableCache.php
index 181934ef..ad9bfcf0 100644
--- a/Simple/TraceableCache.php
+++ b/Simple/TraceableCache.php
@@ -11,22 +11,24 @@
namespace Symfony\Component\Cache\Simple;
-use Psr\SimpleCache\CacheInterface;
+use Psr\SimpleCache\CacheInterface as Psr16CacheInterface;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
+use Symfony\Contracts\Cache\CacheInterface;
+use Symfony\Contracts\Service\ResetInterface;
+
+@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.3, use "%s" and type-hint for "%s" instead.', TraceableCache::class, TraceableAdapter::class, CacheInterface::class), E_USER_DEPRECATED);
/**
- * An adapter that collects data about all cache calls.
- *
- * @author Nicolas Grekas
+ * @deprecated since Symfony 4.3, use TraceableAdapter and type-hint for CacheInterface instead.
*/
-class TraceableCache implements CacheInterface, PruneableInterface, ResettableInterface
+class TraceableCache implements Psr16CacheInterface, PruneableInterface, ResettableInterface
{
private $pool;
private $miss;
- private $calls = array();
+ private $calls = [];
- public function __construct(CacheInterface $pool)
+ public function __construct(Psr16CacheInterface $pool)
{
$this->pool = $pool;
$this->miss = new \stdClass();
@@ -99,7 +101,7 @@ public function set($key, $value, $ttl = null)
public function setMultiple($values, $ttl = null)
{
$event = $this->start(__FUNCTION__);
- $event->result['keys'] = array();
+ $event->result['keys'] = [];
if ($values instanceof \Traversable) {
$values = function () use ($values, $event) {
@@ -133,7 +135,7 @@ public function getMultiple($keys, $default = null)
$event->end = microtime(true);
}
$f = function () use ($result, $event, $miss, $default) {
- $event->result = array();
+ $event->result = [];
foreach ($result as $key => $value) {
if ($event->result[$key] = $miss !== $value) {
++$event->hits;
@@ -200,7 +202,7 @@ public function prune()
*/
public function reset()
{
- if (!$this->pool instanceof ResettableInterface) {
+ if (!$this->pool instanceof ResetInterface) {
return;
}
$event = $this->start(__FUNCTION__);
@@ -216,7 +218,7 @@ public function getCalls()
try {
return $this->calls;
} finally {
- $this->calls = array();
+ $this->calls = [];
}
}
diff --git a/Tests/Adapter/AbstractRedisAdapterTest.php b/Tests/Adapter/AbstractRedisAdapterTest.php
index 147dfcd1..2d102406 100644
--- a/Tests/Adapter/AbstractRedisAdapterTest.php
+++ b/Tests/Adapter/AbstractRedisAdapterTest.php
@@ -15,11 +15,11 @@
abstract class AbstractRedisAdapterTest extends AdapterTestCase
{
- protected $skippedTests = array(
+ protected $skippedTests = [
'testExpiration' => 'Testing expiration slows down the test suite',
'testHasItemReturnsFalseWhenDeferredItemIsExpired' => 'Testing expiration slows down the test suite',
'testDefaultLifeTime' => 'Testing expiration slows down the test suite',
- );
+ ];
protected static $redis;
@@ -28,7 +28,7 @@ public function createCachePool($defaultLifetime = 0)
return new RedisAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);
}
- public static function setupBeforeClass()
+ public static function setUpBeforeClass(): void
{
if (!\extension_loaded('redis')) {
self::markTestSkipped('Extension redis required.');
@@ -39,7 +39,7 @@ public static function setupBeforeClass()
}
}
- public static function tearDownAfterClass()
+ public static function tearDownAfterClass(): void
{
self::$redis = null;
}
diff --git a/Tests/Adapter/AdapterTestCase.php b/Tests/Adapter/AdapterTestCase.php
index 018d1494..a6132ebc 100644
--- a/Tests/Adapter/AdapterTestCase.php
+++ b/Tests/Adapter/AdapterTestCase.php
@@ -12,20 +12,117 @@
namespace Symfony\Component\Cache\Tests\Adapter;
use Cache\IntegrationTests\CachePoolTest;
+use PHPUnit\Framework\Assert;
+use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
+use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\PruneableInterface;
+use Symfony\Contracts\Cache\CallbackInterface;
abstract class AdapterTestCase extends CachePoolTest
{
- protected function setUp()
+ protected function setUp(): void
{
parent::setUp();
- if (!array_key_exists('testPrune', $this->skippedTests) && !$this->createCachePool() instanceof PruneableInterface) {
+ if (!\array_key_exists('testPrune', $this->skippedTests) && !$this->createCachePool() instanceof PruneableInterface) {
$this->skippedTests['testPrune'] = 'Not a pruneable cache pool.';
}
}
+ public function testGet()
+ {
+ if (isset($this->skippedTests[__FUNCTION__])) {
+ $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
+ }
+
+ $cache = $this->createCachePool();
+ $cache->clear();
+
+ $value = mt_rand();
+
+ $this->assertSame($value, $cache->get('foo', function (CacheItem $item) use ($value) {
+ $this->assertSame('foo', $item->getKey());
+
+ return $value;
+ }));
+
+ $item = $cache->getItem('foo');
+ $this->assertSame($value, $item->get());
+
+ $isHit = true;
+ $this->assertSame($value, $cache->get('foo', function (CacheItem $item) use (&$isHit) { $isHit = false; }, 0));
+ $this->assertTrue($isHit);
+
+ $this->assertNull($cache->get('foo', function (CacheItem $item) use (&$isHit, $value) {
+ $isHit = false;
+ $this->assertTrue($item->isHit());
+ $this->assertSame($value, $item->get());
+ }, INF));
+ $this->assertFalse($isHit);
+
+ $this->assertSame($value, $cache->get('bar', new class($value) implements CallbackInterface {
+ private $value;
+
+ public function __construct(int $value)
+ {
+ $this->value = $value;
+ }
+
+ public function __invoke(CacheItemInterface $item, bool &$save)
+ {
+ Assert::assertSame('bar', $item->getKey());
+
+ return $this->value;
+ }
+ }));
+ }
+
+ public function testRecursiveGet()
+ {
+ if (isset($this->skippedTests[__FUNCTION__])) {
+ $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
+ }
+
+ $cache = $this->createCachePool(0, __FUNCTION__);
+
+ $v = $cache->get('k1', function () use (&$counter, $cache) {
+ $cache->get('k2', function () use (&$counter) { return ++$counter; });
+ $v = $cache->get('k2', function () use (&$counter) { return ++$counter; }); // ensure the callback is called once
+
+ return $v;
+ });
+
+ $this->assertSame(1, $counter);
+ $this->assertSame(1, $v);
+ $this->assertSame(1, $cache->get('k2', function () { return 2; }));
+ }
+
+ public function testGetMetadata()
+ {
+ if (isset($this->skippedTests[__FUNCTION__])) {
+ $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
+ }
+
+ $cache = $this->createCachePool(0, __FUNCTION__);
+
+ $cache->deleteItem('foo');
+ $cache->get('foo', function ($item) {
+ $item->expiresAfter(10);
+ usleep(999000);
+
+ return 'bar';
+ });
+
+ $item = $cache->getItem('foo');
+
+ $expected = [
+ CacheItem::METADATA_EXPIRY => 9.5 + time(),
+ CacheItem::METADATA_CTIME => 1000,
+ ];
+ $this->assertEqualsWithDelta($expected, $item->getMetadata(), .6, 'Item metadata should embed expiry and ctime.');
+ }
+
public function testDefaultLifeTime()
{
if (isset($this->skippedTests[__FUNCTION__])) {
@@ -81,11 +178,11 @@ public function testNotUnserializable()
$item = $cache->getItem('foo');
$this->assertFalse($item->isHit());
- foreach ($cache->getItems(array('foo')) as $item) {
+ foreach ($cache->getItems(['foo']) as $item) {
}
$cache->save($item->set(new NotUnserializable()));
- foreach ($cache->getItems(array('foo')) as $item) {
+ foreach ($cache->getItems(['foo']) as $item) {
}
$this->assertFalse($item->isHit());
}
@@ -157,14 +254,9 @@ public function testPrune()
}
}
-class NotUnserializable implements \Serializable
+class NotUnserializable
{
- public function serialize()
- {
- return serialize(123);
- }
-
- public function unserialize($ser)
+ public function __wakeup()
{
throw new \Exception(__CLASS__);
}
diff --git a/Tests/Adapter/ApcuAdapterTest.php b/Tests/Adapter/ApcuAdapterTest.php
index 2b3c6b44..5cca73f5 100644
--- a/Tests/Adapter/ApcuAdapterTest.php
+++ b/Tests/Adapter/ApcuAdapterTest.php
@@ -16,18 +16,18 @@
class ApcuAdapterTest extends AdapterTestCase
{
- protected $skippedTests = array(
+ protected $skippedTests = [
'testExpiration' => 'Testing expiration slows down the test suite',
'testHasItemReturnsFalseWhenDeferredItemIsExpired' => 'Testing expiration slows down the test suite',
'testDefaultLifeTime' => 'Testing expiration slows down the test suite',
- );
+ ];
public function createCachePool($defaultLifetime = 0)
{
- if (!\function_exists('apcu_fetch') || !ini_get('apc.enabled')) {
+ if (!\function_exists('apcu_fetch') || !filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN)) {
$this->markTestSkipped('APCu extension is required.');
}
- if ('cli' === \PHP_SAPI && !ini_get('apc.enable_cli')) {
+ if ('cli' === \PHP_SAPI && !filter_var(ini_get('apc.enable_cli'), FILTER_VALIDATE_BOOLEAN)) {
if ('testWithCliSapi' !== $this->getName()) {
$this->markTestSkipped('apc.enable_cli=1 is required.');
}
diff --git a/Tests/Adapter/ArrayAdapterTest.php b/Tests/Adapter/ArrayAdapterTest.php
index 725d7901..5c72dc6e 100644
--- a/Tests/Adapter/ArrayAdapterTest.php
+++ b/Tests/Adapter/ArrayAdapterTest.php
@@ -18,10 +18,11 @@
*/
class ArrayAdapterTest extends AdapterTestCase
{
- protected $skippedTests = array(
+ protected $skippedTests = [
+ 'testGetMetadata' => 'ArrayAdapter does not keep metadata.',
'testDeferredSaveWithoutCommit' => 'Assumes a shared cache which ArrayAdapter is not.',
'testSaveWithoutExpire' => 'Assumes a shared cache which ArrayAdapter is not.',
- );
+ ];
public function createCachePool($defaultLifetime = 0)
{
@@ -35,12 +36,12 @@ public function testGetValuesHitAndMiss()
// Hit
$item = $cache->getItem('foo');
- $item->set('4711');
+ $item->set('::4711');
$cache->save($item);
$fooItem = $cache->getItem('foo');
$this->assertTrue($fooItem->isHit());
- $this->assertEquals('4711', $fooItem->get());
+ $this->assertEquals('::4711', $fooItem->get());
// Miss (should be present as NULL in $values)
$cache->getItem('bar');
@@ -49,7 +50,7 @@ public function testGetValuesHitAndMiss()
$this->assertCount(2, $values);
$this->assertArrayHasKey('foo', $values);
- $this->assertSame(serialize('4711'), $values['foo']);
+ $this->assertSame(serialize('::4711'), $values['foo']);
$this->assertArrayHasKey('bar', $values);
$this->assertNull($values['bar']);
}
diff --git a/Tests/Adapter/ChainAdapterTest.php b/Tests/Adapter/ChainAdapterTest.php
index a47058f5..ac920064 100644
--- a/Tests/Adapter/ChainAdapterTest.php
+++ b/Tests/Adapter/ChainAdapterTest.php
@@ -11,6 +11,7 @@
namespace Symfony\Component\Cache\Tests\Adapter;
+use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\ChainAdapter;
@@ -24,27 +25,27 @@
*/
class ChainAdapterTest extends AdapterTestCase
{
- public function createCachePool($defaultLifetime = 0)
+ public function createCachePool($defaultLifetime = 0, $testMethod = null)
{
- return new ChainAdapter(array(new ArrayAdapter($defaultLifetime), new ExternalAdapter(), new FilesystemAdapter('', $defaultLifetime)), $defaultLifetime);
+ if ('testGetMetadata' === $testMethod) {
+ return new ChainAdapter([new FilesystemAdapter('', $defaultLifetime)], $defaultLifetime);
+ }
+
+ return new ChainAdapter([new ArrayAdapter($defaultLifetime), new ExternalAdapter(), new FilesystemAdapter('', $defaultLifetime)], $defaultLifetime);
}
- /**
- * @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
- * @expectedExceptionMessage At least one adapter must be specified.
- */
public function testEmptyAdaptersException()
{
- new ChainAdapter(array());
+ $this->expectException('Symfony\Component\Cache\Exception\InvalidArgumentException');
+ $this->expectExceptionMessage('At least one adapter must be specified.');
+ new ChainAdapter([]);
}
- /**
- * @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
- * @expectedExceptionMessage The class "stdClass" does not implement
- */
public function testInvalidAdapterException()
{
- new ChainAdapter(array(new \stdClass()));
+ $this->expectException('Symfony\Component\Cache\Exception\InvalidArgumentException');
+ $this->expectExceptionMessage('The class "stdClass" does not implement');
+ new ChainAdapter([new \stdClass()]);
}
public function testPrune()
@@ -53,23 +54,23 @@ public function testPrune()
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}
- $cache = new ChainAdapter(array(
+ $cache = new ChainAdapter([
$this->getPruneableMock(),
$this->getNonPruneableMock(),
$this->getPruneableMock(),
- ));
+ ]);
$this->assertTrue($cache->prune());
- $cache = new ChainAdapter(array(
+ $cache = new ChainAdapter([
$this->getPruneableMock(),
$this->getFailingPruneableMock(),
$this->getPruneableMock(),
- ));
+ ]);
$this->assertFalse($cache->prune());
}
/**
- * @return \PHPUnit_Framework_MockObject_MockObject|PruneableCacheInterface
+ * @return MockObject|PruneableCacheInterface
*/
private function getPruneableMock()
{
@@ -80,13 +81,13 @@ private function getPruneableMock()
$pruneable
->expects($this->atLeastOnce())
->method('prune')
- ->will($this->returnValue(true));
+ ->willReturn(true);
return $pruneable;
}
/**
- * @return \PHPUnit_Framework_MockObject_MockObject|PruneableCacheInterface
+ * @return MockObject|PruneableCacheInterface
*/
private function getFailingPruneableMock()
{
@@ -97,13 +98,13 @@ private function getFailingPruneableMock()
$pruneable
->expects($this->atLeastOnce())
->method('prune')
- ->will($this->returnValue(false));
+ ->willReturn(false);
return $pruneable;
}
/**
- * @return \PHPUnit_Framework_MockObject_MockObject|AdapterInterface
+ * @return MockObject|AdapterInterface
*/
private function getNonPruneableMock()
{
diff --git a/Tests/Adapter/DoctrineAdapterTest.php b/Tests/Adapter/DoctrineAdapterTest.php
index 8d4dfe28..8f520cb5 100644
--- a/Tests/Adapter/DoctrineAdapterTest.php
+++ b/Tests/Adapter/DoctrineAdapterTest.php
@@ -19,11 +19,11 @@
*/
class DoctrineAdapterTest extends AdapterTestCase
{
- protected $skippedTests = array(
+ protected $skippedTests = [
'testDeferredSaveWithoutCommit' => 'Assumes a shared cache which ArrayCache is not.',
'testSaveWithoutExpire' => 'Assumes a shared cache which ArrayCache is not.',
'testNotUnserializable' => 'ArrayCache does not use serialize/unserialize',
- );
+ ];
public function createCachePool($defaultLifetime = 0)
{
diff --git a/Tests/Adapter/FilesystemAdapterTest.php b/Tests/Adapter/FilesystemAdapterTest.php
index fa830682..b7a69cb4 100644
--- a/Tests/Adapter/FilesystemAdapterTest.php
+++ b/Tests/Adapter/FilesystemAdapterTest.php
@@ -24,7 +24,7 @@ public function createCachePool($defaultLifetime = 0)
return new FilesystemAdapter('', $defaultLifetime);
}
- public static function tearDownAfterClass()
+ public static function tearDownAfterClass(): void
{
self::rmdir(sys_get_temp_dir().'/symfony-cache');
}
diff --git a/Tests/Adapter/FilesystemTagAwareAdapterTest.php b/Tests/Adapter/FilesystemTagAwareAdapterTest.php
new file mode 100644
index 00000000..83a7ea52
--- /dev/null
+++ b/Tests/Adapter/FilesystemTagAwareAdapterTest.php
@@ -0,0 +1,28 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\Tests\Adapter;
+
+use Symfony\Component\Cache\Adapter\FilesystemTagAwareAdapter;
+use Symfony\Component\Cache\Tests\Traits\TagAwareTestTrait;
+
+/**
+ * @group time-sensitive
+ */
+class FilesystemTagAwareAdapterTest extends FilesystemAdapterTest
+{
+ use TagAwareTestTrait;
+
+ public function createCachePool($defaultLifetime = 0)
+ {
+ return new FilesystemTagAwareAdapter('', $defaultLifetime);
+ }
+}
diff --git a/Tests/Adapter/MaxIdLengthAdapterTest.php b/Tests/Adapter/MaxIdLengthAdapterTest.php
index 5e301f25..8d0b70d6 100644
--- a/Tests/Adapter/MaxIdLengthAdapterTest.php
+++ b/Tests/Adapter/MaxIdLengthAdapterTest.php
@@ -19,15 +19,15 @@ class MaxIdLengthAdapterTest extends TestCase
public function testLongKey()
{
$cache = $this->getMockBuilder(MaxIdLengthAdapter::class)
- ->setConstructorArgs(array(str_repeat('-', 10)))
- ->setMethods(array('doHave', 'doFetch', 'doDelete', 'doSave', 'doClear'))
+ ->setConstructorArgs([str_repeat('-', 10)])
+ ->setMethods(['doHave', 'doFetch', 'doDelete', 'doSave', 'doClear'])
->getMock();
$cache->expects($this->exactly(2))
->method('doHave')
->withConsecutive(
- array($this->equalTo('----------:0GTYWa9n4ed8vqNlOT2iEr:')),
- array($this->equalTo('----------:---------------------------------------'))
+ [$this->equalTo('----------:nWfzGiCgLczv3SSUzXL3kg:')],
+ [$this->equalTo('----------:---------------------------------------')]
);
$cache->hasItem(str_repeat('-', 40));
@@ -37,39 +37,41 @@ public function testLongKey()
public function testLongKeyVersioning()
{
$cache = $this->getMockBuilder(MaxIdLengthAdapter::class)
- ->setConstructorArgs(array(str_repeat('-', 26)))
+ ->setConstructorArgs([str_repeat('-', 26)])
->getMock();
+ $cache
+ ->method('doFetch')
+ ->willReturn(['2:']);
+
$reflectionClass = new \ReflectionClass(AbstractAdapter::class);
$reflectionMethod = $reflectionClass->getMethod('getId');
$reflectionMethod->setAccessible(true);
// No versioning enabled
- $this->assertEquals('--------------------------:------------', $reflectionMethod->invokeArgs($cache, array(str_repeat('-', 12))));
- $this->assertLessThanOrEqual(50, \strlen($reflectionMethod->invokeArgs($cache, array(str_repeat('-', 12)))));
- $this->assertLessThanOrEqual(50, \strlen($reflectionMethod->invokeArgs($cache, array(str_repeat('-', 23)))));
- $this->assertLessThanOrEqual(50, \strlen($reflectionMethod->invokeArgs($cache, array(str_repeat('-', 40)))));
+ $this->assertEquals('--------------------------:------------', $reflectionMethod->invokeArgs($cache, [str_repeat('-', 12)]));
+ $this->assertLessThanOrEqual(50, \strlen($reflectionMethod->invokeArgs($cache, [str_repeat('-', 12)])));
+ $this->assertLessThanOrEqual(50, \strlen($reflectionMethod->invokeArgs($cache, [str_repeat('-', 23)])));
+ $this->assertLessThanOrEqual(50, \strlen($reflectionMethod->invokeArgs($cache, [str_repeat('-', 40)])));
$reflectionProperty = $reflectionClass->getProperty('versioningIsEnabled');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($cache, true);
// Versioning enabled
- $this->assertEquals('--------------------------:1:------------', $reflectionMethod->invokeArgs($cache, array(str_repeat('-', 12))));
- $this->assertLessThanOrEqual(50, \strlen($reflectionMethod->invokeArgs($cache, array(str_repeat('-', 12)))));
- $this->assertLessThanOrEqual(50, \strlen($reflectionMethod->invokeArgs($cache, array(str_repeat('-', 23)))));
- $this->assertLessThanOrEqual(50, \strlen($reflectionMethod->invokeArgs($cache, array(str_repeat('-', 40)))));
+ $this->assertEquals('--------------------------:2:------------', $reflectionMethod->invokeArgs($cache, [str_repeat('-', 12)]));
+ $this->assertLessThanOrEqual(50, \strlen($reflectionMethod->invokeArgs($cache, [str_repeat('-', 12)])));
+ $this->assertLessThanOrEqual(50, \strlen($reflectionMethod->invokeArgs($cache, [str_repeat('-', 23)])));
+ $this->assertLessThanOrEqual(50, \strlen($reflectionMethod->invokeArgs($cache, [str_repeat('-', 40)])));
}
- /**
- * @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
- * @expectedExceptionMessage Namespace must be 26 chars max, 40 given ("----------------------------------------")
- */
public function testTooLongNamespace()
{
- $cache = $this->getMockBuilder(MaxIdLengthAdapter::class)
- ->setConstructorArgs(array(str_repeat('-', 40)))
+ $this->expectException('Symfony\Component\Cache\Exception\InvalidArgumentException');
+ $this->expectExceptionMessage('Namespace must be 26 chars max, 40 given ("----------------------------------------")');
+ $this->getMockBuilder(MaxIdLengthAdapter::class)
+ ->setConstructorArgs([str_repeat('-', 40)])
->getMock();
}
}
diff --git a/Tests/Adapter/MemcachedAdapterTest.php b/Tests/Adapter/MemcachedAdapterTest.php
index d1f87903..9f77072b 100644
--- a/Tests/Adapter/MemcachedAdapterTest.php
+++ b/Tests/Adapter/MemcachedAdapterTest.php
@@ -16,19 +16,19 @@
class MemcachedAdapterTest extends AdapterTestCase
{
- protected $skippedTests = array(
+ protected $skippedTests = [
'testHasItemReturnsFalseWhenDeferredItemIsExpired' => 'Testing expiration slows down the test suite',
'testDefaultLifeTime' => 'Testing expiration slows down the test suite',
- );
+ ];
protected static $client;
- public static function setupBeforeClass()
+ public static function setUpBeforeClass(): void
{
if (!MemcachedAdapter::isSupported()) {
self::markTestSkipped('Extension memcached >=2.2.0 required.');
}
- self::$client = AbstractAdapter::createConnection('memcached://'.getenv('MEMCACHED_HOST'), array('binary_protocol' => false));
+ self::$client = AbstractAdapter::createConnection('memcached://'.getenv('MEMCACHED_HOST'), ['binary_protocol' => false]);
self::$client->get('foo');
$code = self::$client->getResultCode();
@@ -46,13 +46,13 @@ public function createCachePool($defaultLifetime = 0)
public function testOptions()
{
- $client = MemcachedAdapter::createConnection(array(), array(
+ $client = MemcachedAdapter::createConnection([], [
'libketama_compatible' => false,
'distribution' => 'modula',
'compression' => true,
'serializer' => 'php',
'hash' => 'md5',
- ));
+ ]);
$this->assertSame(\Memcached::SERIALIZER_PHP, $client->getOption(\Memcached::OPT_SERIALIZER));
$this->assertSame(\Memcached::HASH_MD5, $client->getOption(\Memcached::OPT_HASH));
@@ -63,29 +63,29 @@ public function testOptions()
/**
* @dataProvider provideBadOptions
- * @expectedException \ErrorException
- * @expectedExceptionMessage constant(): Couldn't find constant Memcached::
*/
public function testBadOptions($name, $value)
{
- MemcachedAdapter::createConnection(array(), array($name => $value));
+ $this->expectException('ErrorException');
+ $this->expectExceptionMessage('constant(): Couldn\'t find constant Memcached::');
+ MemcachedAdapter::createConnection([], [$name => $value]);
}
public function provideBadOptions()
{
- return array(
- array('foo', 'bar'),
- array('hash', 'zyx'),
- array('serializer', 'zyx'),
- array('distribution', 'zyx'),
- );
+ return [
+ ['foo', 'bar'],
+ ['hash', 'zyx'],
+ ['serializer', 'zyx'],
+ ['distribution', 'zyx'],
+ ];
}
public function testDefaultOptions()
{
$this->assertTrue(MemcachedAdapter::isSupported());
- $client = MemcachedAdapter::createConnection(array());
+ $client = MemcachedAdapter::createConnection([]);
$this->assertTrue($client->getOption(\Memcached::OPT_COMPRESSION));
$this->assertSame(1, $client->getOption(\Memcached::OPT_BINARY_PROTOCOL));
@@ -93,17 +93,15 @@ public function testDefaultOptions()
$this->assertSame(1, $client->getOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE));
}
- /**
- * @expectedException \Symfony\Component\Cache\Exception\CacheException
- * @expectedExceptionMessage MemcachedAdapter: "serializer" option must be "php" or "igbinary".
- */
public function testOptionSerializer()
{
+ $this->expectException('Symfony\Component\Cache\Exception\CacheException');
+ $this->expectExceptionMessage('MemcachedAdapter: "serializer" option must be "php" or "igbinary".');
if (!\Memcached::HAVE_JSON) {
$this->markTestSkipped('Memcached::HAVE_JSON required');
}
- new MemcachedAdapter(MemcachedAdapter::createConnection(array(), array('serializer' => 'json')));
+ new MemcachedAdapter(MemcachedAdapter::createConnection([], ['serializer' => 'json']));
}
/**
@@ -112,54 +110,54 @@ public function testOptionSerializer()
public function testServersSetting($dsn, $host, $port)
{
$client1 = MemcachedAdapter::createConnection($dsn);
- $client2 = MemcachedAdapter::createConnection(array($dsn));
- $client3 = MemcachedAdapter::createConnection(array(array($host, $port)));
- $expect = array(
+ $client2 = MemcachedAdapter::createConnection([$dsn]);
+ $client3 = MemcachedAdapter::createConnection([[$host, $port]]);
+ $expect = [
'host' => $host,
'port' => $port,
- );
+ ];
- $f = function ($s) { return array('host' => $s['host'], 'port' => $s['port']); };
- $this->assertSame(array($expect), array_map($f, $client1->getServerList()));
- $this->assertSame(array($expect), array_map($f, $client2->getServerList()));
- $this->assertSame(array($expect), array_map($f, $client3->getServerList()));
+ $f = function ($s) { return ['host' => $s['host'], 'port' => $s['port']]; };
+ $this->assertSame([$expect], array_map($f, $client1->getServerList()));
+ $this->assertSame([$expect], array_map($f, $client2->getServerList()));
+ $this->assertSame([$expect], array_map($f, $client3->getServerList()));
}
public function provideServersSetting()
{
- yield array(
+ yield [
'memcached://127.0.0.1/50',
'127.0.0.1',
11211,
- );
- yield array(
+ ];
+ yield [
'memcached://localhost:11222?weight=25',
'localhost',
11222,
- );
- if (ini_get('memcached.use_sasl')) {
- yield array(
+ ];
+ if (filter_var(ini_get('memcached.use_sasl'), FILTER_VALIDATE_BOOLEAN)) {
+ yield [
'memcached://user:password@127.0.0.1?weight=50',
'127.0.0.1',
11211,
- );
+ ];
}
- yield array(
+ yield [
'memcached:///var/run/memcached.sock?weight=25',
'/var/run/memcached.sock',
0,
- );
- yield array(
+ ];
+ yield [
'memcached:///var/local/run/memcached.socket?weight=25',
'/var/local/run/memcached.socket',
0,
- );
- if (ini_get('memcached.use_sasl')) {
- yield array(
+ ];
+ if (filter_var(ini_get('memcached.use_sasl'), FILTER_VALIDATE_BOOLEAN)) {
+ yield [
'memcached://user:password@/var/local/run/memcached.socket?weight=25',
'/var/local/run/memcached.socket',
0,
- );
+ ];
}
}
@@ -181,15 +179,62 @@ public function provideDsnWithOptions()
self::markTestSkipped('Extension memcached required.');
}
- yield array(
+ yield [
'memcached://localhost:11222?retry_timeout=10',
- array(\Memcached::OPT_RETRY_TIMEOUT => 8),
- array(\Memcached::OPT_RETRY_TIMEOUT => 10),
- );
- yield array(
+ [\Memcached::OPT_RETRY_TIMEOUT => 8],
+ [\Memcached::OPT_RETRY_TIMEOUT => 10],
+ ];
+ yield [
'memcached://localhost:11222?socket_recv_size=1&socket_send_size=2',
- array(\Memcached::OPT_RETRY_TIMEOUT => 8),
- array(\Memcached::OPT_SOCKET_RECV_SIZE => 1, \Memcached::OPT_SOCKET_SEND_SIZE => 2, \Memcached::OPT_RETRY_TIMEOUT => 8),
- );
+ [\Memcached::OPT_RETRY_TIMEOUT => 8],
+ [\Memcached::OPT_SOCKET_RECV_SIZE => 1, \Memcached::OPT_SOCKET_SEND_SIZE => 2, \Memcached::OPT_RETRY_TIMEOUT => 8],
+ ];
+ }
+
+ public function testClear()
+ {
+ $this->assertTrue($this->createCachePool()->clear());
+ }
+
+ public function testMultiServerDsn()
+ {
+ $dsn = 'memcached:?host[localhost]&host[localhost:12345]&host[/some/memcached.sock:]=3';
+ $client = MemcachedAdapter::createConnection($dsn);
+
+ $expected = [
+ 0 => [
+ 'host' => 'localhost',
+ 'port' => 11211,
+ 'type' => 'TCP',
+ ],
+ 1 => [
+ 'host' => 'localhost',
+ 'port' => 12345,
+ 'type' => 'TCP',
+ ],
+ 2 => [
+ 'host' => '/some/memcached.sock',
+ 'port' => 0,
+ 'type' => 'SOCKET',
+ ],
+ ];
+ $this->assertSame($expected, $client->getServerList());
+
+ $dsn = 'memcached://localhost?host[foo.bar]=3';
+ $client = MemcachedAdapter::createConnection($dsn);
+
+ $expected = [
+ 0 => [
+ 'host' => 'localhost',
+ 'port' => 11211,
+ 'type' => 'TCP',
+ ],
+ 1 => [
+ 'host' => 'foo.bar',
+ 'port' => 11211,
+ 'type' => 'TCP',
+ ],
+ ];
+ $this->assertSame($expected, $client->getServerList());
}
}
diff --git a/Tests/Adapter/NamespacedProxyAdapterTest.php b/Tests/Adapter/NamespacedProxyAdapterTest.php
index c2714033..f1ffcbb8 100644
--- a/Tests/Adapter/NamespacedProxyAdapterTest.php
+++ b/Tests/Adapter/NamespacedProxyAdapterTest.php
@@ -12,6 +12,7 @@
namespace Symfony\Component\Cache\Tests\Adapter;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
+use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\ProxyAdapter;
/**
@@ -19,8 +20,12 @@
*/
class NamespacedProxyAdapterTest extends ProxyAdapterTest
{
- public function createCachePool($defaultLifetime = 0)
+ public function createCachePool($defaultLifetime = 0, $testMethod = null)
{
+ if ('testGetMetadata' === $testMethod) {
+ return new ProxyAdapter(new FilesystemAdapter(), 'foo', $defaultLifetime);
+ }
+
return new ProxyAdapter(new ArrayAdapter($defaultLifetime), 'foo', $defaultLifetime);
}
}
diff --git a/Tests/Adapter/NullAdapterTest.php b/Tests/Adapter/NullAdapterTest.php
index 73e5cad5..ae3de76d 100644
--- a/Tests/Adapter/NullAdapterTest.php
+++ b/Tests/Adapter/NullAdapterTest.php
@@ -34,6 +34,19 @@ public function testGetItem()
$this->assertNull($item->get(), "Item's value must be null when isHit is false.");
}
+ public function testGet()
+ {
+ $adapter = $this->createCachePool();
+
+ $fetched = [];
+ $adapter->get('myKey', function ($item) use (&$fetched) { $fetched[] = $item; });
+ $this->assertCount(1, $fetched);
+ $item = $fetched[0];
+ $this->assertFalse($item->isHit());
+ $this->assertNull($item->get(), "Item's value must be null when isHit is false.");
+ $this->assertSame('myKey', $item->getKey());
+ }
+
public function testHasItem()
{
$this->assertFalse($this->createCachePool()->hasItem('key'));
@@ -43,7 +56,7 @@ public function testGetItems()
{
$adapter = $this->createCachePool();
- $keys = array('foo', 'bar', 'baz', 'biz');
+ $keys = ['foo', 'bar', 'baz', 'biz'];
/** @var CacheItemInterface[] $items */
$items = $adapter->getItems($keys);
@@ -89,7 +102,7 @@ public function testDeleteItem()
public function testDeleteItems()
{
- $this->assertTrue($this->createCachePool()->deleteItems(array('key', 'foo', 'bar')));
+ $this->assertTrue($this->createCachePool()->deleteItems(['key', 'foo', 'bar']));
}
public function testSave()
diff --git a/Tests/Adapter/PdoAdapterTest.php b/Tests/Adapter/PdoAdapterTest.php
index b587cf6d..cd4b95cf 100644
--- a/Tests/Adapter/PdoAdapterTest.php
+++ b/Tests/Adapter/PdoAdapterTest.php
@@ -23,7 +23,7 @@ class PdoAdapterTest extends AdapterTestCase
protected static $dbFile;
- public static function setupBeforeClass()
+ public static function setUpBeforeClass(): void
{
if (!\extension_loaded('pdo_sqlite')) {
self::markTestSkipped('Extension pdo_sqlite required.');
@@ -35,7 +35,7 @@ public static function setupBeforeClass()
$pool->createTable();
}
- public static function tearDownAfterClass()
+ public static function tearDownAfterClass(): void
{
@unlink(self::$dbFile);
}
diff --git a/Tests/Adapter/PdoDbalAdapterTest.php b/Tests/Adapter/PdoDbalAdapterTest.php
index f89a27ce..d4071bae 100644
--- a/Tests/Adapter/PdoDbalAdapterTest.php
+++ b/Tests/Adapter/PdoDbalAdapterTest.php
@@ -24,25 +24,22 @@ class PdoDbalAdapterTest extends AdapterTestCase
protected static $dbFile;
- public static function setupBeforeClass()
+ public static function setUpBeforeClass(): void
{
if (!\extension_loaded('pdo_sqlite')) {
self::markTestSkipped('Extension pdo_sqlite required.');
}
self::$dbFile = tempnam(sys_get_temp_dir(), 'sf_sqlite_cache');
-
- $pool = new PdoAdapter(DriverManager::getConnection(array('driver' => 'pdo_sqlite', 'path' => self::$dbFile)));
- $pool->createTable();
}
- public static function tearDownAfterClass()
+ public static function tearDownAfterClass(): void
{
@unlink(self::$dbFile);
}
public function createCachePool($defaultLifetime = 0)
{
- return new PdoAdapter(DriverManager::getConnection(array('driver' => 'pdo_sqlite', 'path' => self::$dbFile)), '', $defaultLifetime);
+ return new PdoAdapter(DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile]), '', $defaultLifetime);
}
}
diff --git a/Tests/Adapter/PhpArrayAdapterTest.php b/Tests/Adapter/PhpArrayAdapterTest.php
index 930594fb..c4055fb7 100644
--- a/Tests/Adapter/PhpArrayAdapterTest.php
+++ b/Tests/Adapter/PhpArrayAdapterTest.php
@@ -12,6 +12,7 @@
namespace Symfony\Component\Cache\Tests\Adapter;
use Psr\Cache\CacheItemInterface;
+use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\NullAdapter;
use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
@@ -20,7 +21,9 @@
*/
class PhpArrayAdapterTest extends AdapterTestCase
{
- protected $skippedTests = array(
+ protected $skippedTests = [
+ 'testGet' => 'PhpArrayAdapter is read-only.',
+ 'testRecursiveGet' => 'PhpArrayAdapter is read-only.',
'testBasicUsage' => 'PhpArrayAdapter is read-only.',
'testBasicUsageWithLongKey' => 'PhpArrayAdapter is read-only.',
'testClear' => 'PhpArrayAdapter is read-only.',
@@ -51,45 +54,49 @@ class PhpArrayAdapterTest extends AdapterTestCase
'testDefaultLifeTime' => 'PhpArrayAdapter does not allow configuring a default lifetime.',
'testPrune' => 'PhpArrayAdapter just proxies',
- );
+ ];
protected static $file;
- public static function setupBeforeClass()
+ public static function setUpBeforeClass(): void
{
self::$file = sys_get_temp_dir().'/symfony-cache/php-array-adapter-test.php';
}
- protected function tearDown()
+ protected function tearDown(): void
{
if (file_exists(sys_get_temp_dir().'/symfony-cache')) {
FilesystemAdapterTest::rmdir(sys_get_temp_dir().'/symfony-cache');
}
}
- public function createCachePool()
+ public function createCachePool($defaultLifetime = 0, $testMethod = null)
{
+ if ('testGetMetadata' === $testMethod) {
+ return new PhpArrayAdapter(self::$file, new FilesystemAdapter());
+ }
+
return new PhpArrayAdapterWrapper(self::$file, new NullAdapter());
}
public function testStore()
{
- $arrayWithRefs = array();
+ $arrayWithRefs = [];
$arrayWithRefs[0] = 123;
$arrayWithRefs[1] = &$arrayWithRefs[0];
- $object = (object) array(
+ $object = (object) [
'foo' => 'bar',
'foo2' => 'bar2',
- );
+ ];
- $expected = array(
+ $expected = [
'null' => null,
'serializedString' => serialize($object),
'arrayWithRefs' => $arrayWithRefs,
'object' => $object,
- 'arrayWithObject' => array('bar' => $object),
- );
+ 'arrayWithObject' => ['bar' => $object],
+ ];
$adapter = $this->createCachePool();
$adapter->warmUp($expected);
@@ -101,16 +108,32 @@ public function testStore()
public function testStoredFile()
{
- $expected = array(
+ $data = [
'integer' => 42,
'float' => 42.42,
'boolean' => true,
- 'array_simple' => array('foo', 'bar'),
- 'array_associative' => array('foo' => 'bar', 'foo2' => 'bar2'),
- );
+ 'array_simple' => ['foo', 'bar'],
+ 'array_associative' => ['foo' => 'bar', 'foo2' => 'bar2'],
+ ];
+ $expected = [
+ [
+ 'integer' => 0,
+ 'float' => 1,
+ 'boolean' => 2,
+ 'array_simple' => 3,
+ 'array_associative' => 4,
+ ],
+ [
+ 0 => 42,
+ 1 => 42.42,
+ 2 => true,
+ 3 => ['foo', 'bar'],
+ 4 => ['foo' => 'bar', 'foo2' => 'bar2'],
+ ],
+ ];
$adapter = $this->createCachePool();
- $adapter->warmUp($expected);
+ $adapter->warmUp($data);
$values = eval(substr(file_get_contents(self::$file), 6));
@@ -120,13 +143,17 @@ public function testStoredFile()
class PhpArrayAdapterWrapper extends PhpArrayAdapter
{
+ protected $data = [];
+
public function save(CacheItemInterface $item)
{
- \call_user_func(\Closure::bind(function () use ($item) {
- $this->values[$item->getKey()] = $item->get();
- $this->warmUp($this->values);
- $this->values = eval(substr(file_get_contents($this->file), 6));
- }, $this, PhpArrayAdapter::class));
+ (\Closure::bind(function () use ($item) {
+ $key = $item->getKey();
+ $this->keys[$key] = $id = \count($this->values);
+ $this->data[$key] = $this->values[$id] = $item->get();
+ $this->warmUp($this->data);
+ list($this->keys, $this->values) = eval(substr(file_get_contents($this->file), 6));
+ }, $this, PhpArrayAdapter::class))();
return true;
}
diff --git a/Tests/Adapter/PhpArrayAdapterWithFallbackTest.php b/Tests/Adapter/PhpArrayAdapterWithFallbackTest.php
index 1a23198c..d8e20179 100644
--- a/Tests/Adapter/PhpArrayAdapterWithFallbackTest.php
+++ b/Tests/Adapter/PhpArrayAdapterWithFallbackTest.php
@@ -19,23 +19,23 @@
*/
class PhpArrayAdapterWithFallbackTest extends AdapterTestCase
{
- protected $skippedTests = array(
+ protected $skippedTests = [
'testGetItemInvalidKeys' => 'PhpArrayAdapter does not throw exceptions on invalid key.',
'testGetItemsInvalidKeys' => 'PhpArrayAdapter does not throw exceptions on invalid key.',
'testHasItemInvalidKeys' => 'PhpArrayAdapter does not throw exceptions on invalid key.',
'testDeleteItemInvalidKeys' => 'PhpArrayAdapter does not throw exceptions on invalid key.',
'testDeleteItemsInvalidKeys' => 'PhpArrayAdapter does not throw exceptions on invalid key.',
'testPrune' => 'PhpArrayAdapter just proxies',
- );
+ ];
protected static $file;
- public static function setupBeforeClass()
+ public static function setUpBeforeClass(): void
{
self::$file = sys_get_temp_dir().'/symfony-cache/php-array-adapter-test.php';
}
- protected function tearDown()
+ protected function tearDown(): void
{
if (file_exists(sys_get_temp_dir().'/symfony-cache')) {
FilesystemAdapterTest::rmdir(sys_get_temp_dir().'/symfony-cache');
diff --git a/Tests/Adapter/PhpFilesAdapterTest.php b/Tests/Adapter/PhpFilesAdapterTest.php
index 8e93c937..dec63a62 100644
--- a/Tests/Adapter/PhpFilesAdapterTest.php
+++ b/Tests/Adapter/PhpFilesAdapterTest.php
@@ -19,20 +19,16 @@
*/
class PhpFilesAdapterTest extends AdapterTestCase
{
- protected $skippedTests = array(
+ protected $skippedTests = [
'testDefaultLifeTime' => 'PhpFilesAdapter does not allow configuring a default lifetime.',
- );
+ ];
public function createCachePool()
{
- if (!PhpFilesAdapter::isSupported()) {
- $this->markTestSkipped('OPcache extension is not enabled.');
- }
-
return new PhpFilesAdapter('sf-cache');
}
- public static function tearDownAfterClass()
+ public static function tearDownAfterClass(): void
{
FilesystemAdapterTest::rmdir(sys_get_temp_dir().'/symfony-cache');
}
diff --git a/Tests/Adapter/PredisAdapterTest.php b/Tests/Adapter/PredisAdapterTest.php
index c005d64a..9ced661b 100644
--- a/Tests/Adapter/PredisAdapterTest.php
+++ b/Tests/Adapter/PredisAdapterTest.php
@@ -16,38 +16,32 @@
class PredisAdapterTest extends AbstractRedisAdapterTest
{
- public static function setupBeforeClass()
+ public static function setUpBeforeClass(): void
{
- parent::setupBeforeClass();
- self::$redis = new \Predis\Client(array('host' => getenv('REDIS_HOST')));
+ parent::setUpBeforeClass();
+ self::$redis = new \Predis\Client(['host' => getenv('REDIS_HOST')]);
}
public function testCreateConnection()
{
$redisHost = getenv('REDIS_HOST');
- $redis = RedisAdapter::createConnection('redis://'.$redisHost.'/1', array('class' => \Predis\Client::class, 'timeout' => 3));
+ $redis = RedisAdapter::createConnection('redis://'.$redisHost.'/1', ['class' => \Predis\Client::class, 'timeout' => 3]);
$this->assertInstanceOf(\Predis\Client::class, $redis);
$connection = $redis->getConnection();
$this->assertInstanceOf(StreamConnection::class, $connection);
- $params = array(
+ $params = [
'scheme' => 'tcp',
'host' => $redisHost,
- 'path' => '',
- 'dbindex' => '1',
'port' => 6379,
- 'class' => 'Predis\Client',
- 'timeout' => 3,
'persistent' => 0,
- 'persistent_id' => null,
- 'read_timeout' => 0,
- 'retry_interval' => 0,
- 'lazy' => false,
+ 'timeout' => 3,
+ 'read_write_timeout' => 0,
+ 'tcp_nodelay' => true,
'database' => '1',
- 'password' => null,
- );
+ ];
$this->assertSame($params, $connection->getParameters()->toArray());
}
}
diff --git a/Tests/Adapter/PredisClusterAdapterTest.php b/Tests/Adapter/PredisClusterAdapterTest.php
index 38915397..63fb7ecb 100644
--- a/Tests/Adapter/PredisClusterAdapterTest.php
+++ b/Tests/Adapter/PredisClusterAdapterTest.php
@@ -13,13 +13,13 @@
class PredisClusterAdapterTest extends AbstractRedisAdapterTest
{
- public static function setupBeforeClass()
+ public static function setUpBeforeClass(): void
{
- parent::setupBeforeClass();
- self::$redis = new \Predis\Client(array(array('host' => getenv('REDIS_HOST'))));
+ parent::setUpBeforeClass();
+ self::$redis = new \Predis\Client([['host' => getenv('REDIS_HOST')]]);
}
- public static function tearDownAfterClass()
+ public static function tearDownAfterClass(): void
{
self::$redis = null;
}
diff --git a/Tests/Adapter/PredisRedisClusterAdapterTest.php b/Tests/Adapter/PredisRedisClusterAdapterTest.php
new file mode 100644
index 00000000..52a515d4
--- /dev/null
+++ b/Tests/Adapter/PredisRedisClusterAdapterTest.php
@@ -0,0 +1,31 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\Tests\Adapter;
+
+use Symfony\Component\Cache\Adapter\RedisAdapter;
+
+class PredisRedisClusterAdapterTest extends AbstractRedisAdapterTest
+{
+ public static function setUpBeforeClass(): void
+ {
+ if (!$hosts = getenv('REDIS_CLUSTER_HOSTS')) {
+ self::markTestSkipped('REDIS_CLUSTER_HOSTS env var is not defined.');
+ }
+
+ self::$redis = RedisAdapter::createConnection('redis:?host['.str_replace(' ', ']&host[', $hosts).']', ['class' => \Predis\Client::class, 'redis_cluster' => true]);
+ }
+
+ public static function tearDownAfterClass(): void
+ {
+ self::$redis = null;
+ }
+}
diff --git a/Tests/Adapter/PredisTagAwareAdapterTest.php b/Tests/Adapter/PredisTagAwareAdapterTest.php
new file mode 100644
index 00000000..e685d760
--- /dev/null
+++ b/Tests/Adapter/PredisTagAwareAdapterTest.php
@@ -0,0 +1,34 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\Tests\Adapter;
+
+use Symfony\Component\Cache\Adapter\RedisTagAwareAdapter;
+use Symfony\Component\Cache\Tests\Traits\TagAwareTestTrait;
+
+class PredisTagAwareAdapterTest extends PredisAdapterTest
+{
+ use TagAwareTestTrait;
+
+ protected function setUp(): void
+ {
+ parent::setUp();
+ $this->skippedTests['testTagItemExpiry'] = 'Testing expiration slows down the test suite';
+ }
+
+ public function createCachePool($defaultLifetime = 0)
+ {
+ $this->assertInstanceOf(\Predis\Client::class, self::$redis);
+ $adapter = new RedisTagAwareAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);
+
+ return $adapter;
+ }
+}
diff --git a/Tests/Adapter/PredisTagAwareClusterAdapterTest.php b/Tests/Adapter/PredisTagAwareClusterAdapterTest.php
new file mode 100644
index 00000000..8c604c1c
--- /dev/null
+++ b/Tests/Adapter/PredisTagAwareClusterAdapterTest.php
@@ -0,0 +1,34 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\Tests\Adapter;
+
+use Symfony\Component\Cache\Adapter\RedisTagAwareAdapter;
+use Symfony\Component\Cache\Tests\Traits\TagAwareTestTrait;
+
+class PredisTagAwareClusterAdapterTest extends PredisClusterAdapterTest
+{
+ use TagAwareTestTrait;
+
+ protected function setUp(): void
+ {
+ parent::setUp();
+ $this->skippedTests['testTagItemExpiry'] = 'Testing expiration slows down the test suite';
+ }
+
+ public function createCachePool($defaultLifetime = 0)
+ {
+ $this->assertInstanceOf(\Predis\Client::class, self::$redis);
+ $adapter = new RedisTagAwareAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);
+
+ return $adapter;
+ }
+}
diff --git a/Tests/Adapter/PredisTagAwareRedisClusterAdapterTest.php b/Tests/Adapter/PredisTagAwareRedisClusterAdapterTest.php
new file mode 100644
index 00000000..e8d2ea65
--- /dev/null
+++ b/Tests/Adapter/PredisTagAwareRedisClusterAdapterTest.php
@@ -0,0 +1,34 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\Tests\Adapter;
+
+use Symfony\Component\Cache\Adapter\RedisTagAwareAdapter;
+use Symfony\Component\Cache\Tests\Traits\TagAwareTestTrait;
+
+class PredisTagAwareRedisClusterAdapterTest extends PredisRedisClusterAdapterTest
+{
+ use TagAwareTestTrait;
+
+ protected function setUp(): void
+ {
+ parent::setUp();
+ $this->skippedTests['testTagItemExpiry'] = 'Testing expiration slows down the test suite';
+ }
+
+ public function createCachePool($defaultLifetime = 0)
+ {
+ $this->assertInstanceOf(\Predis\Client::class, self::$redis);
+ $adapter = new RedisTagAwareAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);
+
+ return $adapter;
+ }
+}
diff --git a/Tests/Adapter/ProxyAdapterTest.php b/Tests/Adapter/ProxyAdapterTest.php
index ff4b9d34..24f92ca9 100644
--- a/Tests/Adapter/ProxyAdapterTest.php
+++ b/Tests/Adapter/ProxyAdapterTest.php
@@ -13,6 +13,7 @@
use Psr\Cache\CacheItemInterface;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
+use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\ProxyAdapter;
use Symfony\Component\Cache\CacheItem;
@@ -21,23 +22,25 @@
*/
class ProxyAdapterTest extends AdapterTestCase
{
- protected $skippedTests = array(
+ protected $skippedTests = [
'testDeferredSaveWithoutCommit' => 'Assumes a shared cache which ArrayAdapter is not.',
'testSaveWithoutExpire' => 'Assumes a shared cache which ArrayAdapter is not.',
'testPrune' => 'ProxyAdapter just proxies',
- );
+ ];
- public function createCachePool($defaultLifetime = 0)
+ public function createCachePool($defaultLifetime = 0, $testMethod = null)
{
+ if ('testGetMetadata' === $testMethod) {
+ return new ProxyAdapter(new FilesystemAdapter(), '', $defaultLifetime);
+ }
+
return new ProxyAdapter(new ArrayAdapter(), '', $defaultLifetime);
}
- /**
- * @expectedException \Exception
- * @expectedExceptionMessage OK bar
- */
public function testProxyfiedItem()
{
+ $this->expectException('Exception');
+ $this->expectExceptionMessage('OK bar');
$item = new CacheItem();
$pool = new ProxyAdapter(new TestingArrayAdapter($item));
diff --git a/Tests/Adapter/Psr16AdapterTest.php b/Tests/Adapter/Psr16AdapterTest.php
new file mode 100644
index 00000000..09c55e60
--- /dev/null
+++ b/Tests/Adapter/Psr16AdapterTest.php
@@ -0,0 +1,42 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\Tests\Adapter;
+
+use Symfony\Component\Cache\Adapter\ArrayAdapter;
+use Symfony\Component\Cache\Adapter\FilesystemAdapter;
+use Symfony\Component\Cache\Adapter\Psr16Adapter;
+use Symfony\Component\Cache\Psr16Cache;
+
+/**
+ * @group time-sensitive
+ */
+class Psr16AdapterTest extends AdapterTestCase
+{
+ protected $skippedTests = [
+ 'testPrune' => 'Psr16adapter just proxies',
+ ];
+
+ public function createCachePool($defaultLifetime = 0)
+ {
+ return new Psr16Adapter(new Psr16Cache(new FilesystemAdapter()), '', $defaultLifetime);
+ }
+
+ public function testValidCacheKeyWithNamespace()
+ {
+ $cache = new Psr16Adapter(new Psr16Cache(new ArrayAdapter()), 'some_namespace', 0);
+ $item = $cache->getItem('my_key');
+ $item->set('someValue');
+ $cache->save($item);
+
+ $this->assertTrue($cache->getItem('my_key')->isHit(), 'Stored item is successfully retrieved.');
+ }
+}
diff --git a/Tests/Adapter/RedisAdapterTest.php b/Tests/Adapter/RedisAdapterTest.php
index 28c310fb..b039289c 100644
--- a/Tests/Adapter/RedisAdapterTest.php
+++ b/Tests/Adapter/RedisAdapterTest.php
@@ -17,10 +17,10 @@
class RedisAdapterTest extends AbstractRedisAdapterTest
{
- public static function setupBeforeClass()
+ public static function setUpBeforeClass(): void
{
- parent::setupBeforeClass();
- self::$redis = AbstractAdapter::createConnection('redis://'.getenv('REDIS_HOST'), array('lazy' => true));
+ parent::setUpBeforeClass();
+ self::$redis = AbstractAdapter::createConnection('redis://'.getenv('REDIS_HOST'), ['lazy' => true]);
}
public function createCachePool($defaultLifetime = 0)
@@ -31,62 +31,78 @@ public function createCachePool($defaultLifetime = 0)
return $adapter;
}
- public function testCreateConnection()
+ /**
+ * @dataProvider provideValidSchemes
+ */
+ public function testCreateConnection($dsnScheme)
{
+ $redis = RedisAdapter::createConnection($dsnScheme.':?host[h1]&host[h2]&host[/foo:]');
+ $this->assertInstanceOf(\RedisArray::class, $redis);
+ $this->assertSame(['h1:6379', 'h2:6379', '/foo'], $redis->_hosts());
+ @$redis = null; // some versions of phpredis connect on destruct, let's silence the warning
+
$redisHost = getenv('REDIS_HOST');
- $redis = RedisAdapter::createConnection('redis://'.$redisHost);
+ $redis = RedisAdapter::createConnection($dsnScheme.'://'.$redisHost);
$this->assertInstanceOf(\Redis::class, $redis);
$this->assertTrue($redis->isConnected());
$this->assertSame(0, $redis->getDbNum());
- $redis = RedisAdapter::createConnection('redis://'.$redisHost.'/2');
+ $redis = RedisAdapter::createConnection($dsnScheme.'://'.$redisHost.'/2');
$this->assertSame(2, $redis->getDbNum());
- $redis = RedisAdapter::createConnection('redis://'.$redisHost, array('timeout' => 3));
+ $redis = RedisAdapter::createConnection($dsnScheme.'://'.$redisHost, ['timeout' => 3]);
$this->assertEquals(3, $redis->getTimeout());
- $redis = RedisAdapter::createConnection('redis://'.$redisHost.'?timeout=4');
+ $redis = RedisAdapter::createConnection($dsnScheme.'://'.$redisHost.'?timeout=4');
$this->assertEquals(4, $redis->getTimeout());
- $redis = RedisAdapter::createConnection('redis://'.$redisHost, array('read_timeout' => 5));
+ $redis = RedisAdapter::createConnection($dsnScheme.'://'.$redisHost, ['read_timeout' => 5]);
$this->assertEquals(5, $redis->getReadTimeout());
}
/**
* @dataProvider provideFailedCreateConnection
- * @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
- * @expectedExceptionMessage Redis connection failed
*/
public function testFailedCreateConnection($dsn)
{
+ $this->expectException('Symfony\Component\Cache\Exception\InvalidArgumentException');
+ $this->expectExceptionMessage('Redis connection failed');
RedisAdapter::createConnection($dsn);
}
public function provideFailedCreateConnection()
{
- return array(
- array('redis://localhost:1234'),
- array('redis://foo@localhost'),
- array('redis://localhost/123'),
- );
+ return [
+ ['redis://localhost:1234'],
+ ['redis://foo@localhost'],
+ ['redis://localhost/123'],
+ ];
}
/**
* @dataProvider provideInvalidCreateConnection
- * @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
- * @expectedExceptionMessage Invalid Redis DSN
*/
public function testInvalidCreateConnection($dsn)
{
+ $this->expectException('Symfony\Component\Cache\Exception\InvalidArgumentException');
+ $this->expectExceptionMessage('Invalid Redis DSN');
RedisAdapter::createConnection($dsn);
}
+ public function provideValidSchemes()
+ {
+ return [
+ ['redis'],
+ ['rediss'],
+ ];
+ }
+
public function provideInvalidCreateConnection()
{
- return array(
- array('foo://localhost'),
- array('redis://'),
- );
+ return [
+ ['foo://localhost'],
+ ['redis://'],
+ ];
}
}
diff --git a/Tests/Adapter/RedisArrayAdapterTest.php b/Tests/Adapter/RedisArrayAdapterTest.php
index bef3eb88..63ade368 100644
--- a/Tests/Adapter/RedisArrayAdapterTest.php
+++ b/Tests/Adapter/RedisArrayAdapterTest.php
@@ -13,12 +13,12 @@
class RedisArrayAdapterTest extends AbstractRedisAdapterTest
{
- public static function setupBeforeClass()
+ public static function setUpBeforeClass(): void
{
parent::setupBeforeClass();
if (!class_exists('RedisArray')) {
self::markTestSkipped('The RedisArray class is required.');
}
- self::$redis = new \RedisArray(array(getenv('REDIS_HOST')), array('lazy_connect' => true));
+ self::$redis = new \RedisArray([getenv('REDIS_HOST')], ['lazy_connect' => true]);
}
}
diff --git a/Tests/Adapter/RedisClusterAdapterTest.php b/Tests/Adapter/RedisClusterAdapterTest.php
index 852079c0..34dfae19 100644
--- a/Tests/Adapter/RedisClusterAdapterTest.php
+++ b/Tests/Adapter/RedisClusterAdapterTest.php
@@ -11,9 +11,13 @@
namespace Symfony\Component\Cache\Tests\Adapter;
+use Symfony\Component\Cache\Adapter\AbstractAdapter;
+use Symfony\Component\Cache\Adapter\RedisAdapter;
+use Symfony\Component\Cache\Traits\RedisClusterProxy;
+
class RedisClusterAdapterTest extends AbstractRedisAdapterTest
{
- public static function setupBeforeClass()
+ public static function setUpBeforeClass(): void
{
if (!class_exists('RedisCluster')) {
self::markTestSkipped('The RedisCluster class is required.');
@@ -22,6 +26,33 @@ public static function setupBeforeClass()
self::markTestSkipped('REDIS_CLUSTER_HOSTS env var is not defined.');
}
- self::$redis = new \RedisCluster(null, explode(' ', $hosts));
+ self::$redis = AbstractAdapter::createConnection('redis:?host['.str_replace(' ', ']&host[', $hosts).']', ['lazy' => true, 'redis_cluster' => true]);
+ }
+
+ public function createCachePool($defaultLifetime = 0)
+ {
+ $this->assertInstanceOf(RedisClusterProxy::class, self::$redis);
+ $adapter = new RedisAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);
+
+ return $adapter;
+ }
+
+ /**
+ * @dataProvider provideFailedCreateConnection
+ */
+ public function testFailedCreateConnection($dsn)
+ {
+ $this->expectException('Symfony\Component\Cache\Exception\InvalidArgumentException');
+ $this->expectExceptionMessage('Redis connection failed');
+ RedisAdapter::createConnection($dsn);
+ }
+
+ public function provideFailedCreateConnection()
+ {
+ return [
+ ['redis://localhost:1234?redis_cluster=1'],
+ ['redis://foo@localhost?redis_cluster=1'],
+ ['redis://localhost/123?redis_cluster=1'],
+ ];
}
}
diff --git a/Tests/Adapter/RedisTagAwareAdapterTest.php b/Tests/Adapter/RedisTagAwareAdapterTest.php
new file mode 100644
index 00000000..ef140818
--- /dev/null
+++ b/Tests/Adapter/RedisTagAwareAdapterTest.php
@@ -0,0 +1,35 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\Tests\Adapter;
+
+use Symfony\Component\Cache\Adapter\RedisTagAwareAdapter;
+use Symfony\Component\Cache\Tests\Traits\TagAwareTestTrait;
+use Symfony\Component\Cache\Traits\RedisProxy;
+
+class RedisTagAwareAdapterTest extends RedisAdapterTest
+{
+ use TagAwareTestTrait;
+
+ protected function setUp(): void
+ {
+ parent::setUp();
+ $this->skippedTests['testTagItemExpiry'] = 'Testing expiration slows down the test suite';
+ }
+
+ public function createCachePool($defaultLifetime = 0)
+ {
+ $this->assertInstanceOf(RedisProxy::class, self::$redis);
+ $adapter = new RedisTagAwareAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);
+
+ return $adapter;
+ }
+}
diff --git a/Tests/Adapter/RedisTagAwareArrayAdapterTest.php b/Tests/Adapter/RedisTagAwareArrayAdapterTest.php
new file mode 100644
index 00000000..7c980204
--- /dev/null
+++ b/Tests/Adapter/RedisTagAwareArrayAdapterTest.php
@@ -0,0 +1,34 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\Tests\Adapter;
+
+use Symfony\Component\Cache\Adapter\RedisTagAwareAdapter;
+use Symfony\Component\Cache\Tests\Traits\TagAwareTestTrait;
+
+class RedisTagAwareArrayAdapterTest extends RedisArrayAdapterTest
+{
+ use TagAwareTestTrait;
+
+ protected function setUp(): void
+ {
+ parent::setUp();
+ $this->skippedTests['testTagItemExpiry'] = 'Testing expiration slows down the test suite';
+ }
+
+ public function createCachePool($defaultLifetime = 0)
+ {
+ $this->assertInstanceOf(\RedisArray::class, self::$redis);
+ $adapter = new RedisTagAwareAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);
+
+ return $adapter;
+ }
+}
diff --git a/Tests/Adapter/RedisTagAwareClusterAdapterTest.php b/Tests/Adapter/RedisTagAwareClusterAdapterTest.php
new file mode 100644
index 00000000..7b7d6801
--- /dev/null
+++ b/Tests/Adapter/RedisTagAwareClusterAdapterTest.php
@@ -0,0 +1,35 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\Tests\Adapter;
+
+use Symfony\Component\Cache\Adapter\RedisTagAwareAdapter;
+use Symfony\Component\Cache\Tests\Traits\TagAwareTestTrait;
+use Symfony\Component\Cache\Traits\RedisClusterProxy;
+
+class RedisTagAwareClusterAdapterTest extends RedisClusterAdapterTest
+{
+ use TagAwareTestTrait;
+
+ protected function setUp(): void
+ {
+ parent::setUp();
+ $this->skippedTests['testTagItemExpiry'] = 'Testing expiration slows down the test suite';
+ }
+
+ public function createCachePool($defaultLifetime = 0)
+ {
+ $this->assertInstanceOf(RedisClusterProxy::class, self::$redis);
+ $adapter = new RedisTagAwareAdapter(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);
+
+ return $adapter;
+ }
+}
diff --git a/Tests/Adapter/SimpleCacheAdapterTest.php b/Tests/Adapter/SimpleCacheAdapterTest.php
index 460f3b09..8097e49c 100644
--- a/Tests/Adapter/SimpleCacheAdapterTest.php
+++ b/Tests/Adapter/SimpleCacheAdapterTest.php
@@ -12,19 +12,31 @@
namespace Symfony\Component\Cache\Tests\Adapter;
use Symfony\Component\Cache\Adapter\SimpleCacheAdapter;
+use Symfony\Component\Cache\Simple\ArrayCache;
use Symfony\Component\Cache\Simple\FilesystemCache;
/**
* @group time-sensitive
+ * @group legacy
*/
class SimpleCacheAdapterTest extends AdapterTestCase
{
- protected $skippedTests = array(
+ protected $skippedTests = [
'testPrune' => 'SimpleCache just proxies',
- );
+ ];
public function createCachePool($defaultLifetime = 0)
{
return new SimpleCacheAdapter(new FilesystemCache(), '', $defaultLifetime);
}
+
+ public function testValidCacheKeyWithNamespace()
+ {
+ $cache = new SimpleCacheAdapter(new ArrayCache(), 'some_namespace', 0);
+ $item = $cache->getItem('my_key');
+ $item->set('someValue');
+ $cache->save($item);
+
+ $this->assertTrue($cache->getItem('my_key')->isHit(), 'Stored item is successfully retrieved.');
+ }
}
diff --git a/Tests/Adapter/TagAwareAdapterTest.php b/Tests/Adapter/TagAwareAdapterTest.php
index 7074299e..b4e1ebbc 100644
--- a/Tests/Adapter/TagAwareAdapterTest.php
+++ b/Tests/Adapter/TagAwareAdapterTest.php
@@ -11,158 +11,174 @@
namespace Symfony\Component\Cache\Tests\Adapter;
+use PHPUnit\Framework\MockObject\MockObject;
+use Psr\Cache\CacheItemInterface;
use Symfony\Component\Cache\Adapter\AdapterInterface;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Adapter\TagAwareAdapter;
+use Symfony\Component\Cache\Tests\Traits\TagAwareTestTrait;
/**
* @group time-sensitive
*/
class TagAwareAdapterTest extends AdapterTestCase
{
+ use TagAwareTestTrait;
+
public function createCachePool($defaultLifetime = 0)
{
return new TagAwareAdapter(new FilesystemAdapter('', $defaultLifetime));
}
- public static function tearDownAfterClass()
+ public static function tearDownAfterClass(): void
{
FilesystemAdapterTest::rmdir(sys_get_temp_dir().'/symfony-cache');
}
/**
- * @expectedException \Psr\Cache\InvalidArgumentException
+ * Test feature specific to TagAwareAdapter as it implicit needs to save deferred when also saving expiry info.
*/
- public function testInvalidTag()
+ public function testInvalidateCommitsSeperatePools()
{
- $pool = $this->createCachePool();
- $item = $pool->getItem('foo');
- $item->tag(':');
+ $pool1 = $this->createCachePool();
+
+ $foo = $pool1->getItem('foo');
+ $foo->tag('tag');
+
+ $pool1->saveDeferred($foo->set('foo'));
+ $pool1->invalidateTags(['tag']);
+
+ $pool2 = $this->createCachePool();
+ $foo = $pool2->getItem('foo');
+
+ $this->assertTrue($foo->isHit());
}
- public function testInvalidateTags()
+ public function testPrune()
{
- $pool = $this->createCachePool();
+ $cache = new TagAwareAdapter($this->getPruneableMock());
+ $this->assertTrue($cache->prune());
- $i0 = $pool->getItem('i0');
- $i1 = $pool->getItem('i1');
- $i2 = $pool->getItem('i2');
- $i3 = $pool->getItem('i3');
- $foo = $pool->getItem('foo');
+ $cache = new TagAwareAdapter($this->getNonPruneableMock());
+ $this->assertFalse($cache->prune());
- $pool->save($i0->tag('bar'));
- $pool->save($i1->tag('foo'));
- $pool->save($i2->tag('foo')->tag('bar'));
- $pool->save($i3->tag('foo')->tag('baz'));
- $pool->save($foo);
+ $cache = new TagAwareAdapter($this->getFailingPruneableMock());
+ $this->assertFalse($cache->prune());
+ }
- $pool->invalidateTags(array('bar'));
+ public function testKnownTagVersionsTtl()
+ {
+ $itemsPool = new FilesystemAdapter('', 10);
+ $tagsPool = $this
+ ->getMockBuilder(AdapterInterface::class)
+ ->getMock();
- $this->assertFalse($pool->getItem('i0')->isHit());
- $this->assertTrue($pool->getItem('i1')->isHit());
- $this->assertFalse($pool->getItem('i2')->isHit());
- $this->assertTrue($pool->getItem('i3')->isHit());
+ $pool = new TagAwareAdapter($itemsPool, $tagsPool, 10);
+
+ $item = $pool->getItem('foo');
+ $item->tag(['baz']);
+ $item->expiresAfter(100);
+
+ $tag = $this->getMockBuilder(CacheItemInterface::class)->getMock();
+ $tag->expects(self::exactly(2))->method('get')->willReturn(10);
+
+ $tagsPool->expects(self::exactly(2))->method('getItems')->willReturn([
+ 'baz'.TagAwareAdapter::TAGS_PREFIX => $tag,
+ ]);
+
+ $pool->save($item);
+ $this->assertTrue($pool->getItem('foo')->isHit());
$this->assertTrue($pool->getItem('foo')->isHit());
- $pool->invalidateTags(array('foo'));
+ sleep(20);
- $this->assertFalse($pool->getItem('i1')->isHit());
- $this->assertFalse($pool->getItem('i3')->isHit());
$this->assertTrue($pool->getItem('foo')->isHit());
- $anotherPoolInstance = $this->createCachePool();
+ sleep(5);
- $this->assertFalse($anotherPoolInstance->getItem('i1')->isHit());
- $this->assertFalse($anotherPoolInstance->getItem('i3')->isHit());
- $this->assertTrue($anotherPoolInstance->getItem('foo')->isHit());
+ $this->assertTrue($pool->getItem('foo')->isHit());
}
- public function testInvalidateCommits()
+ public function testTagEntryIsCreatedForItemWithoutTags()
{
- $pool1 = $this->createCachePool();
-
- $foo = $pool1->getItem('foo');
- $foo->tag('tag');
-
- $pool1->saveDeferred($foo->set('foo'));
- $pool1->invalidateTags(array('tag'));
+ $pool = $this->createCachePool();
- $pool2 = $this->createCachePool();
- $foo = $pool2->getItem('foo');
+ $itemKey = 'foo';
+ $item = $pool->getItem($itemKey);
+ $pool->save($item);
- $this->assertTrue($foo->isHit());
+ $adapter = new FilesystemAdapter();
+ $this->assertTrue($adapter->hasItem(TagAwareAdapter::TAGS_PREFIX.$itemKey));
}
- public function testTagsAreCleanedOnSave()
+ public function testHasItemReturnsFalseWhenPoolDoesNotHaveItemTags()
{
$pool = $this->createCachePool();
- $i = $pool->getItem('k');
- $pool->save($i->tag('foo'));
+ $itemKey = 'foo';
+ $item = $pool->getItem($itemKey);
+ $pool->save($item);
+
+ $anotherPool = $this->createCachePool();
- $i = $pool->getItem('k');
- $pool->save($i->tag('bar'));
+ $adapter = new FilesystemAdapter();
+ $adapter->deleteItem(TagAwareAdapter::TAGS_PREFIX.$itemKey); //simulate item losing tags pair
- $pool->invalidateTags(array('foo'));
- $this->assertTrue($pool->getItem('k')->isHit());
+ $this->assertFalse($anotherPool->hasItem($itemKey));
}
- public function testTagsAreCleanedOnDelete()
+ public function testGetItemReturnsCacheMissWhenPoolDoesNotHaveItemTags()
{
$pool = $this->createCachePool();
- $i = $pool->getItem('k');
- $pool->save($i->tag('foo'));
- $pool->deleteItem('k');
+ $itemKey = 'foo';
+ $item = $pool->getItem($itemKey);
+ $pool->save($item);
+
+ $anotherPool = $this->createCachePool();
- $pool->save($pool->getItem('k'));
- $pool->invalidateTags(array('foo'));
+ $adapter = new FilesystemAdapter();
+ $adapter->deleteItem(TagAwareAdapter::TAGS_PREFIX.$itemKey); //simulate item losing tags pair
- $this->assertTrue($pool->getItem('k')->isHit());
+ $item = $anotherPool->getItem($itemKey);
+ $this->assertFalse($item->isHit());
}
- public function testTagItemExpiry()
+ public function testHasItemReturnsFalseWhenPoolDoesNotHaveItemAndOnlyHasTags()
{
- $pool = $this->createCachePool(10);
-
- $item = $pool->getItem('foo');
- $item->tag(array('baz'));
- $item->expiresAfter(100);
+ $pool = $this->createCachePool();
+ $itemKey = 'foo';
+ $item = $pool->getItem($itemKey);
$pool->save($item);
- $pool->invalidateTags(array('baz'));
- $this->assertFalse($pool->getItem('foo')->isHit());
- sleep(20);
+ $anotherPool = $this->createCachePool();
- $this->assertFalse($pool->getItem('foo')->isHit());
+ $adapter = new FilesystemAdapter();
+ $adapter->deleteItem($itemKey); //simulate losing item but keeping tags
+
+ $this->assertFalse($anotherPool->hasItem($itemKey));
}
- public function testGetPreviousTags()
+ public function testGetItemReturnsCacheMissWhenPoolDoesNotHaveItemAndOnlyHasTags()
{
$pool = $this->createCachePool();
- $i = $pool->getItem('k');
- $pool->save($i->tag('foo'));
+ $itemKey = 'foo';
+ $item = $pool->getItem($itemKey);
+ $pool->save($item);
- $i = $pool->getItem('k');
- $this->assertSame(array('foo' => 'foo'), $i->getPreviousTags());
- }
+ $anotherPool = $this->createCachePool();
- public function testPrune()
- {
- $cache = new TagAwareAdapter($this->getPruneableMock());
- $this->assertTrue($cache->prune());
-
- $cache = new TagAwareAdapter($this->getNonPruneableMock());
- $this->assertFalse($cache->prune());
+ $adapter = new FilesystemAdapter();
+ $adapter->deleteItem($itemKey); //simulate losing item but keeping tags
- $cache = new TagAwareAdapter($this->getFailingPruneableMock());
- $this->assertFalse($cache->prune());
+ $item = $anotherPool->getItem($itemKey);
+ $this->assertFalse($item->isHit());
}
/**
- * @return \PHPUnit_Framework_MockObject_MockObject|PruneableCacheInterface
+ * @return MockObject|PruneableCacheInterface
*/
private function getPruneableMock()
{
@@ -173,13 +189,13 @@ private function getPruneableMock()
$pruneable
->expects($this->atLeastOnce())
->method('prune')
- ->will($this->returnValue(true));
+ ->willReturn(true);
return $pruneable;
}
/**
- * @return \PHPUnit_Framework_MockObject_MockObject|PruneableCacheInterface
+ * @return MockObject|PruneableCacheInterface
*/
private function getFailingPruneableMock()
{
@@ -190,13 +206,13 @@ private function getFailingPruneableMock()
$pruneable
->expects($this->atLeastOnce())
->method('prune')
- ->will($this->returnValue(false));
+ ->willReturn(false);
return $pruneable;
}
/**
- * @return \PHPUnit_Framework_MockObject_MockObject|AdapterInterface
+ * @return MockObject|AdapterInterface
*/
private function getNonPruneableMock()
{
diff --git a/Tests/Adapter/TagAwareAndProxyAdapterIntegrationTest.php b/Tests/Adapter/TagAwareAndProxyAdapterIntegrationTest.php
new file mode 100644
index 00000000..b11c1f28
--- /dev/null
+++ b/Tests/Adapter/TagAwareAndProxyAdapterIntegrationTest.php
@@ -0,0 +1,38 @@
+getItem('foo');
+ $item->tag(['tag1', 'tag2']);
+ $item->set('bar');
+ $cache->save($item);
+
+ $this->assertSame('bar', $cache->getItem('foo')->get());
+ }
+
+ public function dataProvider()
+ {
+ return [
+ [new ArrayAdapter()],
+ // also testing with a non-AdapterInterface implementation
+ // because the ProxyAdapter behaves slightly different for those
+ [new ExternalAdapter()],
+ ];
+ }
+}
diff --git a/Tests/Adapter/TraceableAdapterTest.php b/Tests/Adapter/TraceableAdapterTest.php
index 3755e88d..35eba7d7 100644
--- a/Tests/Adapter/TraceableAdapterTest.php
+++ b/Tests/Adapter/TraceableAdapterTest.php
@@ -19,9 +19,9 @@
*/
class TraceableAdapterTest extends AdapterTestCase
{
- protected $skippedTests = array(
+ protected $skippedTests = [
'testPrune' => 'TraceableAdapter just proxies',
- );
+ ];
public function createCachePool($defaultLifetime = 0)
{
@@ -37,7 +37,7 @@ public function testGetItemMissTrace()
$call = $calls[0];
$this->assertSame('getItem', $call->name);
- $this->assertSame(array('k' => false), $call->result);
+ $this->assertSame(['k' => false], $call->result);
$this->assertSame(0, $call->hits);
$this->assertSame(1, $call->misses);
$this->assertNotEmpty($call->start);
@@ -61,7 +61,7 @@ public function testGetItemHitTrace()
public function testGetItemsMissTrace()
{
$pool = $this->createCachePool();
- $arg = array('k0', 'k1');
+ $arg = ['k0', 'k1'];
$items = $pool->getItems($arg);
foreach ($items as $item) {
}
@@ -70,7 +70,7 @@ public function testGetItemsMissTrace()
$call = $calls[0];
$this->assertSame('getItems', $call->name);
- $this->assertSame(array('k0' => false, 'k1' => false), $call->result);
+ $this->assertSame(['k0' => false, 'k1' => false], $call->result);
$this->assertSame(2, $call->misses);
$this->assertNotEmpty($call->start);
$this->assertNotEmpty($call->end);
@@ -85,7 +85,7 @@ public function testHasItemMissTrace()
$call = $calls[0];
$this->assertSame('hasItem', $call->name);
- $this->assertSame(array('k' => false), $call->result);
+ $this->assertSame(['k' => false], $call->result);
$this->assertNotEmpty($call->start);
$this->assertNotEmpty($call->end);
}
@@ -101,7 +101,7 @@ public function testHasItemHitTrace()
$call = $calls[2];
$this->assertSame('hasItem', $call->name);
- $this->assertSame(array('k' => true), $call->result);
+ $this->assertSame(['k' => true], $call->result);
$this->assertNotEmpty($call->start);
$this->assertNotEmpty($call->end);
}
@@ -115,7 +115,7 @@ public function testDeleteItemTrace()
$call = $calls[0];
$this->assertSame('deleteItem', $call->name);
- $this->assertSame(array('k' => true), $call->result);
+ $this->assertSame(['k' => true], $call->result);
$this->assertSame(0, $call->hits);
$this->assertSame(0, $call->misses);
$this->assertNotEmpty($call->start);
@@ -125,14 +125,14 @@ public function testDeleteItemTrace()
public function testDeleteItemsTrace()
{
$pool = $this->createCachePool();
- $arg = array('k0', 'k1');
+ $arg = ['k0', 'k1'];
$pool->deleteItems($arg);
$calls = $pool->getCalls();
$this->assertCount(1, $calls);
$call = $calls[0];
$this->assertSame('deleteItems', $call->name);
- $this->assertSame(array('keys' => $arg, 'result' => true), $call->result);
+ $this->assertSame(['keys' => $arg, 'result' => true], $call->result);
$this->assertSame(0, $call->hits);
$this->assertSame(0, $call->misses);
$this->assertNotEmpty($call->start);
@@ -149,7 +149,7 @@ public function testSaveTrace()
$call = $calls[1];
$this->assertSame('save', $call->name);
- $this->assertSame(array('k' => true), $call->result);
+ $this->assertSame(['k' => true], $call->result);
$this->assertSame(0, $call->hits);
$this->assertSame(0, $call->misses);
$this->assertNotEmpty($call->start);
@@ -166,7 +166,7 @@ public function testSaveDeferredTrace()
$call = $calls[1];
$this->assertSame('saveDeferred', $call->name);
- $this->assertSame(array('k' => true), $call->result);
+ $this->assertSame(['k' => true], $call->result);
$this->assertSame(0, $call->hits);
$this->assertSame(0, $call->misses);
$this->assertNotEmpty($call->start);
diff --git a/Tests/Adapter/TraceableTagAwareAdapterTest.php b/Tests/Adapter/TraceableTagAwareAdapterTest.php
index 9b50bfab..5cd4185c 100644
--- a/Tests/Adapter/TraceableTagAwareAdapterTest.php
+++ b/Tests/Adapter/TraceableTagAwareAdapterTest.php
@@ -23,7 +23,7 @@ class TraceableTagAwareAdapterTest extends TraceableAdapterTest
public function testInvalidateTags()
{
$pool = new TraceableTagAwareAdapter(new TagAwareAdapter(new FilesystemAdapter()));
- $pool->invalidateTags(array('foo'));
+ $pool->invalidateTags(['foo']);
$calls = $pool->getCalls();
$this->assertCount(1, $calls);
diff --git a/Tests/CacheItemTest.php b/Tests/CacheItemTest.php
index 4aae16b6..b36b6343 100644
--- a/Tests/CacheItemTest.php
+++ b/Tests/CacheItemTest.php
@@ -23,55 +23,74 @@ public function testValidKey()
/**
* @dataProvider provideInvalidKey
- * @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
- * @expectedExceptionMessage Cache key
*/
public function testInvalidKey($key)
{
+ $this->expectException('Symfony\Component\Cache\Exception\InvalidArgumentException');
+ $this->expectExceptionMessage('Cache key');
CacheItem::validateKey($key);
}
public function provideInvalidKey()
{
- return array(
- array(''),
- array('{'),
- array('}'),
- array('('),
- array(')'),
- array('/'),
- array('\\'),
- array('@'),
- array(':'),
- array(true),
- array(null),
- array(1),
- array(1.1),
- array(array(array())),
- array(new \Exception('foo')),
- );
+ return [
+ [''],
+ ['{'],
+ ['}'],
+ ['('],
+ [')'],
+ ['/'],
+ ['\\'],
+ ['@'],
+ [':'],
+ [true],
+ [null],
+ [1],
+ [1.1],
+ [[[]]],
+ [new \Exception('foo')],
+ ];
}
public function testTag()
{
$item = new CacheItem();
+ $r = new \ReflectionProperty($item, 'isTaggable');
+ $r->setAccessible(true);
+ $r->setValue($item, true);
$this->assertSame($item, $item->tag('foo'));
- $this->assertSame($item, $item->tag(array('bar', 'baz')));
+ $this->assertSame($item, $item->tag(['bar', 'baz']));
- \call_user_func(\Closure::bind(function () use ($item) {
- $this->assertSame(array('foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'), $item->tags);
- }, $this, CacheItem::class));
+ (\Closure::bind(function () use ($item) {
+ $this->assertSame(['foo' => 'foo', 'bar' => 'bar', 'baz' => 'baz'], $item->newMetadata[CacheItem::METADATA_TAGS]);
+ }, $this, CacheItem::class))();
}
/**
* @dataProvider provideInvalidKey
- * @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
- * @expectedExceptionMessage Cache tag
*/
public function testInvalidTag($tag)
{
+ $this->expectException('Symfony\Component\Cache\Exception\InvalidArgumentException');
+ $this->expectExceptionMessage('Cache tag');
$item = new CacheItem();
+ $r = new \ReflectionProperty($item, 'isTaggable');
+ $r->setAccessible(true);
+ $r->setValue($item, true);
+
$item->tag($tag);
}
+
+ public function testNonTaggableItem()
+ {
+ $this->expectException('Symfony\Component\Cache\Exception\LogicException');
+ $this->expectExceptionMessage('Cache item "foo" comes from a non tag-aware pool: you cannot tag it.');
+ $item = new CacheItem();
+ $r = new \ReflectionProperty($item, 'key');
+ $r->setAccessible(true);
+ $r->setValue($item, 'foo');
+
+ $item->tag([]);
+ }
}
diff --git a/Tests/DependencyInjection/CacheCollectorPassTest.php b/Tests/DependencyInjection/CacheCollectorPassTest.php
new file mode 100644
index 00000000..7e77491d
--- /dev/null
+++ b/Tests/DependencyInjection/CacheCollectorPassTest.php
@@ -0,0 +1,49 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\Tests\DependencyInjection;
+
+use PHPUnit\Framework\TestCase;
+use Symfony\Component\Cache\Adapter\FilesystemAdapter;
+use Symfony\Component\Cache\Adapter\TagAwareAdapter;
+use Symfony\Component\Cache\Adapter\TraceableAdapter;
+use Symfony\Component\Cache\Adapter\TraceableTagAwareAdapter;
+use Symfony\Component\Cache\DataCollector\CacheDataCollector;
+use Symfony\Component\Cache\DependencyInjection\CacheCollectorPass;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Reference;
+
+class CacheCollectorPassTest extends TestCase
+{
+ public function testProcess()
+ {
+ $container = new ContainerBuilder();
+ $container
+ ->register('fs', FilesystemAdapter::class)
+ ->addTag('cache.pool');
+ $container
+ ->register('tagged_fs', TagAwareAdapter::class)
+ ->addArgument(new Reference('fs'))
+ ->addTag('cache.pool');
+
+ $collector = $container->register('data_collector.cache', CacheDataCollector::class);
+ (new CacheCollectorPass())->process($container);
+
+ $this->assertEquals([
+ ['addInstance', ['fs', new Reference('fs')]],
+ ['addInstance', ['tagged_fs', new Reference('tagged_fs')]],
+ ], $collector->getMethodCalls());
+
+ $this->assertSame(TraceableAdapter::class, $container->findDefinition('fs')->getClass());
+ $this->assertSame(TraceableTagAwareAdapter::class, $container->getDefinition('tagged_fs')->getClass());
+ $this->assertFalse($collector->isPublic(), 'The "data_collector.cache" should be private after processing');
+ }
+}
diff --git a/Tests/DependencyInjection/CachePoolClearerPassTest.php b/Tests/DependencyInjection/CachePoolClearerPassTest.php
new file mode 100644
index 00000000..533aa14a
--- /dev/null
+++ b/Tests/DependencyInjection/CachePoolClearerPassTest.php
@@ -0,0 +1,73 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\Tests\DependencyInjection;
+
+use PHPUnit\Framework\TestCase;
+use Symfony\Component\Cache\DependencyInjection\CachePoolClearerPass;
+use Symfony\Component\Cache\DependencyInjection\CachePoolPass;
+use Symfony\Component\DependencyInjection\Compiler\RemoveUnusedDefinitionsPass;
+use Symfony\Component\DependencyInjection\Compiler\RepeatedPass;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Definition;
+use Symfony\Component\DependencyInjection\Reference;
+use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer;
+
+class CachePoolClearerPassTest extends TestCase
+{
+ public function testPoolRefsAreWeak()
+ {
+ $container = new ContainerBuilder();
+ $container->setParameter('kernel.container_class', 'app');
+ $container->setParameter('kernel.project_dir', 'foo');
+
+ $globalClearer = new Definition(Psr6CacheClearer::class);
+ $container->setDefinition('cache.global_clearer', $globalClearer);
+
+ $publicPool = new Definition();
+ $publicPool->addArgument('namespace');
+ $publicPool->addTag('cache.pool', ['clearer' => 'clearer_alias']);
+ $container->setDefinition('public.pool', $publicPool);
+
+ $publicPool = new Definition();
+ $publicPool->addArgument('namespace');
+ $publicPool->addTag('cache.pool', ['clearer' => 'clearer_alias', 'name' => 'pool2']);
+ $container->setDefinition('public.pool2', $publicPool);
+
+ $privatePool = new Definition();
+ $privatePool->setPublic(false);
+ $privatePool->addArgument('namespace');
+ $privatePool->addTag('cache.pool', ['clearer' => 'clearer_alias']);
+ $container->setDefinition('private.pool', $privatePool);
+
+ $clearer = new Definition();
+ $container->setDefinition('clearer', $clearer);
+ $container->setAlias('clearer_alias', 'clearer');
+
+ $pass = new RemoveUnusedDefinitionsPass();
+ foreach ($container->getCompiler()->getPassConfig()->getRemovingPasses() as $removingPass) {
+ if ($removingPass instanceof RepeatedPass) {
+ $pass->setRepeatedPass(new RepeatedPass([$pass]));
+ break;
+ }
+ }
+ foreach ([new CachePoolPass(), $pass, new CachePoolClearerPass()] as $pass) {
+ $pass->process($container);
+ }
+
+ $expected = [[
+ 'public.pool' => new Reference('public.pool'),
+ 'pool2' => new Reference('public.pool2'),
+ ]];
+ $this->assertEquals($expected, $clearer->getArguments());
+ $this->assertEquals($expected, $globalClearer->getArguments());
+ }
+}
diff --git a/Tests/DependencyInjection/CachePoolPassTest.php b/Tests/DependencyInjection/CachePoolPassTest.php
new file mode 100644
index 00000000..e763dabe
--- /dev/null
+++ b/Tests/DependencyInjection/CachePoolPassTest.php
@@ -0,0 +1,177 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\Tests\DependencyInjection;
+
+use PHPUnit\Framework\TestCase;
+use Symfony\Component\Cache\Adapter\ArrayAdapter;
+use Symfony\Component\Cache\Adapter\RedisAdapter;
+use Symfony\Component\Cache\DependencyInjection\CachePoolPass;
+use Symfony\Component\DependencyInjection\ChildDefinition;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Definition;
+use Symfony\Component\DependencyInjection\Reference;
+
+class CachePoolPassTest extends TestCase
+{
+ private $cachePoolPass;
+
+ protected function setUp(): void
+ {
+ $this->cachePoolPass = new CachePoolPass();
+ }
+
+ public function testNamespaceArgumentIsReplaced()
+ {
+ $container = new ContainerBuilder();
+ $container->setParameter('kernel.container_class', 'app');
+ $container->setParameter('kernel.project_dir', 'foo');
+ $adapter = new Definition();
+ $adapter->setAbstract(true);
+ $adapter->addTag('cache.pool');
+ $container->setDefinition('app.cache_adapter', $adapter);
+ $container->setAlias('app.cache_adapter_alias', 'app.cache_adapter');
+ $cachePool = new ChildDefinition('app.cache_adapter_alias');
+ $cachePool->addArgument(null);
+ $cachePool->addTag('cache.pool');
+ $container->setDefinition('app.cache_pool', $cachePool);
+
+ $this->cachePoolPass->process($container);
+
+ $this->assertSame('z3X945Jbf5', $cachePool->getArgument(0));
+ }
+
+ public function testNamespaceArgumentIsSeededWithAdapterClassName()
+ {
+ $container = new ContainerBuilder();
+ $container->setParameter('kernel.container_class', 'app');
+ $container->setParameter('kernel.project_dir', 'foo');
+ $adapter = new Definition();
+ $adapter->setAbstract(true);
+ $adapter->addTag('cache.pool');
+ $adapter->setClass(RedisAdapter::class);
+ $container->setDefinition('app.cache_adapter', $adapter);
+ $container->setAlias('app.cache_adapter_alias', 'app.cache_adapter');
+ $cachePool = new ChildDefinition('app.cache_adapter_alias');
+ $cachePool->addArgument(null);
+ $cachePool->addTag('cache.pool');
+ $container->setDefinition('app.cache_pool', $cachePool);
+
+ $this->cachePoolPass->process($container);
+
+ $this->assertSame('xmOJ8gqF-Y', $cachePool->getArgument(0));
+ }
+
+ public function testNamespaceArgumentIsSeededWithAdapterClassNameWithoutAffectingOtherCachePools()
+ {
+ $container = new ContainerBuilder();
+ $container->setParameter('kernel.container_class', 'app');
+ $container->setParameter('kernel.project_dir', 'foo');
+ $adapter = new Definition();
+ $adapter->setAbstract(true);
+ $adapter->addTag('cache.pool');
+ $adapter->setClass(RedisAdapter::class);
+ $container->setDefinition('app.cache_adapter', $adapter);
+ $container->setAlias('app.cache_adapter_alias', 'app.cache_adapter');
+
+ $otherCachePool = new ChildDefinition('app.cache_adapter_alias');
+ $otherCachePool->addArgument(null);
+ $otherCachePool->addTag('cache.pool');
+ $container->setDefinition('app.other_cache_pool', $otherCachePool);
+
+ $cachePool = new ChildDefinition('app.cache_adapter_alias');
+ $cachePool->addArgument(null);
+ $cachePool->addTag('cache.pool');
+ $container->setDefinition('app.cache_pool', $cachePool);
+
+ $this->cachePoolPass->process($container);
+
+ $this->assertSame('xmOJ8gqF-Y', $cachePool->getArgument(0));
+ }
+
+ public function testNamespaceArgumentIsNotReplacedIfArrayAdapterIsUsed()
+ {
+ $container = new ContainerBuilder();
+ $container->setParameter('kernel.container_class', 'app');
+ $container->setParameter('kernel.project_dir', 'foo');
+
+ $container->register('cache.adapter.array', ArrayAdapter::class)->addArgument(0);
+
+ $cachePool = new ChildDefinition('cache.adapter.array');
+ $cachePool->addTag('cache.pool');
+ $container->setDefinition('app.cache_pool', $cachePool);
+
+ $this->cachePoolPass->process($container);
+
+ $this->assertCount(0, $container->getDefinition('app.cache_pool')->getArguments());
+ }
+
+ public function testArgsAreReplaced()
+ {
+ $container = new ContainerBuilder();
+ $container->setParameter('kernel.container_class', 'app');
+ $container->setParameter('cache.prefix.seed', 'foo');
+ $cachePool = new Definition();
+ $cachePool->addTag('cache.pool', [
+ 'provider' => 'foobar',
+ 'default_lifetime' => 3,
+ ]);
+ $cachePool->addArgument(null);
+ $cachePool->addArgument(null);
+ $cachePool->addArgument(null);
+ $container->setDefinition('app.cache_pool', $cachePool);
+
+ $this->cachePoolPass->process($container);
+
+ $this->assertInstanceOf(Reference::class, $cachePool->getArgument(0));
+ $this->assertSame('foobar', (string) $cachePool->getArgument(0));
+ $this->assertSame('tQNhcV-8xa', $cachePool->getArgument(1));
+ $this->assertSame(3, $cachePool->getArgument(2));
+ }
+
+ public function testWithNameAttribute()
+ {
+ $container = new ContainerBuilder();
+ $container->setParameter('kernel.container_class', 'app');
+ $container->setParameter('cache.prefix.seed', 'foo');
+ $cachePool = new Definition();
+ $cachePool->addTag('cache.pool', [
+ 'name' => 'foobar',
+ 'provider' => 'foobar',
+ ]);
+ $cachePool->addArgument(null);
+ $cachePool->addArgument(null);
+ $cachePool->addArgument(null);
+ $container->setDefinition('app.cache_pool', $cachePool);
+
+ $this->cachePoolPass->process($container);
+
+ $this->assertSame('+naTpPa4Sm', $cachePool->getArgument(1));
+ }
+
+ public function testThrowsExceptionWhenCachePoolTagHasUnknownAttributes()
+ {
+ $this->expectException('InvalidArgumentException');
+ $this->expectExceptionMessage('Invalid "cache.pool" tag for service "app.cache_pool": accepted attributes are');
+ $container = new ContainerBuilder();
+ $container->setParameter('kernel.container_class', 'app');
+ $container->setParameter('kernel.project_dir', 'foo');
+ $adapter = new Definition();
+ $adapter->setAbstract(true);
+ $adapter->addTag('cache.pool');
+ $container->setDefinition('app.cache_adapter', $adapter);
+ $cachePool = new ChildDefinition('app.cache_adapter');
+ $cachePool->addTag('cache.pool', ['foobar' => 123]);
+ $container->setDefinition('app.cache_pool', $cachePool);
+
+ $this->cachePoolPass->process($container);
+ }
+}
diff --git a/Tests/DependencyInjection/CachePoolPrunerPassTest.php b/Tests/DependencyInjection/CachePoolPrunerPassTest.php
new file mode 100644
index 00000000..2a1ab49b
--- /dev/null
+++ b/Tests/DependencyInjection/CachePoolPrunerPassTest.php
@@ -0,0 +1,70 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\Tests\DependencyInjection;
+
+use PHPUnit\Framework\TestCase;
+use Symfony\Component\Cache\Adapter\FilesystemAdapter;
+use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
+use Symfony\Component\Cache\DependencyInjection\CachePoolPrunerPass;
+use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Reference;
+
+class CachePoolPrunerPassTest extends TestCase
+{
+ public function testCompilerPassReplacesCommandArgument()
+ {
+ $container = new ContainerBuilder();
+ $container->register('console.command.cache_pool_prune')->addArgument([]);
+ $container->register('pool.foo', FilesystemAdapter::class)->addTag('cache.pool');
+ $container->register('pool.bar', PhpFilesAdapter::class)->addTag('cache.pool');
+
+ $pass = new CachePoolPrunerPass();
+ $pass->process($container);
+
+ $expected = [
+ 'pool.foo' => new Reference('pool.foo'),
+ 'pool.bar' => new Reference('pool.bar'),
+ ];
+ $argument = $container->getDefinition('console.command.cache_pool_prune')->getArgument(0);
+
+ $this->assertInstanceOf(IteratorArgument::class, $argument);
+ $this->assertEquals($expected, $argument->getValues());
+ }
+
+ public function testCompilePassIsIgnoredIfCommandDoesNotExist()
+ {
+ $container = new ContainerBuilder();
+
+ $definitionsBefore = \count($container->getDefinitions());
+ $aliasesBefore = \count($container->getAliases());
+
+ $pass = new CachePoolPrunerPass();
+ $pass->process($container);
+
+ // the container is untouched (i.e. no new definitions or aliases)
+ $this->assertCount($definitionsBefore, $container->getDefinitions());
+ $this->assertCount($aliasesBefore, $container->getAliases());
+ }
+
+ public function testCompilerPassThrowsOnInvalidDefinitionClass()
+ {
+ $this->expectException('Symfony\Component\DependencyInjection\Exception\InvalidArgumentException');
+ $this->expectExceptionMessage('Class "Symfony\Component\Cache\Tests\DependencyInjection\NotFound" used for service "pool.not-found" cannot be found.');
+ $container = new ContainerBuilder();
+ $container->register('console.command.cache_pool_prune')->addArgument([]);
+ $container->register('pool.not-found', NotFound::class)->addTag('cache.pool');
+
+ $pass = new CachePoolPrunerPass();
+ $pass->process($container);
+ }
+}
diff --git a/Tests/Fixtures/ArrayCache.php b/Tests/Fixtures/ArrayCache.php
index 7cdcafd8..95b39d54 100644
--- a/Tests/Fixtures/ArrayCache.php
+++ b/Tests/Fixtures/ArrayCache.php
@@ -6,7 +6,7 @@
class ArrayCache extends CacheProvider
{
- private $data = array();
+ private $data = [];
protected function doFetch($id)
{
@@ -21,12 +21,12 @@ protected function doContains($id)
$expiry = $this->data[$id][1];
- return !$expiry || time() < $expiry || !$this->doDelete($id);
+ return !$expiry || microtime(true) < $expiry || !$this->doDelete($id);
}
protected function doSave($id, $data, $lifeTime = 0)
{
- $this->data[$id] = array($data, $lifeTime ? time() + $lifeTime : false);
+ $this->data[$id] = [$data, $lifeTime ? microtime(true) + $lifeTime : false];
return true;
}
@@ -40,7 +40,7 @@ protected function doDelete($id)
protected function doFlush()
{
- $this->data = array();
+ $this->data = [];
return true;
}
diff --git a/Tests/Fixtures/ExternalAdapter.php b/Tests/Fixtures/ExternalAdapter.php
index 493906ea..deb0b3bc 100644
--- a/Tests/Fixtures/ExternalAdapter.php
+++ b/Tests/Fixtures/ExternalAdapter.php
@@ -24,9 +24,9 @@ class ExternalAdapter implements CacheItemPoolInterface
{
private $cache;
- public function __construct()
+ public function __construct(int $defaultLifetime = 0)
{
- $this->cache = new ArrayAdapter();
+ $this->cache = new ArrayAdapter($defaultLifetime);
}
public function getItem($key)
@@ -34,7 +34,7 @@ public function getItem($key)
return $this->cache->getItem($key);
}
- public function getItems(array $keys = array())
+ public function getItems(array $keys = [])
{
return $this->cache->getItems($keys);
}
diff --git a/Tests/LockRegistryTest.php b/Tests/LockRegistryTest.php
new file mode 100644
index 00000000..0771347e
--- /dev/null
+++ b/Tests/LockRegistryTest.php
@@ -0,0 +1,26 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\Tests;
+
+use PHPUnit\Framework\TestCase;
+use Symfony\Component\Cache\LockRegistry;
+
+class LockRegistryTest extends TestCase
+{
+ public function testFiles()
+ {
+ $lockFiles = LockRegistry::setFiles([]);
+ LockRegistry::setFiles($lockFiles);
+ $expected = array_map('realpath', glob(__DIR__.'/../Adapter/*'));
+ $this->assertSame($expected, $lockFiles);
+ }
+}
diff --git a/Tests/Marshaller/DefaultMarshallerTest.php b/Tests/Marshaller/DefaultMarshallerTest.php
new file mode 100644
index 00000000..cc94ad15
--- /dev/null
+++ b/Tests/Marshaller/DefaultMarshallerTest.php
@@ -0,0 +1,112 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\Tests\Marshaller;
+
+use PHPUnit\Framework\TestCase;
+use Symfony\Component\Cache\Marshaller\DefaultMarshaller;
+
+class DefaultMarshallerTest extends TestCase
+{
+ public function testSerialize()
+ {
+ $marshaller = new DefaultMarshaller();
+ $values = [
+ 'a' => 123,
+ 'b' => function () {},
+ ];
+
+ $expected = ['a' => \extension_loaded('igbinary') && \PHP_VERSION_ID < 70400 ? igbinary_serialize(123) : serialize(123)];
+ $this->assertSame($expected, $marshaller->marshall($values, $failed));
+ $this->assertSame(['b'], $failed);
+ }
+
+ public function testNativeUnserialize()
+ {
+ $marshaller = new DefaultMarshaller();
+ $this->assertNull($marshaller->unmarshall(serialize(null)));
+ $this->assertFalse($marshaller->unmarshall(serialize(false)));
+ $this->assertSame('', $marshaller->unmarshall(serialize('')));
+ $this->assertSame(0, $marshaller->unmarshall(serialize(0)));
+ }
+
+ /**
+ * @requires extension igbinary
+ */
+ public function testIgbinaryUnserialize()
+ {
+ if (\PHP_VERSION_ID >= 70400) {
+ $this->markTestSkipped('igbinary is not compatible with PHP 7.4.');
+ }
+
+ $marshaller = new DefaultMarshaller();
+ $this->assertNull($marshaller->unmarshall(igbinary_serialize(null)));
+ $this->assertFalse($marshaller->unmarshall(igbinary_serialize(false)));
+ $this->assertSame('', $marshaller->unmarshall(igbinary_serialize('')));
+ $this->assertSame(0, $marshaller->unmarshall(igbinary_serialize(0)));
+ }
+
+ public function testNativeUnserializeNotFoundClass()
+ {
+ $this->expectException('DomainException');
+ $this->expectExceptionMessage('Class not found: NotExistingClass');
+ $marshaller = new DefaultMarshaller();
+ $marshaller->unmarshall('O:16:"NotExistingClass":0:{}');
+ }
+
+ /**
+ * @requires extension igbinary
+ */
+ public function testIgbinaryUnserializeNotFoundClass()
+ {
+ if (\PHP_VERSION_ID >= 70400) {
+ $this->markTestSkipped('igbinary is not compatible with PHP 7.4.');
+ }
+
+ $this->expectException('DomainException');
+ $this->expectExceptionMessage('Class not found: NotExistingClass');
+ $marshaller = new DefaultMarshaller();
+ $marshaller->unmarshall(rawurldecode('%00%00%00%02%17%10NotExistingClass%14%00'));
+ }
+
+ public function testNativeUnserializeInvalid()
+ {
+ $this->expectException('DomainException');
+ $this->expectExceptionMessage('unserialize(): Error at offset 0 of 3 bytes');
+ $marshaller = new DefaultMarshaller();
+ set_error_handler(function () { return false; });
+ try {
+ @$marshaller->unmarshall(':::');
+ } finally {
+ restore_error_handler();
+ }
+ }
+
+ /**
+ * @requires extension igbinary
+ */
+ public function testIgbinaryUnserializeInvalid()
+ {
+ if (\PHP_VERSION_ID >= 70400) {
+ $this->markTestSkipped('igbinary is not compatible with PHP 7.4.');
+ }
+
+ $this->expectException('DomainException');
+ $this->expectExceptionMessage('igbinary_unserialize_zval: unknown type \'61\', position 5');
+ $marshaller = new DefaultMarshaller();
+ set_error_handler(function () { return false; });
+ try {
+ @$marshaller->unmarshall(rawurldecode('%00%00%00%02abc'));
+ } finally {
+ restore_error_handler();
+ }
+ }
+}
diff --git a/Tests/Psr16CacheTest.php b/Tests/Psr16CacheTest.php
new file mode 100644
index 00000000..7774e1dd
--- /dev/null
+++ b/Tests/Psr16CacheTest.php
@@ -0,0 +1,168 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\Tests;
+
+use Cache\IntegrationTests\SimpleCacheTest;
+use Symfony\Component\Cache\Adapter\FilesystemAdapter;
+use Symfony\Component\Cache\PruneableInterface;
+use Symfony\Component\Cache\Psr16Cache;
+
+/**
+ * @group time-sensitive
+ */
+class Psr16CacheTest extends SimpleCacheTest
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ if (\array_key_exists('testPrune', $this->skippedTests)) {
+ return;
+ }
+
+ $pool = $this->createSimpleCache();
+ if ($pool instanceof Psr16Cache) {
+ $pool = ((array) $pool)[sprintf("\0%s\0pool", Psr16Cache::class)];
+ }
+
+ if (!$pool instanceof PruneableInterface) {
+ $this->skippedTests['testPrune'] = 'Not a pruneable cache pool.';
+ }
+ }
+
+ public function createSimpleCache($defaultLifetime = 0)
+ {
+ return new Psr16Cache(new FilesystemAdapter('', $defaultLifetime));
+ }
+
+ public static function validKeys()
+ {
+ return array_merge(parent::validKeys(), [["a\0b"]]);
+ }
+
+ public function testDefaultLifeTime()
+ {
+ if (isset($this->skippedTests[__FUNCTION__])) {
+ $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
+ }
+
+ $cache = $this->createSimpleCache(2);
+ $cache->clear();
+
+ $cache->set('key.dlt', 'value');
+ sleep(1);
+
+ $this->assertSame('value', $cache->get('key.dlt'));
+
+ sleep(2);
+ $this->assertNull($cache->get('key.dlt'));
+
+ $cache->clear();
+ }
+
+ public function testNotUnserializable()
+ {
+ if (isset($this->skippedTests[__FUNCTION__])) {
+ $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
+ }
+
+ $cache = $this->createSimpleCache();
+ $cache->clear();
+
+ $cache->set('foo', new NotUnserializable());
+
+ $this->assertNull($cache->get('foo'));
+
+ $cache->setMultiple(['foo' => new NotUnserializable()]);
+
+ foreach ($cache->getMultiple(['foo']) as $value) {
+ }
+ $this->assertNull($value);
+
+ $cache->clear();
+ }
+
+ public function testPrune()
+ {
+ if (isset($this->skippedTests[__FUNCTION__])) {
+ $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
+ }
+
+ /** @var PruneableInterface|CacheInterface $cache */
+ $cache = $this->createSimpleCache();
+ $cache->clear();
+
+ $cache->set('foo', 'foo-val', new \DateInterval('PT05S'));
+ $cache->set('bar', 'bar-val', new \DateInterval('PT10S'));
+ $cache->set('baz', 'baz-val', new \DateInterval('PT15S'));
+ $cache->set('qux', 'qux-val', new \DateInterval('PT20S'));
+
+ sleep(30);
+ $cache->prune();
+ $this->assertTrue($this->isPruned($cache, 'foo'));
+ $this->assertTrue($this->isPruned($cache, 'bar'));
+ $this->assertTrue($this->isPruned($cache, 'baz'));
+ $this->assertTrue($this->isPruned($cache, 'qux'));
+
+ $cache->set('foo', 'foo-val');
+ $cache->set('bar', 'bar-val', new \DateInterval('PT20S'));
+ $cache->set('baz', 'baz-val', new \DateInterval('PT40S'));
+ $cache->set('qux', 'qux-val', new \DateInterval('PT80S'));
+
+ $cache->prune();
+ $this->assertFalse($this->isPruned($cache, 'foo'));
+ $this->assertFalse($this->isPruned($cache, 'bar'));
+ $this->assertFalse($this->isPruned($cache, 'baz'));
+ $this->assertFalse($this->isPruned($cache, 'qux'));
+
+ sleep(30);
+ $cache->prune();
+ $this->assertFalse($this->isPruned($cache, 'foo'));
+ $this->assertTrue($this->isPruned($cache, 'bar'));
+ $this->assertFalse($this->isPruned($cache, 'baz'));
+ $this->assertFalse($this->isPruned($cache, 'qux'));
+
+ sleep(30);
+ $cache->prune();
+ $this->assertFalse($this->isPruned($cache, 'foo'));
+ $this->assertTrue($this->isPruned($cache, 'baz'));
+ $this->assertFalse($this->isPruned($cache, 'qux'));
+
+ sleep(30);
+ $cache->prune();
+ $this->assertFalse($this->isPruned($cache, 'foo'));
+ $this->assertTrue($this->isPruned($cache, 'qux'));
+
+ $cache->clear();
+ }
+
+ protected function isPruned($cache, $name)
+ {
+ if (Psr16Cache::class !== \get_class($cache)) {
+ $this->fail('Test classes for pruneable caches must implement `isPruned($cache, $name)` method.');
+ }
+
+ $pool = ((array) $cache)[sprintf("\0%s\0pool", Psr16Cache::class)];
+ $getFileMethod = (new \ReflectionObject($pool))->getMethod('getFile');
+ $getFileMethod->setAccessible(true);
+
+ return !file_exists($getFileMethod->invoke($pool, $name));
+ }
+}
+
+class NotUnserializable
+{
+ public function __wakeup()
+ {
+ throw new \Exception(__CLASS__);
+ }
+}
diff --git a/Tests/Simple/AbstractRedisCacheTest.php b/Tests/Simple/AbstractRedisCacheTest.php
index 3e668fdd..81718970 100644
--- a/Tests/Simple/AbstractRedisCacheTest.php
+++ b/Tests/Simple/AbstractRedisCacheTest.php
@@ -13,13 +13,16 @@
use Symfony\Component\Cache\Simple\RedisCache;
+/**
+ * @group legacy
+ */
abstract class AbstractRedisCacheTest extends CacheTestCase
{
- protected $skippedTests = array(
+ protected $skippedTests = [
'testSetTtl' => 'Testing expiration slows down the test suite',
'testSetMultipleTtl' => 'Testing expiration slows down the test suite',
'testDefaultLifeTime' => 'Testing expiration slows down the test suite',
- );
+ ];
protected static $redis;
@@ -28,7 +31,7 @@ public function createSimpleCache($defaultLifetime = 0)
return new RedisCache(self::$redis, str_replace('\\', '.', __CLASS__), $defaultLifetime);
}
- public static function setupBeforeClass()
+ public static function setUpBeforeClass(): void
{
if (!\extension_loaded('redis')) {
self::markTestSkipped('Extension redis required.');
@@ -39,7 +42,7 @@ public static function setupBeforeClass()
}
}
- public static function tearDownAfterClass()
+ public static function tearDownAfterClass(): void
{
self::$redis = null;
}
diff --git a/Tests/Simple/ApcuCacheTest.php b/Tests/Simple/ApcuCacheTest.php
index 737ed4e9..b3220946 100644
--- a/Tests/Simple/ApcuCacheTest.php
+++ b/Tests/Simple/ApcuCacheTest.php
@@ -13,17 +13,20 @@
use Symfony\Component\Cache\Simple\ApcuCache;
+/**
+ * @group legacy
+ */
class ApcuCacheTest extends CacheTestCase
{
- protected $skippedTests = array(
+ protected $skippedTests = [
'testSetTtl' => 'Testing expiration slows down the test suite',
'testSetMultipleTtl' => 'Testing expiration slows down the test suite',
'testDefaultLifeTime' => 'Testing expiration slows down the test suite',
- );
+ ];
public function createSimpleCache($defaultLifetime = 0)
{
- if (!\function_exists('apcu_fetch') || !ini_get('apc.enabled') || ('cli' === \PHP_SAPI && !ini_get('apc.enable_cli'))) {
+ if (!\function_exists('apcu_fetch') || !filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) || ('cli' === \PHP_SAPI && !filter_var(ini_get('apc.enable_cli'), FILTER_VALIDATE_BOOLEAN))) {
$this->markTestSkipped('APCu extension is required.');
}
if ('\\' === \DIRECTORY_SEPARATOR) {
diff --git a/Tests/Simple/ArrayCacheTest.php b/Tests/Simple/ArrayCacheTest.php
index 26c3e14d..587304a5 100644
--- a/Tests/Simple/ArrayCacheTest.php
+++ b/Tests/Simple/ArrayCacheTest.php
@@ -15,6 +15,7 @@
/**
* @group time-sensitive
+ * @group legacy
*/
class ArrayCacheTest extends CacheTestCase
{
diff --git a/Tests/Simple/CacheTestCase.php b/Tests/Simple/CacheTestCase.php
index 5b84d8b0..d23a0ff8 100644
--- a/Tests/Simple/CacheTestCase.php
+++ b/Tests/Simple/CacheTestCase.php
@@ -17,18 +17,18 @@
abstract class CacheTestCase extends SimpleCacheTest
{
- protected function setUp()
+ protected function setUp(): void
{
parent::setUp();
- if (!array_key_exists('testPrune', $this->skippedTests) && !$this->createSimpleCache() instanceof PruneableInterface) {
+ if (!\array_key_exists('testPrune', $this->skippedTests) && !$this->createSimpleCache() instanceof PruneableInterface) {
$this->skippedTests['testPrune'] = 'Not a pruneable cache pool.';
}
}
public static function validKeys()
{
- return array_merge(parent::validKeys(), array(array("a\0b")));
+ return array_merge(parent::validKeys(), [["a\0b"]]);
}
public function testDefaultLifeTime()
@@ -64,9 +64,9 @@ public function testNotUnserializable()
$this->assertNull($cache->get('foo'));
- $cache->setMultiple(array('foo' => new NotUnserializable()));
+ $cache->setMultiple(['foo' => new NotUnserializable()]);
- foreach ($cache->getMultiple(array('foo')) as $value) {
+ foreach ($cache->getMultiple(['foo']) as $value) {
}
$this->assertNull($value);
@@ -132,14 +132,9 @@ public function testPrune()
}
}
-class NotUnserializable implements \Serializable
+class NotUnserializable
{
- public function serialize()
- {
- return serialize(123);
- }
-
- public function unserialize($ser)
+ public function __wakeup()
{
throw new \Exception(__CLASS__);
}
diff --git a/Tests/Simple/ChainCacheTest.php b/Tests/Simple/ChainCacheTest.php
index ab28e3bc..3ec828c1 100644
--- a/Tests/Simple/ChainCacheTest.php
+++ b/Tests/Simple/ChainCacheTest.php
@@ -11,6 +11,7 @@
namespace Symfony\Component\Cache\Tests\Simple;
+use PHPUnit\Framework\MockObject\MockObject;
use Psr\SimpleCache\CacheInterface;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\Simple\ArrayCache;
@@ -19,30 +20,27 @@
/**
* @group time-sensitive
+ * @group legacy
*/
class ChainCacheTest extends CacheTestCase
{
public function createSimpleCache($defaultLifetime = 0)
{
- return new ChainCache(array(new ArrayCache($defaultLifetime), new FilesystemCache('', $defaultLifetime)), $defaultLifetime);
+ return new ChainCache([new ArrayCache($defaultLifetime), new FilesystemCache('', $defaultLifetime)], $defaultLifetime);
}
- /**
- * @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
- * @expectedExceptionMessage At least one cache must be specified.
- */
public function testEmptyCachesException()
{
- new ChainCache(array());
+ $this->expectException('Symfony\Component\Cache\Exception\InvalidArgumentException');
+ $this->expectExceptionMessage('At least one cache must be specified.');
+ new ChainCache([]);
}
- /**
- * @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
- * @expectedExceptionMessage The class "stdClass" does not implement
- */
public function testInvalidCacheException()
{
- new ChainCache(array(new \stdClass()));
+ $this->expectException('Symfony\Component\Cache\Exception\InvalidArgumentException');
+ $this->expectExceptionMessage('The class "stdClass" does not implement');
+ new ChainCache([new \stdClass()]);
}
public function testPrune()
@@ -51,23 +49,23 @@ public function testPrune()
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
}
- $cache = new ChainCache(array(
+ $cache = new ChainCache([
$this->getPruneableMock(),
$this->getNonPruneableMock(),
$this->getPruneableMock(),
- ));
+ ]);
$this->assertTrue($cache->prune());
- $cache = new ChainCache(array(
+ $cache = new ChainCache([
$this->getPruneableMock(),
$this->getFailingPruneableMock(),
$this->getPruneableMock(),
- ));
+ ]);
$this->assertFalse($cache->prune());
}
/**
- * @return \PHPUnit_Framework_MockObject_MockObject|PruneableCacheInterface
+ * @return MockObject|PruneableCacheInterface
*/
private function getPruneableMock()
{
@@ -78,13 +76,13 @@ private function getPruneableMock()
$pruneable
->expects($this->atLeastOnce())
->method('prune')
- ->will($this->returnValue(true));
+ ->willReturn(true);
return $pruneable;
}
/**
- * @return \PHPUnit_Framework_MockObject_MockObject|PruneableCacheInterface
+ * @return MockObject|PruneableCacheInterface
*/
private function getFailingPruneableMock()
{
@@ -95,13 +93,13 @@ private function getFailingPruneableMock()
$pruneable
->expects($this->atLeastOnce())
->method('prune')
- ->will($this->returnValue(false));
+ ->willReturn(false);
return $pruneable;
}
/**
- * @return \PHPUnit_Framework_MockObject_MockObject|CacheInterface
+ * @return MockObject|CacheInterface
*/
private function getNonPruneableMock()
{
diff --git a/Tests/Simple/DoctrineCacheTest.php b/Tests/Simple/DoctrineCacheTest.php
index 127c9685..5d78c00c 100644
--- a/Tests/Simple/DoctrineCacheTest.php
+++ b/Tests/Simple/DoctrineCacheTest.php
@@ -16,13 +16,14 @@
/**
* @group time-sensitive
+ * @group legacy
*/
class DoctrineCacheTest extends CacheTestCase
{
- protected $skippedTests = array(
+ protected $skippedTests = [
'testObjectDoesNotChangeInCache' => 'ArrayCache does not use serialize/unserialize',
'testNotUnserializable' => 'ArrayCache does not use serialize/unserialize',
- );
+ ];
public function createSimpleCache($defaultLifetime = 0)
{
diff --git a/Tests/Simple/FilesystemCacheTest.php b/Tests/Simple/FilesystemCacheTest.php
index 620305a5..9f423ba6 100644
--- a/Tests/Simple/FilesystemCacheTest.php
+++ b/Tests/Simple/FilesystemCacheTest.php
@@ -16,6 +16,7 @@
/**
* @group time-sensitive
+ * @group legacy
*/
class FilesystemCacheTest extends CacheTestCase
{
diff --git a/Tests/Simple/MemcachedCacheTest.php b/Tests/Simple/MemcachedCacheTest.php
index b46d7e44..3a7b27b6 100644
--- a/Tests/Simple/MemcachedCacheTest.php
+++ b/Tests/Simple/MemcachedCacheTest.php
@@ -14,17 +14,20 @@
use Symfony\Component\Cache\Adapter\AbstractAdapter;
use Symfony\Component\Cache\Simple\MemcachedCache;
+/**
+ * @group legacy
+ */
class MemcachedCacheTest extends CacheTestCase
{
- protected $skippedTests = array(
+ protected $skippedTests = [
'testSetTtl' => 'Testing expiration slows down the test suite',
'testSetMultipleTtl' => 'Testing expiration slows down the test suite',
'testDefaultLifeTime' => 'Testing expiration slows down the test suite',
- );
+ ];
protected static $client;
- public static function setupBeforeClass()
+ public static function setUpBeforeClass(): void
{
if (!MemcachedCache::isSupported()) {
self::markTestSkipped('Extension memcached >=2.2.0 required.');
@@ -40,29 +43,29 @@ public static function setupBeforeClass()
public function createSimpleCache($defaultLifetime = 0)
{
- $client = $defaultLifetime ? AbstractAdapter::createConnection('memcached://'.getenv('MEMCACHED_HOST'), array('binary_protocol' => false)) : self::$client;
+ $client = $defaultLifetime ? AbstractAdapter::createConnection('memcached://'.getenv('MEMCACHED_HOST'), ['binary_protocol' => false]) : self::$client;
return new MemcachedCache($client, str_replace('\\', '.', __CLASS__), $defaultLifetime);
}
public function testCreatePersistentConnectionShouldNotDupServerList()
{
- $instance = MemcachedCache::createConnection('memcached://'.getenv('MEMCACHED_HOST'), array('persistent_id' => 'persistent'));
+ $instance = MemcachedCache::createConnection('memcached://'.getenv('MEMCACHED_HOST'), ['persistent_id' => 'persistent']);
$this->assertCount(1, $instance->getServerList());
- $instance = MemcachedCache::createConnection('memcached://'.getenv('MEMCACHED_HOST'), array('persistent_id' => 'persistent'));
+ $instance = MemcachedCache::createConnection('memcached://'.getenv('MEMCACHED_HOST'), ['persistent_id' => 'persistent']);
$this->assertCount(1, $instance->getServerList());
}
public function testOptions()
{
- $client = MemcachedCache::createConnection(array(), array(
+ $client = MemcachedCache::createConnection([], [
'libketama_compatible' => false,
'distribution' => 'modula',
'compression' => true,
'serializer' => 'php',
'hash' => 'md5',
- ));
+ ]);
$this->assertSame(\Memcached::SERIALIZER_PHP, $client->getOption(\Memcached::OPT_SERIALIZER));
$this->assertSame(\Memcached::HASH_MD5, $client->getOption(\Memcached::OPT_HASH));
@@ -73,46 +76,44 @@ public function testOptions()
/**
* @dataProvider provideBadOptions
- * @expectedException \ErrorException
- * @expectedExceptionMessage constant(): Couldn't find constant Memcached::
*/
public function testBadOptions($name, $value)
{
- MemcachedCache::createConnection(array(), array($name => $value));
+ $this->expectException('ErrorException');
+ $this->expectExceptionMessage('constant(): Couldn\'t find constant Memcached::');
+ MemcachedCache::createConnection([], [$name => $value]);
}
public function provideBadOptions()
{
- return array(
- array('foo', 'bar'),
- array('hash', 'zyx'),
- array('serializer', 'zyx'),
- array('distribution', 'zyx'),
- );
+ return [
+ ['foo', 'bar'],
+ ['hash', 'zyx'],
+ ['serializer', 'zyx'],
+ ['distribution', 'zyx'],
+ ];
}
public function testDefaultOptions()
{
$this->assertTrue(MemcachedCache::isSupported());
- $client = MemcachedCache::createConnection(array());
+ $client = MemcachedCache::createConnection([]);
$this->assertTrue($client->getOption(\Memcached::OPT_COMPRESSION));
$this->assertSame(1, $client->getOption(\Memcached::OPT_BINARY_PROTOCOL));
$this->assertSame(1, $client->getOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE));
}
- /**
- * @expectedException \Symfony\Component\Cache\Exception\CacheException
- * @expectedExceptionMessage MemcachedAdapter: "serializer" option must be "php" or "igbinary".
- */
public function testOptionSerializer()
{
+ $this->expectException('Symfony\Component\Cache\Exception\CacheException');
+ $this->expectExceptionMessage('MemcachedAdapter: "serializer" option must be "php" or "igbinary".');
if (!\Memcached::HAVE_JSON) {
$this->markTestSkipped('Memcached::HAVE_JSON required');
}
- new MemcachedCache(MemcachedCache::createConnection(array(), array('serializer' => 'json')));
+ new MemcachedCache(MemcachedCache::createConnection([], ['serializer' => 'json']));
}
/**
@@ -121,54 +122,54 @@ public function testOptionSerializer()
public function testServersSetting($dsn, $host, $port)
{
$client1 = MemcachedCache::createConnection($dsn);
- $client2 = MemcachedCache::createConnection(array($dsn));
- $client3 = MemcachedCache::createConnection(array(array($host, $port)));
- $expect = array(
+ $client2 = MemcachedCache::createConnection([$dsn]);
+ $client3 = MemcachedCache::createConnection([[$host, $port]]);
+ $expect = [
'host' => $host,
'port' => $port,
- );
+ ];
- $f = function ($s) { return array('host' => $s['host'], 'port' => $s['port']); };
- $this->assertSame(array($expect), array_map($f, $client1->getServerList()));
- $this->assertSame(array($expect), array_map($f, $client2->getServerList()));
- $this->assertSame(array($expect), array_map($f, $client3->getServerList()));
+ $f = function ($s) { return ['host' => $s['host'], 'port' => $s['port']]; };
+ $this->assertSame([$expect], array_map($f, $client1->getServerList()));
+ $this->assertSame([$expect], array_map($f, $client2->getServerList()));
+ $this->assertSame([$expect], array_map($f, $client3->getServerList()));
}
public function provideServersSetting()
{
- yield array(
+ yield [
'memcached://127.0.0.1/50',
'127.0.0.1',
11211,
- );
- yield array(
+ ];
+ yield [
'memcached://localhost:11222?weight=25',
'localhost',
11222,
- );
- if (ini_get('memcached.use_sasl')) {
- yield array(
+ ];
+ if (filter_var(ini_get('memcached.use_sasl'), FILTER_VALIDATE_BOOLEAN)) {
+ yield [
'memcached://user:password@127.0.0.1?weight=50',
'127.0.0.1',
11211,
- );
+ ];
}
- yield array(
+ yield [
'memcached:///var/run/memcached.sock?weight=25',
'/var/run/memcached.sock',
0,
- );
- yield array(
+ ];
+ yield [
'memcached:///var/local/run/memcached.socket?weight=25',
'/var/local/run/memcached.socket',
0,
- );
- if (ini_get('memcached.use_sasl')) {
- yield array(
+ ];
+ if (filter_var(ini_get('memcached.use_sasl'), FILTER_VALIDATE_BOOLEAN)) {
+ yield [
'memcached://user:password@/var/local/run/memcached.socket?weight=25',
'/var/local/run/memcached.socket',
0,
- );
+ ];
}
}
}
diff --git a/Tests/Simple/MemcachedCacheTextModeTest.php b/Tests/Simple/MemcachedCacheTextModeTest.php
index 43cadf90..d68131a1 100644
--- a/Tests/Simple/MemcachedCacheTextModeTest.php
+++ b/Tests/Simple/MemcachedCacheTextModeTest.php
@@ -14,11 +14,14 @@
use Symfony\Component\Cache\Adapter\AbstractAdapter;
use Symfony\Component\Cache\Simple\MemcachedCache;
+/**
+ * @group legacy
+ */
class MemcachedCacheTextModeTest extends MemcachedCacheTest
{
public function createSimpleCache($defaultLifetime = 0)
{
- $client = AbstractAdapter::createConnection('memcached://'.getenv('MEMCACHED_HOST'), array('binary_protocol' => false));
+ $client = AbstractAdapter::createConnection('memcached://'.getenv('MEMCACHED_HOST'), ['binary_protocol' => false]);
return new MemcachedCache($client, str_replace('\\', '.', __CLASS__), $defaultLifetime);
}
diff --git a/Tests/Simple/NullCacheTest.php b/Tests/Simple/NullCacheTest.php
index 7b760fd3..cf0dde92 100644
--- a/Tests/Simple/NullCacheTest.php
+++ b/Tests/Simple/NullCacheTest.php
@@ -16,6 +16,7 @@
/**
* @group time-sensitive
+ * @group legacy
*/
class NullCacheTest extends TestCase
{
@@ -40,7 +41,7 @@ public function testGetMultiple()
{
$cache = $this->createCachePool();
- $keys = array('foo', 'bar', 'baz', 'biz');
+ $keys = ['foo', 'bar', 'baz', 'biz'];
$default = new \stdClass();
$items = $cache->getMultiple($keys, $default);
@@ -75,7 +76,7 @@ public function testDelete()
public function testDeleteMultiple()
{
- $this->assertTrue($this->createCachePool()->deleteMultiple(array('key', 'foo', 'bar')));
+ $this->assertTrue($this->createCachePool()->deleteMultiple(['key', 'foo', 'bar']));
}
public function testSet()
@@ -90,7 +91,7 @@ public function testSetMultiple()
{
$cache = $this->createCachePool();
- $this->assertFalse($cache->setMultiple(array('key' => 'val')));
+ $this->assertFalse($cache->setMultiple(['key' => 'val']));
$this->assertNull($cache->get('key'));
}
}
diff --git a/Tests/Simple/PdoCacheTest.php b/Tests/Simple/PdoCacheTest.php
index 665db09f..c326d387 100644
--- a/Tests/Simple/PdoCacheTest.php
+++ b/Tests/Simple/PdoCacheTest.php
@@ -16,6 +16,7 @@
/**
* @group time-sensitive
+ * @group legacy
*/
class PdoCacheTest extends CacheTestCase
{
@@ -23,7 +24,7 @@ class PdoCacheTest extends CacheTestCase
protected static $dbFile;
- public static function setupBeforeClass()
+ public static function setUpBeforeClass(): void
{
if (!\extension_loaded('pdo_sqlite')) {
self::markTestSkipped('Extension pdo_sqlite required.');
@@ -35,7 +36,7 @@ public static function setupBeforeClass()
$pool->createTable();
}
- public static function tearDownAfterClass()
+ public static function tearDownAfterClass(): void
{
@unlink(self::$dbFile);
}
diff --git a/Tests/Simple/PdoDbalCacheTest.php b/Tests/Simple/PdoDbalCacheTest.php
index 158e2c89..2893fee9 100644
--- a/Tests/Simple/PdoDbalCacheTest.php
+++ b/Tests/Simple/PdoDbalCacheTest.php
@@ -17,6 +17,7 @@
/**
* @group time-sensitive
+ * @group legacy
*/
class PdoDbalCacheTest extends CacheTestCase
{
@@ -24,7 +25,7 @@ class PdoDbalCacheTest extends CacheTestCase
protected static $dbFile;
- public static function setupBeforeClass()
+ public static function setUpBeforeClass(): void
{
if (!\extension_loaded('pdo_sqlite')) {
self::markTestSkipped('Extension pdo_sqlite required.');
@@ -32,17 +33,17 @@ public static function setupBeforeClass()
self::$dbFile = tempnam(sys_get_temp_dir(), 'sf_sqlite_cache');
- $pool = new PdoCache(DriverManager::getConnection(array('driver' => 'pdo_sqlite', 'path' => self::$dbFile)));
+ $pool = new PdoCache(DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile]));
$pool->createTable();
}
- public static function tearDownAfterClass()
+ public static function tearDownAfterClass(): void
{
@unlink(self::$dbFile);
}
public function createSimpleCache($defaultLifetime = 0)
{
- return new PdoCache(DriverManager::getConnection(array('driver' => 'pdo_sqlite', 'path' => self::$dbFile)), '', $defaultLifetime);
+ return new PdoCache(DriverManager::getConnection(['driver' => 'pdo_sqlite', 'path' => self::$dbFile]), '', $defaultLifetime);
}
}
diff --git a/Tests/Simple/PhpArrayCacheTest.php b/Tests/Simple/PhpArrayCacheTest.php
index b4862c61..c1d4ab2d 100644
--- a/Tests/Simple/PhpArrayCacheTest.php
+++ b/Tests/Simple/PhpArrayCacheTest.php
@@ -17,10 +17,11 @@
/**
* @group time-sensitive
+ * @group legacy
*/
class PhpArrayCacheTest extends CacheTestCase
{
- protected $skippedTests = array(
+ protected $skippedTests = [
'testBasicUsageWithLongKey' => 'PhpArrayCache does no writes',
'testDelete' => 'PhpArrayCache does no writes',
@@ -45,16 +46,16 @@ class PhpArrayCacheTest extends CacheTestCase
'testDefaultLifeTime' => 'PhpArrayCache does not allow configuring a default lifetime.',
'testPrune' => 'PhpArrayCache just proxies',
- );
+ ];
protected static $file;
- public static function setupBeforeClass()
+ public static function setUpBeforeClass(): void
{
self::$file = sys_get_temp_dir().'/symfony-cache/php-array-adapter-test.php';
}
- protected function tearDown()
+ protected function tearDown(): void
{
if (file_exists(sys_get_temp_dir().'/symfony-cache')) {
FilesystemAdapterTest::rmdir(sys_get_temp_dir().'/symfony-cache');
@@ -68,22 +69,22 @@ public function createSimpleCache()
public function testStore()
{
- $arrayWithRefs = array();
+ $arrayWithRefs = [];
$arrayWithRefs[0] = 123;
$arrayWithRefs[1] = &$arrayWithRefs[0];
- $object = (object) array(
+ $object = (object) [
'foo' => 'bar',
'foo2' => 'bar2',
- );
+ ];
- $expected = array(
+ $expected = [
'null' => null,
'serializedString' => serialize($object),
'arrayWithRefs' => $arrayWithRefs,
'object' => $object,
- 'arrayWithObject' => array('bar' => $object),
- );
+ 'arrayWithObject' => ['bar' => $object],
+ ];
$cache = new PhpArrayCache(self::$file, new NullCache());
$cache->warmUp($expected);
@@ -95,49 +96,35 @@ public function testStore()
public function testStoredFile()
{
- $expected = array(
+ $data = [
'integer' => 42,
'float' => 42.42,
'boolean' => true,
- 'array_simple' => array('foo', 'bar'),
- 'array_associative' => array('foo' => 'bar', 'foo2' => 'bar2'),
- );
+ 'array_simple' => ['foo', 'bar'],
+ 'array_associative' => ['foo' => 'bar', 'foo2' => 'bar2'],
+ ];
+ $expected = [
+ [
+ 'integer' => 0,
+ 'float' => 1,
+ 'boolean' => 2,
+ 'array_simple' => 3,
+ 'array_associative' => 4,
+ ],
+ [
+ 0 => 42,
+ 1 => 42.42,
+ 2 => true,
+ 3 => ['foo', 'bar'],
+ 4 => ['foo' => 'bar', 'foo2' => 'bar2'],
+ ],
+ ];
$cache = new PhpArrayCache(self::$file, new NullCache());
- $cache->warmUp($expected);
+ $cache->warmUp($data);
$values = eval(substr(file_get_contents(self::$file), 6));
$this->assertSame($expected, $values, 'Warm up should create a PHP file that OPCache can load in memory');
}
}
-
-class PhpArrayCacheWrapper extends PhpArrayCache
-{
- public function set($key, $value, $ttl = null)
- {
- \call_user_func(\Closure::bind(function () use ($key, $value) {
- $this->values[$key] = $value;
- $this->warmUp($this->values);
- $this->values = eval(substr(file_get_contents($this->file), 6));
- }, $this, PhpArrayCache::class));
-
- return true;
- }
-
- public function setMultiple($values, $ttl = null)
- {
- if (!\is_array($values) && !$values instanceof \Traversable) {
- return parent::setMultiple($values, $ttl);
- }
- \call_user_func(\Closure::bind(function () use ($values) {
- foreach ($values as $key => $value) {
- $this->values[$key] = $value;
- }
- $this->warmUp($this->values);
- $this->values = eval(substr(file_get_contents($this->file), 6));
- }, $this, PhpArrayCache::class));
-
- return true;
- }
-}
diff --git a/Tests/Simple/PhpArrayCacheWithFallbackTest.php b/Tests/Simple/PhpArrayCacheWithFallbackTest.php
index 4b6a94f7..7ae814a9 100644
--- a/Tests/Simple/PhpArrayCacheWithFallbackTest.php
+++ b/Tests/Simple/PhpArrayCacheWithFallbackTest.php
@@ -17,10 +17,11 @@
/**
* @group time-sensitive
+ * @group legacy
*/
class PhpArrayCacheWithFallbackTest extends CacheTestCase
{
- protected $skippedTests = array(
+ protected $skippedTests = [
'testGetInvalidKeys' => 'PhpArrayCache does no validation',
'testGetMultipleInvalidKeys' => 'PhpArrayCache does no validation',
'testDeleteInvalidKeys' => 'PhpArrayCache does no validation',
@@ -32,16 +33,16 @@ class PhpArrayCacheWithFallbackTest extends CacheTestCase
'testSetMultipleInvalidTtl' => 'PhpArrayCache does no validation',
'testHasInvalidKeys' => 'PhpArrayCache does no validation',
'testPrune' => 'PhpArrayCache just proxies',
- );
+ ];
protected static $file;
- public static function setupBeforeClass()
+ public static function setUpBeforeClass(): void
{
self::$file = sys_get_temp_dir().'/symfony-cache/php-array-adapter-test.php';
}
- protected function tearDown()
+ protected function tearDown(): void
{
if (file_exists(sys_get_temp_dir().'/symfony-cache')) {
FilesystemAdapterTest::rmdir(sys_get_temp_dir().'/symfony-cache');
diff --git a/Tests/Simple/PhpArrayCacheWrapper.php b/Tests/Simple/PhpArrayCacheWrapper.php
new file mode 100644
index 00000000..1e102fe1
--- /dev/null
+++ b/Tests/Simple/PhpArrayCacheWrapper.php
@@ -0,0 +1,46 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\Tests\Simple;
+
+use Symfony\Component\Cache\Simple\PhpArrayCache;
+
+class PhpArrayCacheWrapper extends PhpArrayCache
+{
+ protected $data = [];
+
+ public function set($key, $value, $ttl = null)
+ {
+ (\Closure::bind(function () use ($key, $value) {
+ $this->data[$key] = $value;
+ $this->warmUp($this->data);
+ list($this->keys, $this->values) = eval(substr(file_get_contents($this->file), 6));
+ }, $this, PhpArrayCache::class))();
+
+ return true;
+ }
+
+ public function setMultiple($values, $ttl = null)
+ {
+ if (!\is_array($values) && !$values instanceof \Traversable) {
+ return parent::setMultiple($values, $ttl);
+ }
+ (\Closure::bind(function () use ($values) {
+ foreach ($values as $key => $value) {
+ $this->data[$key] = $value;
+ }
+ $this->warmUp($this->data);
+ list($this->keys, $this->values) = eval(substr(file_get_contents($this->file), 6));
+ }, $this, PhpArrayCache::class))();
+
+ return true;
+ }
+}
diff --git a/Tests/Simple/PhpFilesCacheTest.php b/Tests/Simple/PhpFilesCacheTest.php
index 7a402682..7e40df7d 100644
--- a/Tests/Simple/PhpFilesCacheTest.php
+++ b/Tests/Simple/PhpFilesCacheTest.php
@@ -16,19 +16,16 @@
/**
* @group time-sensitive
+ * @group legacy
*/
class PhpFilesCacheTest extends CacheTestCase
{
- protected $skippedTests = array(
+ protected $skippedTests = [
'testDefaultLifeTime' => 'PhpFilesCache does not allow configuring a default lifetime.',
- );
+ ];
public function createSimpleCache()
{
- if (!PhpFilesCache::isSupported()) {
- $this->markTestSkipped('OPcache extension is not enabled.');
- }
-
return new PhpFilesCache('sf-cache');
}
diff --git a/Tests/Simple/Psr6CacheTest.php b/Tests/Simple/Psr6CacheTest.php
index 78582894..9fff36e4 100644
--- a/Tests/Simple/Psr6CacheTest.php
+++ b/Tests/Simple/Psr6CacheTest.php
@@ -11,20 +11,21 @@
namespace Symfony\Component\Cache\Tests\Simple;
-use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\Cache\Simple\Psr6Cache;
/**
- * @group time-sensitive
+ * @group legacy
*/
-class Psr6CacheTest extends CacheTestCase
+abstract class Psr6CacheTest extends CacheTestCase
{
- protected $skippedTests = array(
+ protected $skippedTests = [
'testPrune' => 'Psr6Cache just proxies',
- );
+ ];
public function createSimpleCache($defaultLifetime = 0)
{
- return new Psr6Cache(new FilesystemAdapter('', $defaultLifetime));
+ return new Psr6Cache($this->createCacheItemPool($defaultLifetime));
}
+
+ abstract protected function createCacheItemPool($defaultLifetime = 0);
}
diff --git a/Tests/Simple/Psr6CacheWithAdapterTest.php b/Tests/Simple/Psr6CacheWithAdapterTest.php
new file mode 100644
index 00000000..e5c7a6a4
--- /dev/null
+++ b/Tests/Simple/Psr6CacheWithAdapterTest.php
@@ -0,0 +1,26 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\Tests\Simple;
+
+use Symfony\Component\Cache\Adapter\FilesystemAdapter;
+
+/**
+ * @group time-sensitive
+ * @group legacy
+ */
+class Psr6CacheWithAdapterTest extends Psr6CacheTest
+{
+ protected function createCacheItemPool($defaultLifetime = 0)
+ {
+ return new FilesystemAdapter('', $defaultLifetime);
+ }
+}
diff --git a/Tests/Simple/Psr6CacheWithoutAdapterTest.php b/Tests/Simple/Psr6CacheWithoutAdapterTest.php
new file mode 100644
index 00000000..f987d405
--- /dev/null
+++ b/Tests/Simple/Psr6CacheWithoutAdapterTest.php
@@ -0,0 +1,26 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\Tests\Simple;
+
+use Symfony\Component\Cache\Tests\Fixtures\ExternalAdapter;
+
+/**
+ * @group time-sensitive
+ * @group legacy
+ */
+class Psr6CacheWithoutAdapterTest extends Psr6CacheTest
+{
+ protected function createCacheItemPool($defaultLifetime = 0)
+ {
+ return new ExternalAdapter($defaultLifetime);
+ }
+}
diff --git a/Tests/Simple/RedisArrayCacheTest.php b/Tests/Simple/RedisArrayCacheTest.php
index 3c903c8a..834b6206 100644
--- a/Tests/Simple/RedisArrayCacheTest.php
+++ b/Tests/Simple/RedisArrayCacheTest.php
@@ -11,14 +11,17 @@
namespace Symfony\Component\Cache\Tests\Simple;
+/**
+ * @group legacy
+ */
class RedisArrayCacheTest extends AbstractRedisCacheTest
{
- public static function setupBeforeClass()
+ public static function setUpBeforeClass(): void
{
parent::setupBeforeClass();
if (!class_exists('RedisArray')) {
self::markTestSkipped('The RedisArray class is required.');
}
- self::$redis = new \RedisArray(array(getenv('REDIS_HOST')), array('lazy_connect' => true));
+ self::$redis = new \RedisArray([getenv('REDIS_HOST')], ['lazy_connect' => true]);
}
}
diff --git a/Tests/Simple/RedisCacheTest.php b/Tests/Simple/RedisCacheTest.php
index d33421f9..b5792f39 100644
--- a/Tests/Simple/RedisCacheTest.php
+++ b/Tests/Simple/RedisCacheTest.php
@@ -13,9 +13,12 @@
use Symfony\Component\Cache\Simple\RedisCache;
+/**
+ * @group legacy
+ */
class RedisCacheTest extends AbstractRedisCacheTest
{
- public static function setupBeforeClass()
+ public static function setUpBeforeClass(): void
{
parent::setupBeforeClass();
self::$redis = RedisCache::createConnection('redis://'.getenv('REDIS_HOST'));
@@ -33,50 +36,50 @@ public function testCreateConnection()
$redis = RedisCache::createConnection('redis://'.$redisHost.'/2');
$this->assertSame(2, $redis->getDbNum());
- $redis = RedisCache::createConnection('redis://'.$redisHost, array('timeout' => 3));
+ $redis = RedisCache::createConnection('redis://'.$redisHost, ['timeout' => 3]);
$this->assertEquals(3, $redis->getTimeout());
$redis = RedisCache::createConnection('redis://'.$redisHost.'?timeout=4');
$this->assertEquals(4, $redis->getTimeout());
- $redis = RedisCache::createConnection('redis://'.$redisHost, array('read_timeout' => 5));
+ $redis = RedisCache::createConnection('redis://'.$redisHost, ['read_timeout' => 5]);
$this->assertEquals(5, $redis->getReadTimeout());
}
/**
* @dataProvider provideFailedCreateConnection
- * @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
- * @expectedExceptionMessage Redis connection failed
*/
public function testFailedCreateConnection($dsn)
{
+ $this->expectException('Symfony\Component\Cache\Exception\InvalidArgumentException');
+ $this->expectExceptionMessage('Redis connection failed');
RedisCache::createConnection($dsn);
}
public function provideFailedCreateConnection()
{
- return array(
- array('redis://localhost:1234'),
- array('redis://foo@localhost'),
- array('redis://localhost/123'),
- );
+ return [
+ ['redis://localhost:1234'],
+ ['redis://foo@localhost'],
+ ['redis://localhost/123'],
+ ];
}
/**
* @dataProvider provideInvalidCreateConnection
- * @expectedException \Symfony\Component\Cache\Exception\InvalidArgumentException
- * @expectedExceptionMessage Invalid Redis DSN
*/
public function testInvalidCreateConnection($dsn)
{
+ $this->expectException('Symfony\Component\Cache\Exception\InvalidArgumentException');
+ $this->expectExceptionMessage('Invalid Redis DSN');
RedisCache::createConnection($dsn);
}
public function provideInvalidCreateConnection()
{
- return array(
- array('foo://localhost'),
- array('redis://'),
- );
+ return [
+ ['foo://localhost'],
+ ['redis://'],
+ ];
}
}
diff --git a/Tests/Simple/RedisClusterCacheTest.php b/Tests/Simple/RedisClusterCacheTest.php
index 99d4e518..c5115c7c 100644
--- a/Tests/Simple/RedisClusterCacheTest.php
+++ b/Tests/Simple/RedisClusterCacheTest.php
@@ -11,9 +11,12 @@
namespace Symfony\Component\Cache\Tests\Simple;
+/**
+ * @group legacy
+ */
class RedisClusterCacheTest extends AbstractRedisCacheTest
{
- public static function setupBeforeClass()
+ public static function setUpBeforeClass(): void
{
if (!class_exists('RedisCluster')) {
self::markTestSkipped('The RedisCluster class is required.');
diff --git a/Tests/Simple/TraceableCacheTest.php b/Tests/Simple/TraceableCacheTest.php
index 535f93da..c2e8a477 100644
--- a/Tests/Simple/TraceableCacheTest.php
+++ b/Tests/Simple/TraceableCacheTest.php
@@ -16,12 +16,13 @@
/**
* @group time-sensitive
+ * @group legacy
*/
class TraceableCacheTest extends CacheTestCase
{
- protected $skippedTests = array(
+ protected $skippedTests = [
'testPrune' => 'TraceableCache just proxies',
- );
+ ];
public function createSimpleCache($defaultLifetime = 0)
{
@@ -37,7 +38,7 @@ public function testGetMissTrace()
$call = $calls[0];
$this->assertSame('get', $call->name);
- $this->assertSame(array('k' => false), $call->result);
+ $this->assertSame(['k' => false], $call->result);
$this->assertSame(0, $call->hits);
$this->assertSame(1, $call->misses);
$this->assertNotEmpty($call->start);
@@ -61,7 +62,7 @@ public function testGetMultipleMissTrace()
{
$pool = $this->createSimpleCache();
$pool->set('k1', 123);
- $values = $pool->getMultiple(array('k0', 'k1'));
+ $values = $pool->getMultiple(['k0', 'k1']);
foreach ($values as $value) {
}
$calls = $pool->getCalls();
@@ -69,7 +70,7 @@ public function testGetMultipleMissTrace()
$call = $calls[1];
$this->assertSame('getMultiple', $call->name);
- $this->assertSame(array('k1' => true, 'k0' => false), $call->result);
+ $this->assertSame(['k1' => true, 'k0' => false], $call->result);
$this->assertSame(1, $call->misses);
$this->assertNotEmpty($call->start);
$this->assertNotEmpty($call->end);
@@ -84,7 +85,7 @@ public function testHasMissTrace()
$call = $calls[0];
$this->assertSame('has', $call->name);
- $this->assertSame(array('k' => false), $call->result);
+ $this->assertSame(['k' => false], $call->result);
$this->assertNotEmpty($call->start);
$this->assertNotEmpty($call->end);
}
@@ -99,7 +100,7 @@ public function testHasHitTrace()
$call = $calls[1];
$this->assertSame('has', $call->name);
- $this->assertSame(array('k' => true), $call->result);
+ $this->assertSame(['k' => true], $call->result);
$this->assertNotEmpty($call->start);
$this->assertNotEmpty($call->end);
}
@@ -113,7 +114,7 @@ public function testDeleteTrace()
$call = $calls[0];
$this->assertSame('delete', $call->name);
- $this->assertSame(array('k' => true), $call->result);
+ $this->assertSame(['k' => true], $call->result);
$this->assertSame(0, $call->hits);
$this->assertSame(0, $call->misses);
$this->assertNotEmpty($call->start);
@@ -123,14 +124,14 @@ public function testDeleteTrace()
public function testDeleteMultipleTrace()
{
$pool = $this->createSimpleCache();
- $arg = array('k0', 'k1');
+ $arg = ['k0', 'k1'];
$pool->deleteMultiple($arg);
$calls = $pool->getCalls();
$this->assertCount(1, $calls);
$call = $calls[0];
$this->assertSame('deleteMultiple', $call->name);
- $this->assertSame(array('keys' => $arg, 'result' => true), $call->result);
+ $this->assertSame(['keys' => $arg, 'result' => true], $call->result);
$this->assertSame(0, $call->hits);
$this->assertSame(0, $call->misses);
$this->assertNotEmpty($call->start);
@@ -146,7 +147,7 @@ public function testTraceSetTrace()
$call = $calls[0];
$this->assertSame('set', $call->name);
- $this->assertSame(array('k' => true), $call->result);
+ $this->assertSame(['k' => true], $call->result);
$this->assertSame(0, $call->hits);
$this->assertSame(0, $call->misses);
$this->assertNotEmpty($call->start);
@@ -156,13 +157,13 @@ public function testTraceSetTrace()
public function testSetMultipleTrace()
{
$pool = $this->createSimpleCache();
- $pool->setMultiple(array('k' => 'foo'));
+ $pool->setMultiple(['k' => 'foo']);
$calls = $pool->getCalls();
$this->assertCount(1, $calls);
$call = $calls[0];
$this->assertSame('setMultiple', $call->name);
- $this->assertSame(array('keys' => array('k'), 'result' => true), $call->result);
+ $this->assertSame(['keys' => ['k'], 'result' => true], $call->result);
$this->assertSame(0, $call->hits);
$this->assertSame(0, $call->misses);
$this->assertNotEmpty($call->start);
diff --git a/Tests/Traits/TagAwareTestTrait.php b/Tests/Traits/TagAwareTestTrait.php
new file mode 100644
index 00000000..9c6ce395
--- /dev/null
+++ b/Tests/Traits/TagAwareTestTrait.php
@@ -0,0 +1,158 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\Tests\Traits;
+
+use Symfony\Component\Cache\CacheItem;
+
+/**
+ * Common assertions for TagAware adapters.
+ *
+ * @method \Symfony\Component\Cache\Adapter\TagAwareAdapterInterface createCachePool() Must be implemented by TestCase
+ */
+trait TagAwareTestTrait
+{
+ public function testInvalidTag()
+ {
+ $this->expectException('Psr\Cache\InvalidArgumentException');
+ $pool = $this->createCachePool();
+ $item = $pool->getItem('foo');
+ $item->tag(':');
+ }
+
+ public function testInvalidateTags()
+ {
+ $pool = $this->createCachePool();
+
+ $i0 = $pool->getItem('i0');
+ $i1 = $pool->getItem('i1');
+ $i2 = $pool->getItem('i2');
+ $i3 = $pool->getItem('i3');
+ $foo = $pool->getItem('foo');
+
+ $pool->save($i0->tag('bar'));
+ $pool->save($i1->tag('foo'));
+ $pool->save($i2->tag('foo')->tag('bar'));
+ $pool->save($i3->tag('foo')->tag('baz'));
+ $pool->save($foo);
+
+ $pool->invalidateTags(['bar']);
+
+ $this->assertFalse($pool->getItem('i0')->isHit());
+ $this->assertTrue($pool->getItem('i1')->isHit());
+ $this->assertFalse($pool->getItem('i2')->isHit());
+ $this->assertTrue($pool->getItem('i3')->isHit());
+ $this->assertTrue($pool->getItem('foo')->isHit());
+
+ $pool->invalidateTags(['foo']);
+
+ $this->assertFalse($pool->getItem('i1')->isHit());
+ $this->assertFalse($pool->getItem('i3')->isHit());
+ $this->assertTrue($pool->getItem('foo')->isHit());
+
+ $anotherPoolInstance = $this->createCachePool();
+
+ $this->assertFalse($anotherPoolInstance->getItem('i1')->isHit());
+ $this->assertFalse($anotherPoolInstance->getItem('i3')->isHit());
+ $this->assertTrue($anotherPoolInstance->getItem('foo')->isHit());
+ }
+
+ public function testInvalidateCommits()
+ {
+ $pool = $this->createCachePool();
+
+ $foo = $pool->getItem('foo');
+ $foo->tag('tag');
+
+ $pool->saveDeferred($foo->set('foo'));
+ $pool->invalidateTags(['tag']);
+
+ // ??: This seems to contradict a bit logic in deleteItems, where it does unset($this->deferred[$key]); on key matches
+
+ $foo = $pool->getItem('foo');
+
+ $this->assertTrue($foo->isHit());
+ }
+
+ public function testTagsAreCleanedOnSave()
+ {
+ $pool = $this->createCachePool();
+
+ $i = $pool->getItem('k');
+ $pool->save($i->tag('foo'));
+
+ $i = $pool->getItem('k');
+ $pool->save($i->tag('bar'));
+
+ $pool->invalidateTags(['foo']);
+ $this->assertTrue($pool->getItem('k')->isHit());
+ }
+
+ public function testTagsAreCleanedOnDelete()
+ {
+ $pool = $this->createCachePool();
+
+ $i = $pool->getItem('k');
+ $pool->save($i->tag('foo'));
+ $pool->deleteItem('k');
+
+ $pool->save($pool->getItem('k'));
+ $pool->invalidateTags(['foo']);
+
+ $this->assertTrue($pool->getItem('k')->isHit());
+ }
+
+ public function testTagItemExpiry()
+ {
+ if (isset($this->skippedTests[__FUNCTION__])) {
+ $this->markTestSkipped($this->skippedTests[__FUNCTION__]);
+ }
+
+ $pool = $this->createCachePool(10);
+
+ $item = $pool->getItem('foo');
+ $item->tag(['baz']);
+ $item->expiresAfter(100);
+
+ $pool->save($item);
+ $pool->invalidateTags(['baz']);
+ $this->assertFalse($pool->getItem('foo')->isHit());
+
+ sleep(20);
+
+ $this->assertFalse($pool->getItem('foo')->isHit());
+ }
+
+ /**
+ * @group legacy
+ */
+ public function testGetPreviousTags()
+ {
+ $pool = $this->createCachePool();
+
+ $i = $pool->getItem('k');
+ $pool->save($i->tag('foo'));
+
+ $i = $pool->getItem('k');
+ $this->assertSame(['foo' => 'foo'], $i->getPreviousTags());
+ }
+
+ public function testGetMetadata()
+ {
+ $pool = $this->createCachePool();
+
+ $i = $pool->getItem('k');
+ $pool->save($i->tag('foo'));
+
+ $i = $pool->getItem('k');
+ $this->assertSame(['foo' => 'foo'], $i->getMetadata()[CacheItem::METADATA_TAGS]);
+ }
+}
diff --git a/Traits/AbstractAdapterTrait.php b/Traits/AbstractAdapterTrait.php
new file mode 100644
index 00000000..445f8658
--- /dev/null
+++ b/Traits/AbstractAdapterTrait.php
@@ -0,0 +1,149 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\Traits;
+
+use Psr\Cache\CacheItemInterface;
+use Symfony\Component\Cache\CacheItem;
+
+/**
+ * @author Nicolas Grekas
+ *
+ * @internal
+ */
+trait AbstractAdapterTrait
+{
+ use AbstractTrait;
+
+ /**
+ * @var \Closure needs to be set by class, signature is function(string
+ *
+ * @internal
+ */
+trait ContractsTrait
+{
+ use CacheTrait {
+ doGet as private contractsGet;
+ }
+
+ private $callbackWrapper = [LockRegistry::class, 'compute'];
+ private $computing = [];
+
+ /**
+ * Wraps the callback passed to ->get() in a callable.
+ *
+ * @return callable the previous callback wrapper
+ */
+ public function setCallbackWrapper(?callable $callbackWrapper): callable
+ {
+ $previousWrapper = $this->callbackWrapper;
+ $this->callbackWrapper = $callbackWrapper ?? function (callable $callback, ItemInterface $item, bool &$save, CacheInterface $pool, \Closure $setMetadata, ?LoggerInterface $logger) {
+ return $callback($item, $save);
+ };
+
+ return $previousWrapper;
+ }
+
+ private function doGet(AdapterInterface $pool, string $key, callable $callback, ?float $beta, array &$metadata = null)
+ {
+ if (0 > $beta = $beta ?? 1.0) {
+ throw new InvalidArgumentException(sprintf('Argument "$beta" provided to "%s::get()" must be a positive number, %f given.', \get_class($this), $beta));
+ }
+
+ static $setMetadata;
+
+ $setMetadata = $setMetadata ?? \Closure::bind(
+ static function (CacheItem $item, float $startTime, ?array &$metadata) {
+ if ($item->expiry > $endTime = microtime(true)) {
+ $item->newMetadata[CacheItem::METADATA_EXPIRY] = $metadata[CacheItem::METADATA_EXPIRY] = $item->expiry;
+ $item->newMetadata[CacheItem::METADATA_CTIME] = $metadata[CacheItem::METADATA_CTIME] = (int) ceil(1000 * ($endTime - $startTime));
+ } else {
+ unset($metadata[CacheItem::METADATA_EXPIRY], $metadata[CacheItem::METADATA_CTIME]);
+ }
+ },
+ null,
+ CacheItem::class
+ );
+
+ return $this->contractsGet($pool, $key, function (CacheItem $item, bool &$save) use ($pool, $callback, $setMetadata, &$metadata, $key) {
+ // don't wrap nor save recursive calls
+ if (isset($this->computing[$key])) {
+ $value = $callback($item, $save);
+ $save = false;
+
+ return $value;
+ }
+
+ $this->computing[$key] = $key;
+ $startTime = microtime(true);
+
+ try {
+ $value = ($this->callbackWrapper)($callback, $item, $save, $pool, function (CacheItem $item) use ($setMetadata, $startTime, &$metadata) {
+ $setMetadata($item, $startTime, $metadata);
+ }, $this->logger ?? null);
+ $setMetadata($item, $startTime, $metadata);
+
+ return $value;
+ } finally {
+ unset($this->computing[$key]);
+ }
+ }, $beta, $metadata, $this->logger ?? null);
+ }
+}
diff --git a/Traits/FilesystemCommonTrait.php b/Traits/FilesystemCommonTrait.php
index 274eb732..37e1fd1f 100644
--- a/Traits/FilesystemCommonTrait.php
+++ b/Traits/FilesystemCommonTrait.php
@@ -56,7 +56,7 @@ protected function doClear($namespace)
$ok = true;
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->directory, \FilesystemIterator::SKIP_DOTS)) as $file) {
- $ok = ($file->isDir() || @unlink($file) || !file_exists($file)) && $ok;
+ $ok = ($file->isDir() || $this->doUnlink($file) || !file_exists($file)) && $ok;
}
return $ok;
@@ -71,12 +71,17 @@ protected function doDelete(array $ids)
foreach ($ids as $id) {
$file = $this->getFile($id);
- $ok = (!file_exists($file) || @unlink($file) || !file_exists($file)) && $ok;
+ $ok = (!file_exists($file) || $this->doUnlink($file) || !file_exists($file)) && $ok;
}
return $ok;
}
+ protected function doUnlink($file)
+ {
+ return @unlink($file);
+ }
+
private function write($file, $data, $expiresAt = null)
{
set_error_handler(__CLASS__.'::throwError');
@@ -96,10 +101,11 @@ private function write($file, $data, $expiresAt = null)
}
}
- private function getFile($id, $mkdir = false)
+ private function getFile($id, $mkdir = false, string $directory = null)
{
- $hash = str_replace('/', '-', base64_encode(hash('sha256', static::class.$id, true)));
- $dir = $this->directory.strtoupper($hash[0].\DIRECTORY_SEPARATOR.$hash[1].\DIRECTORY_SEPARATOR);
+ // Use MD5 to favor speed over security, which is not an issue here
+ $hash = str_replace('/', '-', base64_encode(hash('md5', static::class.$id, true)));
+ $dir = ($directory ?? $this->directory).strtoupper($hash[0].\DIRECTORY_SEPARATOR.$hash[1].\DIRECTORY_SEPARATOR);
if ($mkdir && !file_exists($dir)) {
@mkdir($dir, 0777, true);
@@ -116,6 +122,16 @@ public static function throwError($type, $message, $file, $line)
throw new \ErrorException($message, 0, $type, $file, $line);
}
+ public function __sleep()
+ {
+ throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
+ }
+
+ public function __wakeup()
+ {
+ throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
+ }
+
public function __destruct()
{
if (method_exists(parent::class, '__destruct')) {
diff --git a/Traits/FilesystemTrait.php b/Traits/FilesystemTrait.php
index 23974b3b..9c444f9b 100644
--- a/Traits/FilesystemTrait.php
+++ b/Traits/FilesystemTrait.php
@@ -23,6 +23,8 @@ trait FilesystemTrait
{
use FilesystemCommonTrait;
+ private $marshaller;
+
/**
* @return bool
*/
@@ -52,7 +54,7 @@ public function prune()
*/
protected function doFetch(array $ids)
{
- $values = array();
+ $values = [];
$now = time();
foreach ($ids as $id) {
@@ -68,7 +70,7 @@ protected function doFetch(array $ids)
$value = stream_get_contents($h);
fclose($h);
if ($i === $id) {
- $values[$id] = parent::unserialize($value);
+ $values[$id] = $this->marshaller->unmarshall($value);
}
}
}
@@ -83,7 +85,7 @@ protected function doHave($id)
{
$file = $this->getFile($id);
- return file_exists($file) && (@filemtime($file) > time() || $this->doFetch(array($id)));
+ return file_exists($file) && (@filemtime($file) > time() || $this->doFetch([$id]));
}
/**
@@ -91,17 +93,19 @@ protected function doHave($id)
*/
protected function doSave(array $values, $lifetime)
{
- $ok = true;
$expiresAt = $lifetime ? (time() + $lifetime) : 0;
+ $values = $this->marshaller->marshall($values, $failed);
foreach ($values as $id => $value) {
- $ok = $this->write($this->getFile($id, true), $expiresAt."\n".rawurlencode($id)."\n".serialize($value), $expiresAt) && $ok;
+ if (!$this->write($this->getFile($id, true), $expiresAt."\n".rawurlencode($id)."\n".$value, $expiresAt)) {
+ $failed[] = $id;
+ }
}
- if (!$ok && !is_writable($this->directory)) {
+ if ($failed && !is_writable($this->directory)) {
throw new CacheException(sprintf('Cache directory is not writable (%s)', $this->directory));
}
- return $ok;
+ return $failed;
}
}
diff --git a/Traits/MemcachedTrait.php b/Traits/MemcachedTrait.php
index 5983d9eb..ccfc66b4 100644
--- a/Traits/MemcachedTrait.php
+++ b/Traits/MemcachedTrait.php
@@ -13,6 +13,8 @@
use Symfony\Component\Cache\Exception\CacheException;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
+use Symfony\Component\Cache\Marshaller\DefaultMarshaller;
+use Symfony\Component\Cache\Marshaller\MarshallerInterface;
/**
* @author Rob Frawley 2nd
@@ -36,7 +36,7 @@ public function prune()
*/
public function reset()
{
- if ($this->pool instanceof ResettableInterface) {
+ if ($this->pool instanceof ResetInterface) {
$this->pool->reset();
}
}
diff --git a/Traits/RedisClusterProxy.php b/Traits/RedisClusterProxy.php
new file mode 100644
index 00000000..b4cef59a
--- /dev/null
+++ b/Traits/RedisClusterProxy.php
@@ -0,0 +1,63 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Cache\Traits;
+
+/**
+ * @author Alessandro Chitolina Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages. Alternative Proxies:Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.