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

Skip to content

Commit 4871079

Browse files
feature #18825 [Cache] Create NullAdapter to disable cache if needed (tgalopin)
This PR was merged into the 3.2-dev branch. Discussion ---------- [Cache] Create NullAdapter to disable cache if needed | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | WIP | Fixed tickets | - | License | MIT | Doc PR | - Tthis PR aims to implement a Symfony Cache Null adapter able to disable cache if needed (very useful in tests or in the case of the fallback of the OpCacheAdapter for instance). This PR is required by #18823 and #18533 Commits ------- 0519d22 [Cache] Create NullAdapter to disable cache if needed
2 parents 5042788 + 0519d22 commit 4871079

File tree

2 files changed

+245
-0
lines changed

2 files changed

+245
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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\Adapter;
13+
14+
use Psr\Cache\CacheItemInterface;
15+
use Symfony\Component\Cache\CacheItem;
16+
17+
/**
18+
* @author Titouan Galopin <[email protected]>
19+
*/
20+
class NullAdapter implements AdapterInterface
21+
{
22+
private $createCacheItem;
23+
24+
public function __construct()
25+
{
26+
$this->createCacheItem = \Closure::bind(
27+
function ($key) {
28+
$item = new CacheItem();
29+
$item->key = $key;
30+
$item->isHit = false;
31+
32+
return $item;
33+
},
34+
$this,
35+
CacheItem::class
36+
);
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function getItem($key)
43+
{
44+
$f = $this->createCacheItem;
45+
46+
return $f($key);
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function getItems(array $keys = array())
53+
{
54+
return $this->generateItems($keys);
55+
}
56+
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
public function hasItem($key)
61+
{
62+
return false;
63+
}
64+
65+
/**
66+
* {@inheritdoc}
67+
*/
68+
public function clear()
69+
{
70+
return true;
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
public function deleteItem($key)
77+
{
78+
return true;
79+
}
80+
81+
/**
82+
* {@inheritdoc}
83+
*/
84+
public function deleteItems(array $keys)
85+
{
86+
return true;
87+
}
88+
89+
/**
90+
* {@inheritdoc}
91+
*/
92+
public function save(CacheItemInterface $item)
93+
{
94+
return false;
95+
}
96+
97+
/**
98+
* {@inheritdoc}
99+
*/
100+
public function saveDeferred(CacheItemInterface $item)
101+
{
102+
return false;
103+
}
104+
105+
/**
106+
* {@inheritdoc}
107+
*/
108+
public function commit()
109+
{
110+
return false;
111+
}
112+
113+
private function generateItems(array $keys)
114+
{
115+
$f = $this->createCacheItem;
116+
117+
foreach ($keys as $key) {
118+
yield $key => $f($key);
119+
}
120+
}
121+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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 Psr\Cache\CacheItemInterface;
15+
use Symfony\Component\Cache\Adapter\NullAdapter;
16+
17+
class NullAdapterTest extends \PHPUnit_Framework_TestCase
18+
{
19+
public function createCachePool()
20+
{
21+
return new NullAdapter();
22+
}
23+
24+
public function testGetItem()
25+
{
26+
$adapter = $this->createCachePool();
27+
28+
$item = $adapter->getItem('key');
29+
$this->assertFalse($item->isHit());
30+
$this->assertNull($item->get(), "Item's value must be null when isHit is false.");
31+
}
32+
33+
public function testHasItem()
34+
{
35+
$this->assertFalse($this->createCachePool()->hasItem('key'));
36+
}
37+
38+
public function testGetItems()
39+
{
40+
$adapter = $this->createCachePool();
41+
42+
$keys = array('foo', 'bar', 'baz', 'biz');
43+
44+
/** @var CacheItemInterface[] $items */
45+
$items = $adapter->getItems($keys);
46+
$count = 0;
47+
48+
foreach ($items as $key => $item) {
49+
$itemKey = $item->getKey();
50+
51+
$this->assertEquals($itemKey, $key, 'Keys must be preserved when fetching multiple items');
52+
$this->assertTrue(in_array($key, $keys), 'Cache key can not change.');
53+
$this->assertFalse($item->isHit());
54+
55+
// Remove $key for $keys
56+
foreach ($keys as $k => $v) {
57+
if ($v === $key) {
58+
unset($keys[$k]);
59+
}
60+
}
61+
62+
++$count;
63+
}
64+
65+
$this->assertSame(4, $count);
66+
}
67+
68+
public function testIsHit()
69+
{
70+
$adapter = $this->createCachePool();
71+
72+
$item = $adapter->getItem('key');
73+
$this->assertFalse($item->isHit());
74+
}
75+
76+
public function testClear()
77+
{
78+
$this->assertTrue($this->createCachePool()->clear());
79+
}
80+
81+
public function testDeleteItem()
82+
{
83+
$this->assertTrue($this->createCachePool()->deleteItem('key'));
84+
}
85+
86+
public function testDeleteItems()
87+
{
88+
$this->assertTrue($this->createCachePool()->deleteItems(array('key', 'foo', 'bar')));
89+
}
90+
91+
public function testSave()
92+
{
93+
$adapter = $this->createCachePool();
94+
95+
$item = $adapter->getItem('key');
96+
$this->assertFalse($item->isHit());
97+
$this->assertNull($item->get(), "Item's value must be null when isHit is false.");
98+
99+
$this->assertFalse($adapter->save($item));
100+
}
101+
102+
public function testDeferredSave()
103+
{
104+
$adapter = $this->createCachePool();
105+
106+
$item = $adapter->getItem('key');
107+
$this->assertFalse($item->isHit());
108+
$this->assertNull($item->get(), "Item's value must be null when isHit is false.");
109+
110+
$this->assertFalse($adapter->saveDeferred($item));
111+
}
112+
113+
public function testCommit()
114+
{
115+
$adapter = $this->createCachePool();
116+
117+
$item = $adapter->getItem('key');
118+
$this->assertFalse($item->isHit());
119+
$this->assertNull($item->get(), "Item's value must be null when isHit is false.");
120+
121+
$this->assertFalse($adapter->saveDeferred($item));
122+
$this->assertFalse($this->createCachePool()->commit());
123+
}
124+
}

0 commit comments

Comments
 (0)