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

Skip to content
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
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
language: php

php:
- 5.6
- 5.5
- 5.4
- 7.1
- 7.0

before_script:
- wget http://getcomposer.org/composer.phar
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}
],
"require": {
"php": ">=5.4",
"php": ">=7.0",
"m6web/redis-component": "^3.3.0",
"doctrine/cache": "~1.3"
},
Expand All @@ -18,7 +18,8 @@
"m6web/redis-mock": "@stable",
"symfony/symfony": "~2.3|~3.0",
"m6web/coke" : "~1.2",
"m6web/symfony2-coding-standard" : "~1.1"
"m6web/symfony2-coding-standard" : "~1.1",
"psr/cache": "~1.0"
},
"suggest": {
"m6web/wsclient-bundle": "@stable",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
<?php
namespace M6Web\Bundle\RedisBundle\CacheAdapters;

use M6Web\Bundle\RedisBundle\Redis\Redis as BaseRedis;

use Symfony\Component\Cache\CacheItem;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Cache\CacheItemInterface;

/**
* Class RedisCacheItemPoolAdapter
*/
class RedisCacheItemPoolAdapter extends BaseRedis implements CacheItemPoolInterface
{
/**
* @var \Closure
*/
private $createCacheItem;

/**
* @var \Closure
*/
private $getItemLifeTime;

/**
* @var bool
*/
protected $transactionInProgress = false;

public function __construct($redis, $defaultLifetime = 0)
{
$this->createCacheItem = \Closure::bind(
function ($key, $value, $isHit, $cacheExpire) use ($defaultLifetime) {
$cacheItem = new CacheItem();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why don't we subclass CacheItem, instead of using closure binding to access protected properties?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

because we can't set the key

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

$key is a protected property, so it seems to be designed to be subclassed:

class MyCacheItem extends CacheItem
{
   public function __construct($key, $value, $isHit, $cacheExpire, $defaultLifetime) {
    // Content of your anonymous function
  }

  public function getLifetime() {
    return $this->expiry;
  }
}

Then, instead of doing

($this->createCacheItem)($key, $cacheValue, $isHit, $cacheExpire);
($this->getItemLifeTime)($item);

You can use

$item = new MyCacheItem($key, $value, $isHit, $cacheExpire, $defaultLifetime);
$item->getLifetime();

Did I miss something? Seems more clear for me in this way

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

CacheItem is a final class

Copy link
Copy Markdown

@b-viguier b-viguier Jul 28, 2017

Choose a reason for hiding this comment

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

$cacheItem->key = $key;
$cacheItem->value = $value;
$cacheItem->isHit = $isHit;
$cacheItem->defaultLifetime = $defaultLifetime;

if (is_int($cacheExpire) && $cacheExpire > time()) {
$cacheItem->expiry = $cacheExpire;
}

return $cacheItem;
},
null,
CacheItem::class
);

$this->getItemLifeTime = \Closure::bind(
function (CacheItem $item) {

return $item->expiry;
},
null,
CacheItem::class
);

return parent::__construct($redis);
}

/**
* @param string $key
*
* @return CacheItem
*/
public function getItem($key)
{
$cacheValue = $this->get($key);
$cacheExpire = $this->ttl($key);
$isHit = ($cacheValue !== false) ? 1 : 0;

$cacheValue = is_string($cacheValue) ? unserialize($cacheValue) : null;

return ($this->createCacheItem)($key, $cacheValue, $isHit, $cacheExpire);
}

/**
* @param array $keys
*
* @return array
*/
public function getItems(array $keys = array())
{
$items = new \ArrayIterator();
foreach ($keys as $key) {
$items->append($this->getItem($key));
}

return $items;
}

/**
* @param string $key
*
* @return bool
*/
public function hasItem($key)
{
return $this->has($key);
}

/**
* @return bool
*/
public function clear()
{
$this->redis->discard();
$this->endTransaction();

// return true, we don't have the result from BaseRedis method
return true;
}

/**
* @param string $key
*
* @return int
*/
public function deleteItem($key)
{
return $this->remove($key);
}

/**
* @param array $keys
*
* @return bool
*/
public function deleteItems(array $keys)
{
array_walk($keys, [$this, 'remove']);

return true;
}

/**
* @param CacheItemInterface $item
*
* @return bool
*/
public function save(CacheItemInterface $item)
{
return $this->set($item->getKey(), serialize($item->get()), ($this->getItemLifeTime)($item));
}

/**
* @param CacheItemInterface $item
*
* @return bool
*/
public function saveDeferred(CacheItemInterface $item)
{
$this->startTransaction();
$this->save($item);

// return true, we don't have the result from BaseRedis method
return true;
}

/**
* @return bool
*/
public function commit()
{
$this->redis->exec();
$this->endTransaction();

// return true, we don't have the result from BaseRedis method
return true;
}

protected function startTransaction()
{
if (!$this->transactionInProgress) {
$this->transactionInProgress = true;
$this->redis->multi();
}
}

protected function endTransaction()
{
$this->transactionInProgress = false;
}
}
34 changes: 34 additions & 0 deletions src/M6Web/Bundle/RedisBundle/Tests/Units/AbstractTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
namespace M6Web\Bundle\RedisBundle\Tests\Units;

use M6Web\Component\RedisMock\RedisMockFactory;
use mageekguy\atoum;

class AbstractTest extends atoum\test
{
static protected $params = array(
'namespace' => '__tt____',
'timeout' => 2,
'compress' => true,
'server_config' => array(
'local' => array(
'ip' => 'localhost',
'port' => 6379,
)
)
);

/**
* get a redis Instance
*
* @return BaseRedis
*/
protected function getRedisMock()
{
$factory = new RedisMockFactory();
$myRedisMockClass = $factory->getAdapterClass('M6Web\Component\Redis\Cache', true, true);
$myRedisMock = new $myRedisMockClass(static::$params, true);

return $myRedisMock;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
namespace M6Web\Bundle\RedisBundle\Tests\Units\CacheAdapters;

use M6Web\Bundle\RedisBundle\Tests\Units\AbstractTest;

/**
* Class RedisCacheItemPoolAdapter
*/
class RedisCacheItemPoolAdapter extends AbstractTest
{
public function testCreateItem()
{
$this
->given(
$this->newTestedInstance($this->getRedisMock()),
$item = $this->testedInstance->getItem('myKey')
)
->then
->object($item)
->isInstanceof('Psr\Cache\CacheItemInterface')
->variable($item->get())
->isNull()
->given(
$item->set('myValue'),
$item->expiresAt(new \DateTime('+1 second')),
$this->testedInstance->save($item)
)
->then
->boolean($this->testedInstance->hasItem('myKey'))
->isTrue()
->object($item)
->isInstanceof('Psr\Cache\CacheItemInterface')
->string($item->get())
->isEqualTo('myValue')
->if($this->testedInstance->deleteItem('myKey'))
->then
->boolean($this->testedInstance->hasItem('myKey'))
->isFalse()
;
}
}
23 changes: 3 additions & 20 deletions src/M6Web/Bundle/RedisBundle/Tests/Units/Redis/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

require_once __DIR__.'/../../../../../../../vendor/autoload.php';

use mageekguy\atoum;
use M6Web\Bundle\RedisBundle\Tests\Units\AbstractTest;
use M6Web\Component\Redis\Cache;
use M6Web\Bundle\RedisBundle\Redis\Redis as BaseRedis;
use M6Web\Component\RedisMock\RedisMockFactory;
Expand All @@ -12,33 +12,16 @@
* Test compute last modified date
*
*/
class Redis extends atoum\test
class Redis extends AbstractTest
{
static protected $params = array(
'namespace' => '__tt____',
'timeout' => 2,
'compress' => true,
'server_config' => array(
'local' => array(
'ip' => 'localhost',
'port' => 6379,
)
)
);

/**
* get a redis Instance
*
* @return BaseRedis
*/
protected function getRedisInstance()
{
$factory = new RedisMockFactory();
$myRedisMockClass = $factory->getAdapterClass('M6Web\Component\Redis\Cache', true, true);
$myRedisMock = new $myRedisMockClass(static::$params, true);
$redis = new BaseRedis($myRedisMock);

return $redis;
return new BaseRedis($this->getRedisMock());
}

/**
Expand Down