Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
2 changed files
with
245 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Cache\Adapter; | ||
|
||
use Psr\Cache\CacheItemInterface; | ||
use Symfony\Component\Cache\CacheItem; | ||
|
||
/** | ||
* @author Titouan Galopin <galopintitouan@gmail.com> | ||
*/ | ||
class NullAdapter implements AdapterInterface | ||
{ | ||
private $createCacheItem; | ||
|
||
public function __construct() | ||
{ | ||
$this->createCacheItem = \Closure::bind( | ||
function ($key) { | ||
$item = new CacheItem(); | ||
$item->key = $key; | ||
$item->isHit = false; | ||
|
||
return $item; | ||
}, | ||
$this, | ||
CacheItem::class | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getItem($key) | ||
{ | ||
$f = $this->createCacheItem; | ||
|
||
return $f($key); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getItems(array $keys = array()) | ||
{ | ||
return $this->generateItems($keys); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function hasItem($key) | ||
{ | ||
return false; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function clear() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function deleteItem($key) | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function deleteItems(array $keys) | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function save(CacheItemInterface $item) | ||
{ | ||
return false; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function saveDeferred(CacheItemInterface $item) | ||
{ | ||
return false; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function commit() | ||
{ | ||
return false; | ||
} | ||
|
||
private function generateItems(array $keys) | ||
{ | ||
$f = $this->createCacheItem; | ||
|
||
foreach ($keys as $key) { | ||
yield $key => $f($key); | ||
} | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
src/Symfony/Component/Cache/Tests/Adapter/NullAdapterTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Cache\Tests\Adapter; | ||
|
||
use Psr\Cache\CacheItemInterface; | ||
use Symfony\Component\Cache\Adapter\NullAdapter; | ||
|
||
class NullAdapterTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function createCachePool() | ||
{ | ||
return new NullAdapter(); | ||
} | ||
|
||
public function testGetItem() | ||
{ | ||
$adapter = $this->createCachePool(); | ||
|
||
$item = $adapter->getItem('key'); | ||
$this->assertFalse($item->isHit()); | ||
$this->assertNull($item->get(), "Item's value must be null when isHit is false."); | ||
} | ||
|
||
public function testHasItem() | ||
{ | ||
$this->assertFalse($this->createCachePool()->hasItem('key')); | ||
} | ||
|
||
public function testGetItems() | ||
{ | ||
$adapter = $this->createCachePool(); | ||
|
||
$keys = array('foo', 'bar', 'baz', 'biz'); | ||
|
||
/** @var CacheItemInterface[] $items */ | ||
$items = $adapter->getItems($keys); | ||
$count = 0; | ||
|
||
foreach ($items as $key => $item) { | ||
$itemKey = $item->getKey(); | ||
|
||
$this->assertEquals($itemKey, $key, 'Keys must be preserved when fetching multiple items'); | ||
$this->assertTrue(in_array($key, $keys), 'Cache key can not change.'); | ||
$this->assertFalse($item->isHit()); | ||
|
||
// Remove $key for $keys | ||
foreach ($keys as $k => $v) { | ||
if ($v === $key) { | ||
unset($keys[$k]); | ||
} | ||
} | ||
|
||
++$count; | ||
} | ||
|
||
$this->assertSame(4, $count); | ||
} | ||
|
||
public function testIsHit() | ||
{ | ||
$adapter = $this->createCachePool(); | ||
|
||
$item = $adapter->getItem('key'); | ||
$this->assertFalse($item->isHit()); | ||
} | ||
|
||
public function testClear() | ||
{ | ||
$this->assertTrue($this->createCachePool()->clear()); | ||
} | ||
|
||
public function testDeleteItem() | ||
{ | ||
$this->assertTrue($this->createCachePool()->deleteItem('key')); | ||
} | ||
|
||
public function testDeleteItems() | ||
{ | ||
$this->assertTrue($this->createCachePool()->deleteItems(array('key', 'foo', 'bar'))); | ||
} | ||
|
||
public function testSave() | ||
{ | ||
$adapter = $this->createCachePool(); | ||
|
||
$item = $adapter->getItem('key'); | ||
$this->assertFalse($item->isHit()); | ||
$this->assertNull($item->get(), "Item's value must be null when isHit is false."); | ||
|
||
$this->assertFalse($adapter->save($item)); | ||
} | ||
|
||
public function testDeferredSave() | ||
{ | ||
$adapter = $this->createCachePool(); | ||
|
||
$item = $adapter->getItem('key'); | ||
$this->assertFalse($item->isHit()); | ||
$this->assertNull($item->get(), "Item's value must be null when isHit is false."); | ||
|
||
$this->assertFalse($adapter->saveDeferred($item)); | ||
} | ||
|
||
public function testCommit() | ||
{ | ||
$adapter = $this->createCachePool(); | ||
|
||
$item = $adapter->getItem('key'); | ||
$this->assertFalse($item->isHit()); | ||
$this->assertNull($item->get(), "Item's value must be null when isHit is false."); | ||
|
||
$this->assertFalse($adapter->saveDeferred($item)); | ||
$this->assertFalse($this->createCachePool()->commit()); | ||
} | ||
} |