From a4894e84975b9d93115db145321021481a0435d0 Mon Sep 17 00:00:00 2001 From: Nicolas Le Goff Date: Wed, 8 Feb 2012 11:00:08 +0100 Subject: [PATCH] add support for redis cache --- lib/Alchemy/Phrasea/Cache/RedisCache.php | 97 +++++++++++++++++++ .../Alchemy/Phrasea/Cache/RedisCacheTest.php | 66 +++++++++++++ 2 files changed, 163 insertions(+) create mode 100644 lib/Alchemy/Phrasea/Cache/RedisCache.php create mode 100644 lib/unitTest/Alchemy/Phrasea/Cache/RedisCacheTest.php diff --git a/lib/Alchemy/Phrasea/Cache/RedisCache.php b/lib/Alchemy/Phrasea/Cache/RedisCache.php new file mode 100644 index 0000000000..704991a69f --- /dev/null +++ b/lib/Alchemy/Phrasea/Cache/RedisCache.php @@ -0,0 +1,97 @@ +_redis = $redis; + } + + /** + * Gets the memcache instance used by the cache. + * + * @return Memcache + */ + public function getRedis() + { + return $this->_redis; + } + + /** + * {@inheritdoc} + */ + public function getIds() + { + return $this->_redis->keys('*'); + } + + /** + * {@inheritdoc} + */ + protected function _doFetch($id) + { + return $this->_redis->get($id); + } + + /** + * {@inheritdoc} + */ + protected function _doContains($id) + { + return (bool) $this->_redis->get($id); + } + + /** + * {@inheritdoc} + */ + protected function _doSave($id, $data, $lifeTime = 0) + { + if (0 === $lifeTime) + { + return $this->_redis->set($id, $data); + } + else + { + return $this->_redis->setex($id, $lifeTime, $data); + } + } + + /** + * {@inheritdoc} + */ + protected function _doDelete($id) + { + return $this->_redis->delete($id); + } + +} diff --git a/lib/unitTest/Alchemy/Phrasea/Cache/RedisCacheTest.php b/lib/unitTest/Alchemy/Phrasea/Cache/RedisCacheTest.php new file mode 100644 index 0000000000..016bc8060e --- /dev/null +++ b/lib/unitTest/Alchemy/Phrasea/Cache/RedisCacheTest.php @@ -0,0 +1,66 @@ +connect('127.0.0.1', 6379); + if (!$ok) + { + $this->markTestSkipped('The ' . __CLASS__ . ' requires the use of redis'); + } + } + else + { + $this->markTestSkipped('The ' . __CLASS__ . ' requires the use of redis'); + } + + $cache = new \Alchemy\Phrasea\Cache\RedisCache(); + $cache->setRedis($redis); + // Test save + $cache->save('test_key', 'testing this out'); + + // Test contains to test that save() worked + $this->assertTrue($cache->contains('test_key')); + + $cache->save('test_key1', 'testing this out', 20); + + // Test contains to test that save() worked + $this->assertTrue($cache->contains('test_key1')); + + // Test fetch + $this->assertEquals('testing this out', $cache->fetch('test_key')); + + // Test delete + $cache->save('test_key2', 'test2'); + $cache->delete('test_key2'); + $this->assertFalse($cache->contains('test_key2')); + + $ids = $cache->getIds(); + $this->assertTrue(in_array('test_key', $ids)); + + $this->assertEquals($redis, $cache->getRedis()); + } + +} \ No newline at end of file