Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Cache] Add namespace handling to all adapters #18024

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 10, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Cache] Add namespace handling to all adapters
  • Loading branch information
nicolas-grekas committed Mar 10, 2016
commit 5068f8751a52ef9d0ce53a7eab21f8bf9a57b940
6 changes: 3 additions & 3 deletions 6 src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ abstract class AbstractAdapter implements CacheItemPoolInterface, LoggerAwareInt

protected function __construct($namespace = '', $defaultLifetime = 0)
{
$this->namespace = $namespace;
$this->namespace = $this->getId($namespace, true);
$this->createCacheItem = \Closure::bind(
function ($key, $value, $isHit) use ($defaultLifetime) {
$item = new CacheItem();
Expand Down Expand Up @@ -331,12 +331,12 @@ public function __destruct()
}
}

private function getId($key)
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])) {
if (!isset($key[0]) && !$ns) {
throw new InvalidArgumentException('Cache key length must be greater than zero');
}
if (isset($key[strcspn($key, '{}()/\@:')])) {
Expand Down
4 changes: 2 additions & 2 deletions 4 src/Symfony/Component/Cache/Adapter/DoctrineAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ class DoctrineAdapter extends AbstractAdapter
{
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;
}

Expand Down
8 changes: 7 additions & 1 deletion 8 src/Symfony/Component/Cache/Adapter/FilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ class FilesystemAdapter extends AbstractAdapter
{
private $directory;

public function __construct($directory, $defaultLifetime = null)
public function __construct($directory, $defaultLifetime = 0, $namespace = '')
{
parent::__construct('', $defaultLifetime);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't the $namespace be also passed as the first argument to the parent call?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no because we handle it differently here: we create a subdir to isolate "namespaces" so there is no need to isolate using key prefixing by the abstract adapter

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


if (!isset($directory[0])) {
$directory = sys_get_temp_dir().'/symfony-cache';
}
if (isset($namespace[0])) {
$directory .= '/'.$namespace;
}
if (!file_exists($dir = $directory.'/.')) {
@mkdir($directory, 0777, true);
}
Expand Down
48 changes: 42 additions & 6 deletions 48 src/Symfony/Component/Cache/Adapter/ProxyAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,32 @@
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\Exception\InvalidArgumentException;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class ProxyAdapter implements CacheItemPoolInterface
{
private $pool;
private $namespace;
private $namespaceLen;
private $createCacheItem;
private $hits = 0;
private $misses = 0;

public function __construct(CacheItemPoolInterface $pool)
public function __construct(CacheItemPoolInterface $pool, $defaultLifetime = 0, $namespace = '')
{
$this->pool = $pool;
$this->namespace = $this->getId($namespace, true);
$this->namespaceLen = strlen($namespace);
$this->createCacheItem = \Closure::bind(
function ($key, $value, $isHit) {
function ($key, $value, $isHit) use ($defaultLifetime) {
$item = new CacheItem();
$item->key = $key;
$item->value = $value;
$item->isHit = $isHit;
$item->defaultLifetime = $defaultLifetime;

return $item;
},
Expand All @@ -48,7 +54,7 @@ function ($key, $value, $isHit) {
public function getItem($key)
{
$f = $this->createCacheItem;
$item = $this->pool->getItem($key);
$item = $this->pool->getItem($this->getId($key));
if ($isHit = $item->isHit()) {
++$this->hits;
} else {
Expand All @@ -63,6 +69,12 @@ public function getItem($key)
*/
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));
}

Expand All @@ -71,7 +83,7 @@ public function getItems(array $keys = array())
*/
public function hasItem($key)
{
return $this->pool->hasItem($key);
return $this->pool->hasItem($this->getId($key));
}

/**
Expand All @@ -87,14 +99,20 @@ public function clear()
*/
public function deleteItem($key)
{
return $this->pool->deleteItem($key);
return $this->pool->deleteItem($this->getId($key));
}

/**
* {@inheritdoc}
*/
public function deleteItems(array $keys)
{
if ($this->namespaceLen) {
foreach ($keys as $i => $key) {
$keys[$i] = $this->getId($key);
}
}

return $this->pool->deleteItems($keys);
}

Expand Down Expand Up @@ -129,7 +147,7 @@ private function doSave(CacheItemInterface $item, $method)
}
$item = (array) $item;
$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->expiresAt(null !== $expiry ? \DateTime::createFromFormat('U', $expiry) : null);

Expand All @@ -146,6 +164,9 @@ private function generateItems($items)
} else {
++$this->misses;
}
if ($this->namespaceLen) {
$key = substr($key, $this->namespaceLen);
}

yield $key => $f($key, $item->get(), $isHit);
}
Expand All @@ -170,4 +191,19 @@ public function getMisses()
{
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public function createCachePool()
$this->markTestSkipped('Fails transiently on Windows.');
}

return new ApcuAdapter(__CLASS__);
return new ApcuAdapter(str_replace('\\', '.', __CLASS__));
}
}
Original file line number Diff line number Diff line change
@@ -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');
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.