From c91a84937c558a740b8399c2172b20121c08ae4f Mon Sep 17 00:00:00 2001 From: Romain Neutron Date: Tue, 21 Feb 2012 16:49:54 +0100 Subject: [PATCH] Refactor Cache services --- lib/Alchemy/Phrasea/Cache/ApcCache.php | 29 +++++++- lib/Alchemy/Phrasea/Cache/ArrayCache.php | 31 ++++++++- lib/Alchemy/Phrasea/Cache/Cache.php | 8 ++- lib/Alchemy/Phrasea/Cache/Exception.php | 22 +++++++ lib/Alchemy/Phrasea/Cache/Manager.php | 54 ++++++++++++--- lib/Alchemy/Phrasea/Cache/MemcacheCache.php | 31 ++++++++- lib/Alchemy/Phrasea/Cache/RedisCache.php | 33 ++++++++-- lib/Alchemy/Phrasea/Cache/XcacheCache.php | 29 ++++++-- .../Alchemy/Phrasea/Cache/ApcCacheTest.php | 49 ++++++++++++++ .../Alchemy/Phrasea/Cache/ArrayCacheTest.php | 53 +++++++++++++++ .../Alchemy/Phrasea/Cache/ManagerTest.php | 51 ++++++++++++++ .../Phrasea/Cache/MemcacheCacheTest.php | 66 +++++++++++++++++++ .../Alchemy/Phrasea/Cache/XcacheCacheTest.php | 53 +++++++++++++++ 13 files changed, 484 insertions(+), 25 deletions(-) create mode 100644 lib/Alchemy/Phrasea/Cache/Exception.php create mode 100644 lib/unitTest/Alchemy/Phrasea/Cache/ApcCacheTest.php create mode 100644 lib/unitTest/Alchemy/Phrasea/Cache/ArrayCacheTest.php create mode 100644 lib/unitTest/Alchemy/Phrasea/Cache/ManagerTest.php create mode 100644 lib/unitTest/Alchemy/Phrasea/Cache/MemcacheCacheTest.php create mode 100644 lib/unitTest/Alchemy/Phrasea/Cache/XcacheCacheTest.php diff --git a/lib/Alchemy/Phrasea/Cache/ApcCache.php b/lib/Alchemy/Phrasea/Cache/ApcCache.php index a7c75c260f..ca20934668 100644 --- a/lib/Alchemy/Phrasea/Cache/ApcCache.php +++ b/lib/Alchemy/Phrasea/Cache/ApcCache.php @@ -22,9 +22,34 @@ use Doctrine\Common\Cache\ApcCache as DoctrineApc; class ApcCache extends DoctrineApc implements Cache { - public function flushAll() + public function isServer() { - return apc_clear_cache() && apc_clear_cache('user'); + return false; + } + + public function getStats() + { + return null; + } + + public function get($key) + { + if (!$this->contains($key)) + { + throw new Exception('Unable to retrieve the value'); + } + + return $this->fetch($key); + } + + public function deleteMulti(array $array_keys) + { + foreach ($array_keys as $id) + { + $this->delete($id); + } + + return $this; } } \ No newline at end of file diff --git a/lib/Alchemy/Phrasea/Cache/ArrayCache.php b/lib/Alchemy/Phrasea/Cache/ArrayCache.php index 41c371e23b..a01e9bfb36 100644 --- a/lib/Alchemy/Phrasea/Cache/ArrayCache.php +++ b/lib/Alchemy/Phrasea/Cache/ArrayCache.php @@ -22,10 +22,35 @@ use Doctrine\Common\Cache\ArrayCache as DoctrineArray; class ArrayCache extends DoctrineArray implements Cache { - public function flushAll() + public function isServer() { - $this->data = array(); - return true; + + return false; + } + + public function getStats() + { + + return null; + } + + public function get($id) + { + if (!$this->contains($id)) + { + throw new Exception(sprintf('Unable to find key %s', $id)); + } + return $this->fetch($id); + } + + public function deleteMulti(array $array_keys) + { + foreach ($array_keys as $id) + { + $this->delete($id); + } + + return; } } \ No newline at end of file diff --git a/lib/Alchemy/Phrasea/Cache/Cache.php b/lib/Alchemy/Phrasea/Cache/Cache.php index 4e2baa61d8..cd19553f6a 100644 --- a/lib/Alchemy/Phrasea/Cache/Cache.php +++ b/lib/Alchemy/Phrasea/Cache/Cache.php @@ -22,6 +22,12 @@ use Doctrine\Common\Cache\Cache as DoctrineCache; interface Cache extends DoctrineCache { - public function flushAll(); + public function isServer(); + + public function getStats(); + + public function get($key); + + public function deleteMulti(array $array_keys); } \ No newline at end of file diff --git a/lib/Alchemy/Phrasea/Cache/Exception.php b/lib/Alchemy/Phrasea/Cache/Exception.php new file mode 100644 index 0000000000..5ed763014f --- /dev/null +++ b/lib/Alchemy/Phrasea/Cache/Exception.php @@ -0,0 +1,22 @@ +cacheFile = $file; $this->parser = $parser; + $this->core = $core; $this->registry = $parser->parse($file); } - public function exists($name) + protected function exists($name) { return isset($this->registry[$name]); } - public function get($name) + public function flushAll() { - return $this->exists($name) ? - $this->registry[$name] : null; + foreach ($this->registry as $cacheKey => $service_name) + { + $this->get($cacheKey, $service_name)->getDriver()->deleteAll(); + } + + return $this; } - public function hasChange($name, $driver) + public function get($cacheKey, $service_name) + { + if (!$this->exists($cacheKey)) + { + $this->registry[$cacheKey] = $service_name; + } + + + try + { + $configuration = $this->core->getConfiguration()->getService($service_name); + } + catch (\Exception $e) + { + $configuration = new \Symfony\Component\DependencyInjection\ParameterBag\ParameterBag( + array('type' => 'Cache\\ArrayCache') + ); + } + + $driver = Builder::create($this->core, $service_name, $configuration); + + if ($this->hasChange($cacheKey, $service_name)) + { + $driver->getDriver()->deleteAll(); + $this->save($cacheKey, $service_name); + } + + return $driver; + } + + protected function hasChange($name, $driver) { return $this->exists($name) ? $this->registry[$name] !== $driver : true; } - public function save($name, $driver) + protected function save($name, $driver) { $date = new \DateTime(); diff --git a/lib/Alchemy/Phrasea/Cache/MemcacheCache.php b/lib/Alchemy/Phrasea/Cache/MemcacheCache.php index 6704edcfad..1d81e5e9b8 100644 --- a/lib/Alchemy/Phrasea/Cache/MemcacheCache.php +++ b/lib/Alchemy/Phrasea/Cache/MemcacheCache.php @@ -11,7 +11,7 @@ namespace Alchemy\Phrasea\Cache; -use Doctrine\Common\Cache\MemcacheCache as DoctrineMemcache; +use \Doctrine\Common\Cache\MemcacheCache as DoctrineMemcache; /** * @@ -22,9 +22,34 @@ use Doctrine\Common\Cache\MemcacheCache as DoctrineMemcache; class MemcacheCache extends DoctrineMemcache implements Cache { - public function flushAll() + public function isServer() { - return $this->getMemcache()->flush(); + return true; + } + + public function getStats() + { + return $this->getMemcache()->getstats(); + } + + public function get($key) + { + if (!$this->contains($key)) + { + throw new Exception('Unable to retrieve the value'); + } + + return $this->fetch($key); + } + + public function deleteMulti(array $array_keys) + { + foreach ($array_keys as $id) + { + $this->delete($id); + } + + return $this; } } \ No newline at end of file diff --git a/lib/Alchemy/Phrasea/Cache/RedisCache.php b/lib/Alchemy/Phrasea/Cache/RedisCache.php index b2839b9cbe..b30b49e3c5 100644 --- a/lib/Alchemy/Phrasea/Cache/RedisCache.php +++ b/lib/Alchemy/Phrasea/Cache/RedisCache.php @@ -21,7 +21,7 @@ use Doctrine\Common\Cache\AbstractCache; */ class RedisCache extends AbstractCache implements Cache { - + /** * @var Memcache */ @@ -93,10 +93,35 @@ class RedisCache extends AbstractCache implements Cache { return $this->_redis->delete($id); } - - public function flushAll() + + public function isServer() { - return $this->_redis->flushAll(); + return true; + } + + public function getStats() + { + return null; + } + + public function get($key) + { + if (!$this->contains($key)) + { + throw new Exception('Unable to retrieve the value'); + } + + return $this->fetch($key); + } + + public function deleteMulti(array $array_keys) + { + foreach ($array_keys as $id) + { + $this->delete($id); + } + + return $this; } } diff --git a/lib/Alchemy/Phrasea/Cache/XcacheCache.php b/lib/Alchemy/Phrasea/Cache/XcacheCache.php index d7aa67e6a8..c8c4145629 100644 --- a/lib/Alchemy/Phrasea/Cache/XcacheCache.php +++ b/lib/Alchemy/Phrasea/Cache/XcacheCache.php @@ -22,13 +22,34 @@ use Doctrine\Common\Cache\XcacheCache as DoctrineXcache; class XcacheCache extends DoctrineXcache implements Cache { - public function flushAll() + public function isServer() { - $this->_checkAuth(); + return false; + } - xcache_clear_cache(XC_TYPE_VAR, 0); + public function getStats() + { + return null; + } - return true; + public function get($key) + { + if (!$this->contains($key)) + { + throw new Exception('Unable to retrieve the value'); + } + + return $this->fetch($key); + } + + public function deleteMulti(array $array_keys) + { + foreach ($array_keys as $id) + { + $this->delete($id); + } + + return $this; } } \ No newline at end of file diff --git a/lib/unitTest/Alchemy/Phrasea/Cache/ApcCacheTest.php b/lib/unitTest/Alchemy/Phrasea/Cache/ApcCacheTest.php new file mode 100644 index 0000000000..f88150606a --- /dev/null +++ b/lib/unitTest/Alchemy/Phrasea/Cache/ApcCacheTest.php @@ -0,0 +1,49 @@ +object = new \Alchemy\Phrasea\Cache\ApcCache; + } + + public function testIsServer() + { + $this->assertTrue(is_bool($this->object->isServer())); + } + + public function testGetStats() + { + $this->assertTrue(is_array($this->object->getStats()) || is_null($this->object->getStats())); + } + + public function testGet() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + public function testDeleteMulti() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + +} + diff --git a/lib/unitTest/Alchemy/Phrasea/Cache/ArrayCacheTest.php b/lib/unitTest/Alchemy/Phrasea/Cache/ArrayCacheTest.php new file mode 100644 index 0000000000..c329467562 --- /dev/null +++ b/lib/unitTest/Alchemy/Phrasea/Cache/ArrayCacheTest.php @@ -0,0 +1,53 @@ +object = new \Alchemy\Phrasea\Cache\ArrayCache; + } + + public function testIsServer() + { + $this->assertTrue(is_bool($this->object->isServer())); + } + + public function testGetStats() + { + $this->assertTrue(is_array($this->object->getStats()) || is_null($this->object->getStats())); + } + + public function testGet() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + public function testDeleteMulti() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + +} + diff --git a/lib/unitTest/Alchemy/Phrasea/Cache/ManagerTest.php b/lib/unitTest/Alchemy/Phrasea/Cache/ManagerTest.php new file mode 100644 index 0000000000..b8871a9ca7 --- /dev/null +++ b/lib/unitTest/Alchemy/Phrasea/Cache/ManagerTest.php @@ -0,0 +1,51 @@ +object = new \Alchemy\Phrasea\Cache\Manager; + } + + /** + * @covers {className}::{origMethodName} + * @todo Implement testFlushAll(). + */ + public function testFlushAll() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + /** + * @covers {className}::{origMethodName} + * @todo Implement testGet(). + */ + public function testGet() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + +} + diff --git a/lib/unitTest/Alchemy/Phrasea/Cache/MemcacheCacheTest.php b/lib/unitTest/Alchemy/Phrasea/Cache/MemcacheCacheTest.php new file mode 100644 index 0000000000..6dc6733bcf --- /dev/null +++ b/lib/unitTest/Alchemy/Phrasea/Cache/MemcacheCacheTest.php @@ -0,0 +1,66 @@ +object = new \Alchemy\Phrasea\Cache\MemcacheCache; + + if(!class_exists('Memcache')) + { + $this->markTestSkipped('No memcache extension'); + } + + $memcache = new Memcache(); + if(!@$memcache->connect('localhost', 11211)) + { + $this->markTestSkipped('No memcache server'); + } + + $this->object->setMemcache($memcache); + } + + public function testIsServer() + { + $this->assertTrue(is_bool($this->object->isServer())); + } + + public function testGetStats() + { + $this->assertTrue(is_array($this->object->getStats()) || is_null($this->object->getStats())); + } + + public function testGet() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + public function testDeleteMulti() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + +} + diff --git a/lib/unitTest/Alchemy/Phrasea/Cache/XcacheCacheTest.php b/lib/unitTest/Alchemy/Phrasea/Cache/XcacheCacheTest.php new file mode 100644 index 0000000000..42ef4312d8 --- /dev/null +++ b/lib/unitTest/Alchemy/Phrasea/Cache/XcacheCacheTest.php @@ -0,0 +1,53 @@ +object = new \Alchemy\Phrasea\Cache\XcacheCache; + } + + public function testIsServer() + { + $this->assertTrue(is_bool($this->object->isServer())); + } + + public function testGetStats() + { + $this->assertTrue(is_array($this->object->getStats()) || is_null($this->object->getStats())); + } + + public function testGet() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + + public function testDeleteMulti() + { + // Remove the following lines when you implement this test. + $this->markTestIncomplete( + 'This test has not been implemented yet.' + ); + } + +} +