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

Skip to content

Commit d5b8458

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

File tree

4 files changed

+71
-9
lines changed

4 files changed

+71
-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: 36 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
},
@@ -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,13 @@ public function getMisses()
170191
{
171192
return $this->misses;
172193
}
194+
195+
private function getId($key)
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+
201+
return $this->namespace.$key;
202+
}
173203
}
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)