From 34d51f45107c4b2a2ab6fe5e75b9d2324e503fe6 Mon Sep 17 00:00:00 2001 From: d3f3kt Date: Fri, 2 Jun 2017 15:54:08 +0200 Subject: [PATCH] [Feature] add cache manager service Add cache manager to get registered memcache pools and flush all pools at once. --- Cache/MemcacheManager.php | 87 ++++++++++++++++++++ DependencyInjection/LswMemcacheExtension.php | 9 +- Resources/config/config.yml | 4 + Tests/Cache/MemcacheManagerTest.php | 53 ++++++++++++ 4 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 Cache/MemcacheManager.php create mode 100644 Tests/Cache/MemcacheManagerTest.php diff --git a/Cache/MemcacheManager.php b/Cache/MemcacheManager.php new file mode 100644 index 0000000..9ee637b --- /dev/null +++ b/Cache/MemcacheManager.php @@ -0,0 +1,87 @@ + + */ +class MemcacheManager +{ + /** + * Registered memcache pools. + * + * @var MemcacheInterface[] + */ + private $pools; + + /** + * Inititialize empty pool collection. + */ + public function __construct() + { + $this->pools = array(); + } + + /** + * Add registered memcache. + * + * @param string $name memcache name obtained from config + * @param MemcacheInterface $memcache + */ + public function addMemcachePool($name, MemcacheInterface $memcache) + { + $this->pools[$name] = $memcache; + } + + /** + * Get memcache service by name if it exists. + * + * @param string $name memcache name + * + * @return null|MemcacheInterface + */ + public function getMemcache($name) + { + if (array_key_exists($name, $this->pools)) { + return $this->pools[$name]; + } + } + + /** + * Get all memcache pools. + * + * @return array + */ + public function getMemcaches() + { + return $this->pools; + } + + /** + * Flush memcache by name. + * + * @param string $name memcache name + * + * @return bool TRUE if flush was successful, FALSE otherwise + */ + public function flushMemcache($name) + { + if ($memcache = $this->getMemcache($name)) { + return $memcache->flush(); + } else { + return false; + } + } + + /** + * Flush all registered memcaches. + */ + public function flushAllMemcaches() + { + foreach ($this->pools as $memcache) { + $memcache->flush(); + } + } +} diff --git a/DependencyInjection/LswMemcacheExtension.php b/DependencyInjection/LswMemcacheExtension.php index 565ff38..254ece8 100644 --- a/DependencyInjection/LswMemcacheExtension.php +++ b/DependencyInjection/LswMemcacheExtension.php @@ -126,7 +126,7 @@ protected function loadDoctrine(array $config, ContainerBuilder $container) } } } - + /** * Loads the Firewall configuration. * @@ -211,7 +211,7 @@ private function newMemcacheClient($name, array $config, ContainerBuilder $conta $memcache->addMethodCall('addServer', $server); } } - + $memcache->addArgument($config['options']); $options = array(); @@ -223,11 +223,16 @@ private function newMemcacheClient($name, array $config, ContainerBuilder $conta // Add the service to the container $serviceName = sprintf('memcache.%s', $name); $container->setDefinition($serviceName, $memcache); + // Add the service to the data collector if ($container->hasDefinition('memcache.data_collector')) { $definition = $container->getDefinition('memcache.data_collector'); $definition->addMethodCall('addClient', array($name, $options, new Reference($serviceName))); } + + // Add the service to the manager + $container->getDefinition('memcache.memcache_manager') + ->addMethodCall('addMemcachePool', array($name, new Reference($serviceName))); } } diff --git a/Resources/config/config.yml b/Resources/config/config.yml index 138d221..a152b6b 100644 --- a/Resources/config/config.yml +++ b/Resources/config/config.yml @@ -2,3 +2,7 @@ parameters: memcache.doctrine_cache.class: Lsw\MemcacheBundle\Doctrine\Cache\MemcacheCache memcache.session_handler.class: Lsw\MemcacheBundle\Session\Storage\LockingSessionHandler memcache.firewall_handler.class: Lsw\MemcacheBundle\Firewall\FirewallHandler + +services: + memcache.memcache_manager: + class: Lsw\MemcacheBundle\Cache\MemcacheManager diff --git a/Tests/Cache/MemcacheManagerTest.php b/Tests/Cache/MemcacheManagerTest.php new file mode 100644 index 0000000..cacb391 --- /dev/null +++ b/Tests/Cache/MemcacheManagerTest.php @@ -0,0 +1,53 @@ + + */ +class MemcacheManagerTest extends \PHPUnit_Framework_TestCase +{ + public function testConstruct() + { + $cache = new LoggingMemcache(false); + $cache->addServer('localhost', '11211'); + $cacheManager = new MemcacheManager(); + + $this->assertEquals(array(), $cacheManager->getMemcaches()); + + $cacheManager->addMemcachePool('foo', $cache); + + $this->assertArrayHasKey('foo', $cacheManager->getMemcaches()); + $this->assertEquals($cache, $cacheManager->getMemcache('foo')); + + return array($cache, $cacheManager); + } + + /** + * @depends testConstruct + */ + public function testFlush($args) + { + list($cache, $cacheManager) = $args; + + $cache->set('key', 'value'); + $this->assertEquals('value', $cache->get('key')); + + $this->assertFalse($cacheManager->flushMemcache('bar')); + $this->assertTrue($cacheManager->flushMemcache('foo')); + $this->assertFlase($cache->get('key')); + + $cache->set('key', 'value'); + $this->assertEquals('value', $cache->get('key')); + $cacheManager->flushAllMemcaches(); + + $this->assertFalse($cache->get('key')); + + } +}