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

Commit d0530e8

Browse filesBrowse files
[Cache] Add namespace handling to all adapters
1 parent b868feb commit d0530e8
Copy full SHA for d0530e8

File tree

5 files changed

+80
-12
lines changed
Filter options

5 files changed

+80
-12
lines changed

‎src/Symfony/Component/Cache/Adapter/AbstractAdapter.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Adapter/AbstractAdapter.php
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ abstract class AbstractAdapter implements CacheItemPoolInterface, LoggerAwareInt
3232

3333
protected function __construct($namespace = '', $defaultLifetime = 0)
3434
{
35-
$this->namespace = $namespace;
35+
$this->namespace = $this->getId($namespace, true);
3636
$this->createCacheItem = \Closure::bind(
3737
function ($key, $value, $isHit) use ($defaultLifetime) {
3838
$item = new CacheItem();
@@ -331,12 +331,12 @@ public function __destruct()
331331
}
332332
}
333333

334-
private function getId($key)
334+
private function getId($key, $ns = false)
335335
{
336336
if (!is_string($key)) {
337337
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', is_object($key) ? get_class($key) : gettype($key)));
338338
}
339-
if (!isset($key[0])) {
339+
if (!isset($key[0]) && !$ns) {
340340
throw new InvalidArgumentException('Cache key length must be greater than zero');
341341
}
342342
if (isset($key[strcspn($key, '{}()/\@:')])) {

‎src/Symfony/Component/Cache/Adapter/DoctrineAdapter.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Adapter/DoctrineAdapter.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class DoctrineAdapter extends AbstractAdapter
2020
{
2121
private $provider;
2222

23-
public function __construct(CacheProvider $provider, $defaultLifetime = null)
23+
public function __construct(CacheProvider $provider, $defaultLifetime = 0, $namespace = '')
2424
{
25-
parent::__construct('', $defaultLifetime);
25+
parent::__construct($namespace, $defaultLifetime);
2626
$this->provider = $provider;
2727
}
2828

‎src/Symfony/Component/Cache/Adapter/FilesystemAdapter.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Adapter/FilesystemAdapter.php
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@ class FilesystemAdapter extends AbstractAdapter
2020
{
2121
private $directory;
2222

23-
public function __construct($directory, $defaultLifetime = null)
23+
public function __construct($directory, $defaultLifetime = 0, $namespace = '')
2424
{
2525
parent::__construct('', $defaultLifetime);
2626

27+
if (!isset($directory[0])) {
28+
$directory = sys_get_temp_dir().'/symfony-cache';
29+
}
30+
if (isset($namespace[0])) {
31+
$directory .= '/'.$namespace;
32+
}
2733
if (!file_exists($dir = $directory.'/.')) {
2834
@mkdir($directory, 0777, true);
2935
}

‎src/Symfony/Component/Cache/Adapter/ProxyAdapter.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Cache/Adapter/ProxyAdapter.php
+42-6Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,32 @@
1414
use Psr\Cache\CacheItemInterface;
1515
use Psr\Cache\CacheItemPoolInterface;
1616
use Symfony\Component\Cache\CacheItem;
17+
use Symfony\Component\Cache\Exception\InvalidArgumentException;
1718

1819
/**
1920
* @author Nicolas Grekas <p@tchwork.com>
2021
*/
2122
class ProxyAdapter implements CacheItemPoolInterface
2223
{
2324
private $pool;
25+
private $namespace;
26+
private $namespaceLen;
2427
private $createCacheItem;
2528
private $hits = 0;
2629
private $misses = 0;
2730

28-
public function __construct(CacheItemPoolInterface $pool)
31+
public function __construct(CacheItemPoolInterface $pool, $defaultLifetime = 0, $namespace = '')
2932
{
3033
$this->pool = $pool;
34+
$this->namespace = $this->getId($namespace, true);
35+
$this->namespaceLen = strlen($namespace);
3136
$this->createCacheItem = \Closure::bind(
32-
function ($key, $value, $isHit) {
37+
function ($key, $value, $isHit) use ($defaultLifetime) {
3338
$item = new CacheItem();
3439
$item->key = $key;
3540
$item->value = $value;
3641
$item->isHit = $isHit;
42+
$item->defaultLifetime = $defaultLifetime;
3743

3844
return $item;
3945
},
@@ -48,7 +54,7 @@ function ($key, $value, $isHit) {
4854
public function getItem($key)
4955
{
5056
$f = $this->createCacheItem;
51-
$item = $this->pool->getItem($key);
57+
$item = $this->pool->getItem($this->getId($key));
5258
if ($isHit = $item->isHit()) {
5359
++$this->hits;
5460
} else {
@@ -63,6 +69,12 @@ public function getItem($key)
6369
*/
6470
public function getItems(array $keys = array())
6571
{
72+
if ($this->namespaceLen) {
73+
foreach ($keys as $i => $key) {
74+
$keys[$i] = $this->getId($key);
75+
}
76+
}
77+
6678
return $this->generateItems($this->pool->getItems($keys));
6779
}
6880

@@ -71,7 +83,7 @@ public function getItems(array $keys = array())
7183
*/
7284
public function hasItem($key)
7385
{
74-
return $this->pool->hasItem($key);
86+
return $this->pool->hasItem($this->getId($key));
7587
}
7688

7789
/**
@@ -87,14 +99,20 @@ public function clear()
8799
*/
88100
public function deleteItem($key)
89101
{
90-
return $this->pool->deleteItem($key);
102+
return $this->pool->deleteItem($this->getId($key));
91103
}
92104

93105
/**
94106
* {@inheritdoc}
95107
*/
96108
public function deleteItems(array $keys)
97109
{
110+
if ($this->namespaceLen) {
111+
foreach ($keys as $i => $key) {
112+
$keys[$i] = $this->getId($key);
113+
}
114+
}
115+
98116
return $this->pool->deleteItems($keys);
99117
}
100118

@@ -129,7 +147,7 @@ private function doSave(CacheItemInterface $item, $method)
129147
}
130148
$item = (array) $item;
131149
$expiry = $item[CacheItem::CAST_PREFIX.'expiry'];
132-
$poolItem = $this->pool->getItem($item[CacheItem::CAST_PREFIX.'key']);
150+
$poolItem = $this->pool->getItem($this->namespace.$item[CacheItem::CAST_PREFIX.'key']);
133151
$poolItem->set($item[CacheItem::CAST_PREFIX.'value']);
134152
$poolItem->expiresAt(null !== $expiry ? \DateTime::createFromFormat('U', $expiry) : null);
135153

@@ -146,6 +164,9 @@ private function generateItems($items)
146164
} else {
147165
++$this->misses;
148166
}
167+
if ($this->namespaceLen) {
168+
$key = substr($key, $this->namespaceLen);
169+
}
149170

150171
yield $key => $f($key, $item->get(), $isHit);
151172
}
@@ -170,4 +191,19 @@ public function getMisses()
170191
{
171192
return $this->misses;
172193
}
194+
195+
private function getId($key, $ns = false)
196+
{
197+
if (!is_string($key)) {
198+
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', is_object($key) ? get_class($key) : gettype($key)));
199+
}
200+
if (!isset($key[0]) && !$ns) {
201+
throw new InvalidArgumentException('Cache key length must be greater than zero');
202+
}
203+
if (isset($key[strcspn($key, '{}()/\@:')])) {
204+
throw new InvalidArgumentException('Cache key contains reserved characters {}()/\@:');
205+
}
206+
207+
return $this->namespace.$key;
208+
}
173209
}
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Cache\Tests\Adapter;
13+
14+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
15+
use Symfony\Component\Cache\Adapter\ProxyAdapter;
16+
17+
/**
18+
* @group time-sensitive
19+
*/
20+
class NamespacedProxyAdapterTest extends ProxyAdapterTest
21+
{
22+
public function createCachePool()
23+
{
24+
return new ProxyAdapter(new ArrayAdapter(), 0, 'foo');
25+
}
26+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.