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 b15bd78

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

File tree

4 files changed

+80
-9
lines changed
Filter options

4 files changed

+80
-9
lines changed

‎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
+44-6Lines changed: 44 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 = $namespace;
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
},
@@ -47,8 +53,11 @@ function ($key, $value, $isHit) {
4753
*/
4854
public function getItem($key)
4955
{
56+
if (!is_string($key)) {
57+
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', is_object($key) ? get_class($key) : gettype($key)));
58+
}
5059
$f = $this->createCacheItem;
51-
$item = $this->pool->getItem($key);
60+
$item = $this->pool->getItem($this->namespace.$key);
5261
if ($isHit = $item->isHit()) {
5362
++$this->hits;
5463
} else {
@@ -63,6 +72,15 @@ public function getItem($key)
6372
*/
6473
public function getItems(array $keys = array())
6574
{
75+
if ($this->namespaceLen) {
76+
foreach ($keys as $i => $key) {
77+
if (!is_string($key)) {
78+
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', is_object($key) ? get_class($key) : gettype($key)));
79+
}
80+
$keys[$i] = $this->namespace.$key;
81+
}
82+
}
83+
6684
return $this->generateItems($this->pool->getItems($keys));
6785
}
6886

@@ -71,7 +89,11 @@ public function getItems(array $keys = array())
7189
*/
7290
public function hasItem($key)
7391
{
74-
return $this->pool->hasItem($key);
92+
if (!is_string($key)) {
93+
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', is_object($key) ? get_class($key) : gettype($key)));
94+
}
95+
96+
return $this->pool->hasItem($this->namespace.$key);
7597
}
7698

7799
/**
@@ -87,14 +109,27 @@ public function clear()
87109
*/
88110
public function deleteItem($key)
89111
{
90-
return $this->pool->deleteItem($key);
112+
if (!is_string($key)) {
113+
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', is_object($key) ? get_class($key) : gettype($key)));
114+
}
115+
116+
return $this->pool->deleteItem($this->namespace.$key);
91117
}
92118

93119
/**
94120
* {@inheritdoc}
95121
*/
96122
public function deleteItems(array $keys)
97123
{
124+
if ($this->namespaceLen) {
125+
foreach ($keys as $i => $key) {
126+
if (!is_string($key)) {
127+
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', is_object($key) ? get_class($key) : gettype($key)));
128+
}
129+
$keys[$i] = $this->namespace.$key;
130+
}
131+
}
132+
98133
return $this->pool->deleteItems($keys);
99134
}
100135

@@ -129,7 +164,7 @@ private function doSave(CacheItemInterface $item, $method)
129164
}
130165
$item = (array) $item;
131166
$expiry = $item[CacheItem::CAST_PREFIX.'expiry'];
132-
$poolItem = $this->pool->getItem($item[CacheItem::CAST_PREFIX.'key']);
167+
$poolItem = $this->pool->getItem($this->namespace.$item[CacheItem::CAST_PREFIX.'key']);
133168
$poolItem->set($item[CacheItem::CAST_PREFIX.'value']);
134169
$poolItem->expiresAt(null !== $expiry ? \DateTime::createFromFormat('U', $expiry) : null);
135170

@@ -146,6 +181,9 @@ private function generateItems($items)
146181
} else {
147182
++$this->misses;
148183
}
184+
if ($this->namespaceLen) {
185+
$key = substr($key, $this->namespaceLen);
186+
}
149187

150188
yield $key => $f($key, $item->get(), $isHit);
151189
}
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 Cache\IntegrationTests\CachePoolTest;
15+
use Symfony\Component\Cache\Adapter\ArrayAdapter;
16+
use Symfony\Component\Cache\Adapter\ProxyAdapter;
17+
18+
/**
19+
* @group time-sensitive
20+
*/
21+
class NamespacedProxyAdapterTest extends ProxyAdapterTest
22+
{
23+
public function createCachePool()
24+
{
25+
return new ProxyAdapter(new ArrayAdapter(), 0, 'foo');
26+
}
27+
}

0 commit comments

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