Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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

Merged
merged 1 commit into from
May 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 121 additions & 0 deletions 121 src/Symfony/Component/Cache/Adapter/NullAdapter.php
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

phpdoc missing

Copy link
Member

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)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for your feedback 👍


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
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.");
Copy link
Contributor

@OskarStark OskarStark May 24, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the symfony standard, but i would recommend to use ' instead of "

Copy link
Member

@nicolas-grekas nicolas-grekas May 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not mandatory: we add double quotes when we need special chars like \n, but this case looks good to me also, more readable

Copy link
Contributor

Choose a reason for hiding this comment

The 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());
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.