feature #18024 [Cache] Add namespace handling to all adapters (nicolas-grekas)
This PR was merged into the 3.1-dev branch.
Discussion
----------
[Cache] Add namespace handling to all adapters
| Q | A
| ------------- | ---
| Branch | master
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | -
| License | MIT
| Doc PR | -
This allows more flexible handling of adapters, allowing e.g. to take one cache pool and split in in several sub-pools by using ProxyAdapter.
Commits
-------
5068f87
[Cache] Add namespace handling to all adapters
This commit is contained in:
commit
0cd41d827e
@ -32,7 +32,7 @@ abstract class AbstractAdapter implements CacheItemPoolInterface, LoggerAwareInt
|
|||||||
|
|
||||||
protected function __construct($namespace = '', $defaultLifetime = 0)
|
protected function __construct($namespace = '', $defaultLifetime = 0)
|
||||||
{
|
{
|
||||||
$this->namespace = $namespace;
|
$this->namespace = $this->getId($namespace, true);
|
||||||
$this->createCacheItem = \Closure::bind(
|
$this->createCacheItem = \Closure::bind(
|
||||||
function ($key, $value, $isHit) use ($defaultLifetime) {
|
function ($key, $value, $isHit) use ($defaultLifetime) {
|
||||||
$item = new CacheItem();
|
$item = new CacheItem();
|
||||||
@ -331,12 +331,12 @@ abstract class AbstractAdapter implements CacheItemPoolInterface, LoggerAwareInt
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getId($key)
|
private function getId($key, $ns = false)
|
||||||
{
|
{
|
||||||
if (!is_string($key)) {
|
if (!is_string($key)) {
|
||||||
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', is_object($key) ? get_class($key) : gettype($key)));
|
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', is_object($key) ? get_class($key) : gettype($key)));
|
||||||
}
|
}
|
||||||
if (!isset($key[0])) {
|
if (!isset($key[0]) && !$ns) {
|
||||||
throw new InvalidArgumentException('Cache key length must be greater than zero');
|
throw new InvalidArgumentException('Cache key length must be greater than zero');
|
||||||
}
|
}
|
||||||
if (isset($key[strcspn($key, '{}()/\@:')])) {
|
if (isset($key[strcspn($key, '{}()/\@:')])) {
|
||||||
|
@ -20,9 +20,9 @@ class DoctrineAdapter extends AbstractAdapter
|
|||||||
{
|
{
|
||||||
private $provider;
|
private $provider;
|
||||||
|
|
||||||
public function __construct(CacheProvider $provider, $defaultLifetime = null)
|
public function __construct(CacheProvider $provider, $defaultLifetime = 0, $namespace = '')
|
||||||
{
|
{
|
||||||
parent::__construct('', $defaultLifetime);
|
parent::__construct($namespace, $defaultLifetime);
|
||||||
$this->provider = $provider;
|
$this->provider = $provider;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,10 +20,16 @@ class FilesystemAdapter extends AbstractAdapter
|
|||||||
{
|
{
|
||||||
private $directory;
|
private $directory;
|
||||||
|
|
||||||
public function __construct($directory, $defaultLifetime = null)
|
public function __construct($directory, $defaultLifetime = 0, $namespace = '')
|
||||||
{
|
{
|
||||||
parent::__construct('', $defaultLifetime);
|
parent::__construct('', $defaultLifetime);
|
||||||
|
|
||||||
|
if (!isset($directory[0])) {
|
||||||
|
$directory = sys_get_temp_dir().'/symfony-cache';
|
||||||
|
}
|
||||||
|
if (isset($namespace[0])) {
|
||||||
|
$directory .= '/'.$namespace;
|
||||||
|
}
|
||||||
if (!file_exists($dir = $directory.'/.')) {
|
if (!file_exists($dir = $directory.'/.')) {
|
||||||
@mkdir($directory, 0777, true);
|
@mkdir($directory, 0777, true);
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ namespace Symfony\Component\Cache\Adapter;
|
|||||||
use Psr\Cache\CacheItemInterface;
|
use Psr\Cache\CacheItemInterface;
|
||||||
use Psr\Cache\CacheItemPoolInterface;
|
use Psr\Cache\CacheItemPoolInterface;
|
||||||
use Symfony\Component\Cache\CacheItem;
|
use Symfony\Component\Cache\CacheItem;
|
||||||
|
use Symfony\Component\Cache\Exception\InvalidArgumentException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Nicolas Grekas <p@tchwork.com>
|
* @author Nicolas Grekas <p@tchwork.com>
|
||||||
@ -21,19 +22,24 @@ use Symfony\Component\Cache\CacheItem;
|
|||||||
class ProxyAdapter implements CacheItemPoolInterface
|
class ProxyAdapter implements CacheItemPoolInterface
|
||||||
{
|
{
|
||||||
private $pool;
|
private $pool;
|
||||||
|
private $namespace;
|
||||||
|
private $namespaceLen;
|
||||||
private $createCacheItem;
|
private $createCacheItem;
|
||||||
private $hits = 0;
|
private $hits = 0;
|
||||||
private $misses = 0;
|
private $misses = 0;
|
||||||
|
|
||||||
public function __construct(CacheItemPoolInterface $pool)
|
public function __construct(CacheItemPoolInterface $pool, $defaultLifetime = 0, $namespace = '')
|
||||||
{
|
{
|
||||||
$this->pool = $pool;
|
$this->pool = $pool;
|
||||||
|
$this->namespace = $this->getId($namespace, true);
|
||||||
|
$this->namespaceLen = strlen($namespace);
|
||||||
$this->createCacheItem = \Closure::bind(
|
$this->createCacheItem = \Closure::bind(
|
||||||
function ($key, $value, $isHit) {
|
function ($key, $value, $isHit) use ($defaultLifetime) {
|
||||||
$item = new CacheItem();
|
$item = new CacheItem();
|
||||||
$item->key = $key;
|
$item->key = $key;
|
||||||
$item->value = $value;
|
$item->value = $value;
|
||||||
$item->isHit = $isHit;
|
$item->isHit = $isHit;
|
||||||
|
$item->defaultLifetime = $defaultLifetime;
|
||||||
|
|
||||||
return $item;
|
return $item;
|
||||||
},
|
},
|
||||||
@ -48,7 +54,7 @@ class ProxyAdapter implements CacheItemPoolInterface
|
|||||||
public function getItem($key)
|
public function getItem($key)
|
||||||
{
|
{
|
||||||
$f = $this->createCacheItem;
|
$f = $this->createCacheItem;
|
||||||
$item = $this->pool->getItem($key);
|
$item = $this->pool->getItem($this->getId($key));
|
||||||
if ($isHit = $item->isHit()) {
|
if ($isHit = $item->isHit()) {
|
||||||
++$this->hits;
|
++$this->hits;
|
||||||
} else {
|
} else {
|
||||||
@ -63,6 +69,12 @@ class ProxyAdapter implements CacheItemPoolInterface
|
|||||||
*/
|
*/
|
||||||
public function getItems(array $keys = array())
|
public function getItems(array $keys = array())
|
||||||
{
|
{
|
||||||
|
if ($this->namespaceLen) {
|
||||||
|
foreach ($keys as $i => $key) {
|
||||||
|
$keys[$i] = $this->getId($key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $this->generateItems($this->pool->getItems($keys));
|
return $this->generateItems($this->pool->getItems($keys));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,7 +83,7 @@ class ProxyAdapter implements CacheItemPoolInterface
|
|||||||
*/
|
*/
|
||||||
public function hasItem($key)
|
public function hasItem($key)
|
||||||
{
|
{
|
||||||
return $this->pool->hasItem($key);
|
return $this->pool->hasItem($this->getId($key));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -87,7 +99,7 @@ class ProxyAdapter implements CacheItemPoolInterface
|
|||||||
*/
|
*/
|
||||||
public function deleteItem($key)
|
public function deleteItem($key)
|
||||||
{
|
{
|
||||||
return $this->pool->deleteItem($key);
|
return $this->pool->deleteItem($this->getId($key));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -95,6 +107,12 @@ class ProxyAdapter implements CacheItemPoolInterface
|
|||||||
*/
|
*/
|
||||||
public function deleteItems(array $keys)
|
public function deleteItems(array $keys)
|
||||||
{
|
{
|
||||||
|
if ($this->namespaceLen) {
|
||||||
|
foreach ($keys as $i => $key) {
|
||||||
|
$keys[$i] = $this->getId($key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $this->pool->deleteItems($keys);
|
return $this->pool->deleteItems($keys);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,7 +147,7 @@ class ProxyAdapter implements CacheItemPoolInterface
|
|||||||
}
|
}
|
||||||
$item = (array) $item;
|
$item = (array) $item;
|
||||||
$expiry = $item[CacheItem::CAST_PREFIX.'expiry'];
|
$expiry = $item[CacheItem::CAST_PREFIX.'expiry'];
|
||||||
$poolItem = $this->pool->getItem($item[CacheItem::CAST_PREFIX.'key']);
|
$poolItem = $this->pool->getItem($this->namespace.$item[CacheItem::CAST_PREFIX.'key']);
|
||||||
$poolItem->set($item[CacheItem::CAST_PREFIX.'value']);
|
$poolItem->set($item[CacheItem::CAST_PREFIX.'value']);
|
||||||
$poolItem->expiresAt(null !== $expiry ? \DateTime::createFromFormat('U', $expiry) : null);
|
$poolItem->expiresAt(null !== $expiry ? \DateTime::createFromFormat('U', $expiry) : null);
|
||||||
|
|
||||||
@ -146,6 +164,9 @@ class ProxyAdapter implements CacheItemPoolInterface
|
|||||||
} else {
|
} else {
|
||||||
++$this->misses;
|
++$this->misses;
|
||||||
}
|
}
|
||||||
|
if ($this->namespaceLen) {
|
||||||
|
$key = substr($key, $this->namespaceLen);
|
||||||
|
}
|
||||||
|
|
||||||
yield $key => $f($key, $item->get(), $isHit);
|
yield $key => $f($key, $item->get(), $isHit);
|
||||||
}
|
}
|
||||||
@ -170,4 +191,19 @@ class ProxyAdapter implements CacheItemPoolInterface
|
|||||||
{
|
{
|
||||||
return $this->misses;
|
return $this->misses;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getId($key, $ns = false)
|
||||||
|
{
|
||||||
|
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($key[0]) && !$ns) {
|
||||||
|
throw new InvalidArgumentException('Cache key length must be greater than zero');
|
||||||
|
}
|
||||||
|
if (isset($key[strcspn($key, '{}()/\@:')])) {
|
||||||
|
throw new InvalidArgumentException('Cache key contains reserved characters {}()/\@:');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->namespace.$key;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -28,6 +28,6 @@ class ApcuAdapterTest extends CachePoolTest
|
|||||||
$this->markTestSkipped('Fails transiently on Windows.');
|
$this->markTestSkipped('Fails transiently on Windows.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return new ApcuAdapter(__CLASS__);
|
return new ApcuAdapter(str_replace('\\', '.', __CLASS__));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Cache\Tests\Adapter;
|
||||||
|
|
||||||
|
use Symfony\Component\Cache\Adapter\ArrayAdapter;
|
||||||
|
use Symfony\Component\Cache\Adapter\ProxyAdapter;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group time-sensitive
|
||||||
|
*/
|
||||||
|
class NamespacedProxyAdapterTest extends ProxyAdapterTest
|
||||||
|
{
|
||||||
|
public function createCachePool()
|
||||||
|
{
|
||||||
|
return new ProxyAdapter(new ArrayAdapter(), 0, 'foo');
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user