Add serializer option to Memcached Cache engine

Add option to select a serializer engine among php, json or igbinary
This commit is contained in:
Kamisama 2013-09-18 00:29:48 -04:00
parent a7a6fcae8a
commit bd3f005ab6
2 changed files with 121 additions and 3 deletions

View file

@ -47,6 +47,19 @@ class MemcachedEngine extends CacheEngine {
*/
public $settings = array();
/**
* List of available serializer engine
*
* Memcached must be compiled with json and igbinary support to use these engines
*
* @var array
*/
public static $serializer = array(
'igbinary' => Memcached::SERIALIZER_IGBINARY,
'json' => Memcached::SERIALIZER_JSON,
'php' => Memcached::SERIALIZER_PHP
);
/**
* Initialize the Cache Engine
*
@ -71,6 +84,7 @@ class MemcachedEngine extends CacheEngine {
'persistent' => false,
'login' => null,
'password' => null,
'serializer' => 'php'
);
parent::init($settings);
@ -113,14 +127,33 @@ class MemcachedEngine extends CacheEngine {
/**
* Settings the memcached instance
*
* @throws CacheException when the Memcached extension is not built with the desired serializer engine
*/
protected function _setOptions() {
$this->_Memcached->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
if (Memcached::HAVE_IGBINARY) {
$this->_Memcached->setOption(Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_IGBINARY);
if (!array_key_exists($this->settings['serializer'], self::$serializer)) {
throw new CacheException(
__d('cake_dev', sprintf('%s is not a valid serializer engine for Memcached', $this->settings['serializer']))
);
}
$serializer = self::$serializer['php'];
switch($this->settings['serializer']) {
case 'igbinary':
if (Memcached::HAVE_IGBINARY) {
$serializer = self::$serializer['igbinary'];
}
break;
case 'json':
if (Memcached::HAVE_JSON) {
$serializer = self::$serializer['json'];
}
break;
}
$this->_Memcached->setOption(Memcached::OPT_SERIALIZER, $serializer);
// Check for Amazon ElastiCache instance
if (defined('Memcached::OPT_CLIENT_MODE') && defined('Memcached::DYNAMIC_CLIENT_MODE')) {
$this->_Memcached->setOption(Memcached::OPT_CLIENT_MODE, Memcached::DYNAMIC_CLIENT_MODE);

View file

@ -102,7 +102,8 @@ class MemcachedEngineTest extends CakeTestCase {
'engine' => 'Memcached',
'login' => null,
'password' => null,
'groups' => array()
'groups' => array(),
'serializer' => 'php'
);
$this->assertEquals($expecting, $settings);
}
@ -132,6 +133,90 @@ class MemcachedEngineTest extends CakeTestCase {
$this->assertTrue($MemcachedCompressed->getMemcached()->getOption(Memcached::OPT_COMPRESSION));
}
/**
* test accepts only valid serializer engine
*
* @return void
*/
public function testInvalidSerializerSetting() {
$Memcached = new TestMemcachedEngine();
$settings = array(
'engine' => 'Memcached',
'servers' => array('127.0.0.1:11211'),
'persistent' => false,
'serializer' => 'invalid_serializer'
);
$this->setExpectedException(
'CacheException', 'invalid_serializer is not a valid serializer engine for Memcached'
);
$Memcached->init($settings);
}
/**
* testPhpSerializerSetting method
*
* @return void
*/
public function testPhpSerializerSetting() {
$Memcached = new TestMemcachedEngine();
$settings = array(
'engine' => 'Memcached',
'servers' => array('127.0.0.1:11211'),
'persistent' => false,
'serializer' => 'php'
);
$Memcached->init($settings);
$this->assertEquals(Memcached::SERIALIZER_PHP, $Memcached->getMemcached()->getOption(Memcached::OPT_SERIALIZER));
}
/**
* testPhpSerializerSetting method
*
* @return void
*/
public function testJsonSerializerSetting() {
$this->skipIf(
!Memcached::HAVE_JSON,
'Memcached extension is not compiled with json support'
);
$Memcached = new TestMemcachedEngine();
$settings = array(
'engine' => 'Memcached',
'servers' => array('127.0.0.1:11211'),
'persistent' => false,
'serializer' => 'json'
);
$Memcached->init($settings);
$this->assertEquals(Memcached::SERIALIZER_JSON, $Memcached->getMemcached()->getOption(Memcached::OPT_SERIALIZER));
}
/**
* testPhpSerializerSetting method
*
* @return void
*/
public function testIgbinarySerializerSetting() {
$this->skipIf(
!Memcached::HAVE_IGBINARY,
'Memcached extension is not compiled with igbinary support'
);
$Memcached = new TestMemcachedEngine();
$settings = array(
'engine' => 'Memcached',
'servers' => array('127.0.0.1:11211'),
'persistent' => false,
'serializer' => 'igbinary'
);
$Memcached->init($settings);
$this->assertEquals(Memcached::SERIALIZER_IGBINARY, $Memcached->getMemcached()->getOption(Memcached::OPT_SERIALIZER));
}
/**
* test using authentication without memcached installed with SASL support
* throw an exception