Skip to content
This repository has been archived by the owner on Oct 28, 2020. It is now read-only.

Commit

Permalink
[Feature] add cache manager service
Browse files Browse the repository at this point in the history
Add cache manager to get registered memcache pools and
flush all pools at once.
  • Loading branch information
d3f3kt committed Jun 2, 2017
1 parent da91b5b commit 34d51f4
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 2 deletions.
87 changes: 87 additions & 0 deletions Cache/MemcacheManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace Lsw\MemcacheBundle\Cache;

/**
* Central class to manage single memcache instances.
*
* @author David Geistert <david@geistert.info>
*/
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();
}
}
}
9 changes: 7 additions & 2 deletions DependencyInjection/LswMemcacheExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected function loadDoctrine(array $config, ContainerBuilder $container)
}
}
}

/**
* Loads the Firewall configuration.
*
Expand Down Expand Up @@ -211,7 +211,7 @@ private function newMemcacheClient($name, array $config, ContainerBuilder $conta
$memcache->addMethodCall('addServer', $server);
}
}

$memcache->addArgument($config['options']);

$options = array();
Expand All @@ -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)));
}

}
4 changes: 4 additions & 0 deletions Resources/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
53 changes: 53 additions & 0 deletions Tests/Cache/MemcacheManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Lsw\MemcacheBundle\Tests\Cache;


use Lsw\MemcacheBundle\Cache\MemcacheManager;
use Lsw\MemcacheBundle\Cache\LoggingMemcache;

/**
* Testing the MemcacheManager Class.
*
* @author David Geistert <david@geistert.info>
*/
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'));

}
}

0 comments on commit 34d51f4

Please sign in to comment.