Add cache provider for memcached extension

This commit is contained in:
Romain Neutron
2013-11-04 19:11:36 +01:00
parent 1b393ef6d9
commit ab512540ca
2 changed files with 29 additions and 0 deletions

View File

@@ -45,6 +45,10 @@ class Factory
case 'memcachecache':
$cache = $this->createMemcache($options);
break;
case 'memcached':
case 'memcachecached':
$cache = $this->createMemcached($options);
break;
case 'redis':
case 'rediscache':
$cache = $this->createRedis($options);
@@ -131,6 +135,29 @@ class Factory
return $cache;
}
private function createMemcached($options)
{
if (!extension_loaded('memcached')) {
throw new RuntimeException('The Memcached cache requires the Memcached extension.');
}
$host = isset($options['host']) ? $options['host'] : 'localhost';
$port = isset($options['port']) ? $options['port'] : 11211;
$memcached = new \Memcached();
$memcached->addServer($host, $port);
$memcached->getStats();
if (\Memcached::RES_SUCCESS !== $memcached->getResultCode()) {
throw new RuntimeException(sprintf("Memcached instance with host '%s' and port '%s' is not reachable", $host, $port));
}
$cache = new MemcachedCache();
$cache->setMemcached($memcached);
return $cache;
}
private function createApc($options)
{
if (!extension_loaded('apc')) {

View File

@@ -28,6 +28,8 @@ class FactoryTest extends \PHPUnit_Framework_TestCase
array('arraycache', null, 'Alchemy\Phrasea\Cache\ArrayCache'),
array('memcache', 'memcache', 'Alchemy\Phrasea\Cache\MemcacheCache'),
array('memcachecache', 'memcache', 'Alchemy\Phrasea\Cache\MemcacheCache'),
array('memcached', 'memcached', 'Alchemy\Phrasea\Cache\MemcachedCache'),
array('memcachecached', 'memcached', 'Alchemy\Phrasea\Cache\MemcachedCache'),
array('redis', 'redis', 'Alchemy\Phrasea\Cache\RedisCache'),
array('rediscache', 'redis', 'Alchemy\Phrasea\Cache\RedisCache'),
array('wincache', 'wincache', 'Alchemy\Phrasea\Cache\WincacheCache'),