This repository has been archived by the owner on Oct 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cache manager to get registered memcache pools and flush all pools at once.
- Loading branch information
Showing
4 changed files
with
151 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
*/ | ||
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
*/ | ||
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->assertNull($cache->get('key')); | ||
|
||
$cache->set('key', 'value'); | ||
$this->assertEquals('value', $cache->get('key')); | ||
$cacheManager->flushAllMemcaches(); | ||
|
||
$this->assertNull($cache->get('key')); | ||
|
||
} | ||
} |