From 7a0773bd1d0a0e21febc83b1353c60fd11df464a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9l=C3=A9na=20Hiraux?= Date: Wed, 28 Jun 2017 18:05:04 +0200 Subject: [PATCH 1/2] add redis cache item pool adapter --- composer.json | 3 +- .../RedisCacheItemPoolAdapter.php | 185 ++++++++++++++++++ .../RedisBundle/Tests/Units/AbstractTest.php | 34 ++++ .../RedisCacheItemPoolAdapter.php | 41 ++++ .../RedisBundle/Tests/Units/Redis/Redis.php | 23 +-- 5 files changed, 265 insertions(+), 21 deletions(-) create mode 100644 src/M6Web/Bundle/RedisBundle/CacheAdapters/RedisCacheItemPoolAdapter.php create mode 100644 src/M6Web/Bundle/RedisBundle/Tests/Units/AbstractTest.php create mode 100644 src/M6Web/Bundle/RedisBundle/Tests/Units/CacheAdapters/RedisCacheItemPoolAdapter.php diff --git a/composer.json b/composer.json index ef68205..72b81f1 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/M6Web/Bundle/RedisBundle/CacheAdapters/RedisCacheItemPoolAdapter.php b/src/M6Web/Bundle/RedisBundle/CacheAdapters/RedisCacheItemPoolAdapter.php new file mode 100644 index 0000000..33e107d --- /dev/null +++ b/src/M6Web/Bundle/RedisBundle/CacheAdapters/RedisCacheItemPoolAdapter.php @@ -0,0 +1,185 @@ +createCacheItem = \Closure::bind( + function ($key, $value, $isHit, $cacheExpire) use ($defaultLifetime) { + $cacheItem = new CacheItem(); + $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; + } +} diff --git a/src/M6Web/Bundle/RedisBundle/Tests/Units/AbstractTest.php b/src/M6Web/Bundle/RedisBundle/Tests/Units/AbstractTest.php new file mode 100644 index 0000000..b0a4945 --- /dev/null +++ b/src/M6Web/Bundle/RedisBundle/Tests/Units/AbstractTest.php @@ -0,0 +1,34 @@ + '__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; + } +} diff --git a/src/M6Web/Bundle/RedisBundle/Tests/Units/CacheAdapters/RedisCacheItemPoolAdapter.php b/src/M6Web/Bundle/RedisBundle/Tests/Units/CacheAdapters/RedisCacheItemPoolAdapter.php new file mode 100644 index 0000000..1a7f29a --- /dev/null +++ b/src/M6Web/Bundle/RedisBundle/Tests/Units/CacheAdapters/RedisCacheItemPoolAdapter.php @@ -0,0 +1,41 @@ +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() + ; + } +} diff --git a/src/M6Web/Bundle/RedisBundle/Tests/Units/Redis/Redis.php b/src/M6Web/Bundle/RedisBundle/Tests/Units/Redis/Redis.php index 8557ed2..a5b991a 100644 --- a/src/M6Web/Bundle/RedisBundle/Tests/Units/Redis/Redis.php +++ b/src/M6Web/Bundle/RedisBundle/Tests/Units/Redis/Redis.php @@ -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; @@ -12,20 +12,8 @@ * 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 * @@ -33,12 +21,7 @@ class Redis extends atoum\test */ 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()); } /** From 1dae86cdcfdf26c071b8b27aa1c2ccafc1f16250 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9l=C3=A9na=20Hiraux?= Date: Fri, 21 Jul 2017 16:34:50 +0200 Subject: [PATCH 2/2] remove php 5 compatibility and add php 7 to travis --- .travis.yml | 5 ++--- composer.json | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2adcee1..ff4417e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/composer.json b/composer.json index 72b81f1..d67d24f 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ } ], "require": { - "php": ">=5.4", + "php": ">=7.0", "m6web/redis-component": "^3.3.0", "doctrine/cache": "~1.3" },