diff --git a/src/Cache/Engine/MemcachedEngine.php b/src/Cache/Engine/MemcachedEngine.php index 31dd17f00e1..9984504ade3 100644 --- a/src/Cache/Engine/MemcachedEngine.php +++ b/src/Cache/Engine/MemcachedEngine.php @@ -232,6 +232,27 @@ public function write($key, $value) { return $this->_Memcached->set($key, $value, $duration); } +/** + * Write many cache entries to the cache at once + * + * @param array $data An array of data to be stored in the cache + * @return array of bools for each key provided, true if the data was successfully cached, false on failure + */ + public function writeMany($data) { + $cacheData = array(); + foreach ($data as $key => $value) { + $cacheData[$this->_key($key)] = $value; + } + + $success = $this->_Memcached->setMulti($cacheData); + + $return = array(); + foreach (array_keys($data) as $key) { + $return[$key] = $success; + } + return $return; + } + /** * Read a key from the cache * @@ -244,6 +265,27 @@ public function read($key) { return $this->_Memcached->get($key); } +/** + * Read many keys from the cache at once + * + * @param array $keys An array of identifiers for the data + * @return An array containing, for each of the given $keys, the cached data or false if cached data could not be + * retreived + */ + public function readMany($keys) { + $cacheKeys = array(); + foreach ($keys as $key) { + $cacheKeys[] = $this->_key($key); + } + + $values = $this->_Memcached->getMulti($cacheKeys); + $return = array(); + foreach ($keys as &$key) { + $return[$key] = array_key_exists($this->_key($key), $values) ? $values[$this->_key($key)] : false; + } + return $return; + } + /** * Increments the value of an integer cached key * @@ -284,6 +326,28 @@ public function delete($key) { return $this->_Memcached->delete($key); } +/** + * Delete many keys from the cache at once + * + * @param array $keys An array of identifiers for the data + * @return array of boolean values that are true if the key was successfully deleted, false if it didn't exist or + * couldn't be removed + */ + public function deleteMany($keys) { + $cacheKeys = array(); + foreach ($keys as $key) { + $cacheKeys[] = $this->_key($key); + } + + $success = $this->_Memcached->deleteMulti($cacheKeys); + + $return = array(); + foreach ($keys as $key) { + $return[$key] = $success; + } + return $return; + } + /** * Delete all keys from the cache * diff --git a/tests/TestCase/Cache/Engine/MemcachedEngineTest.php b/tests/TestCase/Cache/Engine/MemcachedEngineTest.php index e5e366b053d..0bd04b21af3 100644 --- a/tests/TestCase/Cache/Engine/MemcachedEngineTest.php +++ b/tests/TestCase/Cache/Engine/MemcachedEngineTest.php @@ -450,6 +450,85 @@ public function testReadAndWriteCache() { Cache::delete('test', 'memcached'); } +/** + * testReadMany method + * + * @return void + */ + public function testReadMany() { + $this->_configCache(['duration' => 2]); + $data = array( + 'App.falseTest' => false, + 'App.trueTest' => true, + 'App.nullTest' => null, + 'App.zeroTest' => 0, + 'App.zeroTest2' => '0' + ); + foreach ($data as $key => $value) { + Cache::write($key, $value, 'memcached'); + } + + $read = Cache::readMany(array_keys($data), 'memcached'); + + $this->assertSame($read['App.falseTest'], false); + $this->assertSame($read['App.trueTest'], true); + $this->assertSame($read['App.nullTest'], null); + $this->assertSame($read['App.zeroTest'], 0); + $this->assertSame($read['App.zeroTest2'], '0'); + } + +/** + * testWriteMany method + * + * @return void + */ + public function testWriteMany() { + $this->_configCache(['duration' => 2]); + $data = array( + 'App.falseTest' => false, + 'App.trueTest' => true, + 'App.nullTest' => null, + 'App.zeroTest' => 0, + 'App.zeroTest2' => '0' + ); + Cache::writeMany($data, 'memcached'); + + $this->assertSame(Cache::read('App.falseTest', 'memcached'), false); + $this->assertSame(Cache::read('App.trueTest', 'memcached'), true); + $this->assertSame(Cache::read('App.nullTest', 'memcached'), null); + $this->assertSame(Cache::read('App.zeroTest', 'memcached'), 0); + $this->assertSame(Cache::read('App.zeroTest2', 'memcached'), '0'); + } + +/** + * testDeleteMany method + * + * @return void + */ + public function testDeleteMany() { + $this->_configCache(); + $data = array( + 'App.falseTest' => false, + 'App.trueTest' => true, + 'App.nullTest' => null, + 'App.zeroTest' => 0, + 'App.zeroTest2' => '0', + ); + foreach ($data as $key => $value) { + Cache::write($key, $value, 'memcached'); + } + Cache::write('App.keepTest', 'keepMe', 'memcached'); + + Cache::deleteMany(array_merge(array_keys($data), ['App.doesNotExist']), 'memcached'); + + $this->assertSame(Cache::read('App.falseTest', 'memcached'), false); + $this->assertSame(Cache::read('App.trueTest', 'memcached'), false); + $this->assertSame(Cache::read('App.nullTest', 'memcached'), false); + $this->assertSame(Cache::read('App.zeroTest', 'memcached'), false); + $this->assertSame(Cache::read('App.zeroTest2', 'memcached'), false); + $this->assertSame(Cache::read('App.keepTest', 'memcached'), 'keepMe'); + } + /** * testExpiry method *