Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ea0647b

Browse files
[Cache] Add namespace handling to all adapters
1 parent b868feb commit ea0647b

File tree

4 files changed

+79
-9
lines changed

4 files changed

+79
-9
lines changed

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

Lines 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

Lines 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

Lines 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 <[email protected]>
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
}
Lines 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 <[email protected]>
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)