Skip to content

Commit

Permalink
Fix Memcached engine
Browse files Browse the repository at this point in the history
  • Loading branch information
ADmad committed Sep 8, 2013
1 parent b1184dc commit 4f4e8a5
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 29 deletions.
26 changes: 15 additions & 11 deletions lib/Cake/Cache/Engine/MemcachedEngine.php 100755 → 100644
Expand Up @@ -12,6 +12,11 @@
* @since CakePHP(tm) v 2.5.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Cache\Engine;

use Cake\Cache\CacheEngine;
use Cake\Error;
use Cake\Utility\Inflector;

/**
* Memcached storage engine for cache. Memcached has some limitations in the amount of
Expand All @@ -23,7 +28,6 @@
* (if memcached extension compiled with --enable-igbinary)
* Compressed keys can also be incremented/decremented
*
* @package Cake.Cache.Engine
*/
class MemcachedEngine extends CacheEngine {

Expand Down Expand Up @@ -55,7 +59,7 @@ class MemcachedEngine extends CacheEngine {
*
* @param array $settings array of setting for the engine
* @return boolean True if the engine has been successfully initialized, false if not
* @throws CacheException when you try use authentication without Memcached compiled with SASL support
* @throws Cake\Error\Exception when you try use authentication without Memcached compiled with SASL support
*/
public function init($settings = array()) {
if (!class_exists('Memcached')) {
Expand All @@ -82,7 +86,7 @@ public function init($settings = array()) {
return true;
}

$this->_Memcached = new Memcached($this->settings['persistent'] ? (string)$this->settings['persistent'] : null);
$this->_Memcached = new \Memcached($this->settings['persistent'] ? (string)$this->settings['persistent'] : null);
$this->_setOptions();

if (count($this->_Memcached->getServerList())) {
Expand All @@ -100,7 +104,7 @@ public function init($settings = array()) {

if ($this->settings['login'] !== null && $this->settings['password'] !== null) {
if (!method_exists($this->_Memcached, 'setSaslAuthData')) {
throw new CacheException(
throw new Error\Exception(
__d('cake_dev', 'Memcached extension is not build with SASL support')
);
}
Expand All @@ -115,18 +119,18 @@ public function init($settings = array()) {
*
*/
protected function _setOptions() {
$this->_Memcached->setOption(Memcached::OPT_LIBKETAMA_COMPATIBLE, true);
$this->_Memcached->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true);

if (Memcached::HAVE_IGBINARY) {
$this->_Memcached->setOption(Memcached::OPT_SERIALIZER, Memcached::SERIALIZER_IGBINARY);
if (\Memcached::HAVE_IGBINARY) {
$this->_Memcached->setOption(\Memcached::OPT_SERIALIZER, \Memcached::SERIALIZER_IGBINARY);
}

// 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);
$this->_Memcached->setOption(\Memcached::OPT_CLIENT_MODE, \Memcached::DYNAMIC_CLIENT_MODE);
}

$this->_Memcached->setOption(Memcached::OPT_COMPRESSION, (bool)$this->settings['compress']);
$this->_Memcached->setOption(\Memcached::OPT_COMPRESSION, (bool)$this->settings['compress']);
}

/**
Expand Down Expand Up @@ -192,7 +196,7 @@ public function read($key) {
* @param string $key Identifier for the data
* @param integer $offset How much to increment
* @return New incremented value, false otherwise
* @throws CacheException when you try to increment with compress = true
* @throws Cake\Error\Exception when you try to increment with compress = true
*/
public function increment($key, $offset = 1) {
return $this->_Memcached->increment($key, $offset);
Expand All @@ -204,7 +208,7 @@ public function increment($key, $offset = 1) {
* @param string $key Identifier for the data
* @param integer $offset How much to subtract
* @return New decremented value, false otherwise
* @throws CacheException when you try to decrement with compress = true
* @throws Cake\Error\Exception when you try to decrement with compress = true
*/
public function decrement($key, $offset = 1) {
return $this->_Memcached->decrement($key, $offset);
Expand Down
65 changes: 47 additions & 18 deletions ...Case/Cache/Engine/MemcachedEngineTest.php → ...Case/Cache/Engine/MemcachedEngineTest.php 100755 → 100644
Expand Up @@ -13,18 +13,19 @@
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Cache.Engine
* @since CakePHP(tm) v 2.5.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Test\TestCase\Cache\Engine;

App::uses('Cache', 'Cache');
App::uses('MemcachedEngine', 'Cache/Engine');
use Cake\Cache\Cache;
use Cake\Cache\Engine\MemcachedEngine;
use Cake\Core\Configure;
use Cake\TestSuite\TestCase;

/**
* Class TestMemcachedEngine
*
* @package Cake.Test.Case.Cache.Engine
*/
class TestMemcachedEngine extends MemcachedEngine {

Expand All @@ -51,9 +52,8 @@ public function getMemcached() {
/**
* MemcachedEngineTest class
*
* @package Cake.Test.Case.Cache.Engine
*/
class MemcachedEngineTest extends CakeTestCase {
class MemcachedEngineTest extends TestCase {

/**
* setUp method
Expand Down Expand Up @@ -81,6 +81,7 @@ public function tearDown() {
Cache::drop('memcached');
Cache::drop('memcached_groups');
Cache::drop('memcached_helper');
Cache::drop('compressed_memcached');
Cache::config('default');
}

Expand Down Expand Up @@ -120,7 +121,7 @@ public function testCompressionSetting() {
'compress' => false
));

$this->assertFalse($Memcached->getMemcached()->getOption(Memcached::OPT_COMPRESSION));
$this->assertFalse($Memcached->getMemcached()->getOption(\Memcached::OPT_COMPRESSION));

$MemcachedCompressed = new TestMemcachedEngine();
$MemcachedCompressed->init(array(
Expand All @@ -129,7 +130,7 @@ public function testCompressionSetting() {
'compress' => true
));

$this->assertTrue($MemcachedCompressed->getMemcached()->getOption(Memcached::OPT_COMPRESSION));
$this->assertTrue($MemcachedCompressed->getMemcached()->getOption(\Memcached::OPT_COMPRESSION));
}

/**
Expand All @@ -154,7 +155,7 @@ public function testSaslAuthException() {
);

$this->setExpectedException(
'CacheException', 'Memcached extension is not build with SASL support'
'Cake\Error\Exception', 'Memcached extension is not build with SASL support'
);
$Memcached->init($settings);
}
Expand All @@ -167,7 +168,7 @@ public function testSaslAuthException() {
public function testMultipleServers() {
$servers = array('127.0.0.1:11211', '127.0.0.1:11222');
$available = true;
$Memcached = new Memcached();
$Memcached = new \Memcached();

foreach ($servers as $server) {
list($host, $port) = explode(':', $server);
Expand Down Expand Up @@ -237,7 +238,12 @@ public function testParseServerStringUnix() {
* @return void
*/
public function testReadAndWriteCache() {
Cache::set(array('duration' => 1), null, 'memcached');
Cache::drop('memcached');
Cache::config('memcached', array(
'engine' => 'Memcached',
'prefix' => 'cake_',
'duration' => 1
));

$result = Cache::read('test', 'memcached');
$expecting = '';
Expand All @@ -260,7 +266,12 @@ public function testReadAndWriteCache() {
* @return void
*/
public function testExpiry() {
Cache::set(array('duration' => 1), 'memcached');
Cache::drop('memcached');
Cache::config('memcached', array(
'engine' => 'Memcached',
'prefix' => 'cake_',
'duration' => 1
));

$result = Cache::read('test', 'memcached');
$this->assertFalse($result);
Expand All @@ -273,7 +284,12 @@ public function testExpiry() {
$result = Cache::read('other_test', 'memcached');
$this->assertFalse($result);

Cache::set(array('duration' => "+1 second"), 'memcached');
Cache::drop('memcached');
Cache::config('memcached', array(
'engine' => 'Memcached',
'prefix' => 'cake_',
'duration' => '+1 second'
));

$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('other_test', $data, 'memcached');
Expand All @@ -283,12 +299,22 @@ public function testExpiry() {
$result = Cache::read('other_test', 'memcached');
$this->assertFalse($result);

Cache::config('memcached', array('duration' => '+1 second'));
Cache::drop('memcached');
Cache::config('memcached', array(
'engine' => 'Memcached',
'prefix' => 'cake_',
'duration' => '+1 second'
));

$result = Cache::read('other_test', 'memcached');
$this->assertFalse($result);

Cache::config('memcached', array('duration' => '+29 days'));
Cache::drop('memcached');
Cache::config('memcached', array(
'engine' => 'Memcached',
'prefix' => 'cake2_',
'duration' => '+29 days'
));
$data = 'this is a test of the emergency broadcasting system';
$result = Cache::write('long_expiry_test', $data, 'memcached');
$this->assertTrue($result);
Expand All @@ -297,8 +323,6 @@ public function testExpiry() {
$result = Cache::read('long_expiry_test', 'memcached');
$expecting = $data;
$this->assertEquals($expecting, $result);

Cache::config('memcached', array('duration' => 3600));
}

/**
Expand Down Expand Up @@ -492,7 +516,12 @@ public function testClear() {
* @return void
*/
public function testZeroDuration() {
Cache::config('memcached', array('duration' => 0));
Cache::drop('memcached');
Cache::config('memcached', array(
'engine' => 'Memcached',
'prefix' => 'cake2_',
'duration' => 0
));
$result = Cache::write('test_key', 'written!', 'memcached');

$this->assertTrue($result);
Expand Down

0 comments on commit 4f4e8a5

Please sign in to comment.