-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Cache] Create NullAdapter to disable cache if needed #18825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} | ||
} | ||
} |
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."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure about the symfony standard, but i would recommend to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not mandatory: we add double quotes when we need special chars like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thank you for the feedback 👍 |
||
|
||
$this->assertFalse($adapter->saveDeferred($item)); | ||
$this->assertFalse($this->createCachePool()->commit()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
phpdoc missing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't add phpdoc on private properties when IDEs are able to infer the type from the constructor (which is the case here)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for your feedback 👍