From 4f4e8a5be8ed5acc971cf71d664ddcf7e1d4b895 Mon Sep 17 00:00:00 2001 From: ADmad Date: Sun, 8 Sep 2013 21:40:37 +0530 Subject: [PATCH] Fix Memcached engine --- lib/Cake/Cache/Engine/MemcachedEngine.php | 26 ++++---- .../Cache/Engine/MemcachedEngineTest.php | 65 ++++++++++++++----- 2 files changed, 62 insertions(+), 29 deletions(-) mode change 100755 => 100644 lib/Cake/Cache/Engine/MemcachedEngine.php rename lib/Cake/Test/{Case => TestCase}/Cache/Engine/MemcachedEngineTest.php (91%) mode change 100755 => 100644 diff --git a/lib/Cake/Cache/Engine/MemcachedEngine.php b/lib/Cake/Cache/Engine/MemcachedEngine.php old mode 100755 new mode 100644 index ad41f1d680f..a42d2c6e608 --- a/lib/Cake/Cache/Engine/MemcachedEngine.php +++ b/lib/Cake/Cache/Engine/MemcachedEngine.php @@ -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 @@ -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 { @@ -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')) { @@ -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())) { @@ -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') ); } @@ -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']); } /** @@ -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); @@ -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); diff --git a/lib/Cake/Test/Case/Cache/Engine/MemcachedEngineTest.php b/lib/Cake/Test/TestCase/Cache/Engine/MemcachedEngineTest.php old mode 100755 new mode 100644 similarity index 91% rename from lib/Cake/Test/Case/Cache/Engine/MemcachedEngineTest.php rename to lib/Cake/Test/TestCase/Cache/Engine/MemcachedEngineTest.php index 7ca8e41555d..3018ddeecfc --- a/lib/Cake/Test/Case/Cache/Engine/MemcachedEngineTest.php +++ b/lib/Cake/Test/TestCase/Cache/Engine/MemcachedEngineTest.php @@ -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 { @@ -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 @@ -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'); } @@ -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( @@ -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)); } /** @@ -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); } @@ -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); @@ -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 = ''; @@ -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); @@ -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'); @@ -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); @@ -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)); } /** @@ -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);