Skip to content

Commit

Permalink
Add a null caching engine.
Browse files Browse the repository at this point in the history
Having a NullEngine makes a bunch of internal operations simpler as we
don't have to check whether or not the engine() method returned an
instance or not. It also makes it easy to use a null adapter in testing
without having to generate mocks.
  • Loading branch information
markstory committed Aug 17, 2014
1 parent edd85f5 commit 05d8a19
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 40 deletions.
43 changes: 6 additions & 37 deletions src/Cache/Cache.php
Expand Up @@ -15,6 +15,7 @@
namespace Cake\Cache;

use Cake\Core\StaticConfigTrait;
use Cake\Cache\Engine\NullEngine;
use Cake\Error;

/**
Expand Down Expand Up @@ -128,11 +129,11 @@ protected static function _buildEngine($name) {
* triggered.
*
* @param string $config The configuration name you want an engine for.
* @return \Cake\Cache\CacheEngine
* @return \Cake\Cache\CacheEngine When caching is diabled a null engine will be returned.
*/
public static function engine($config) {
if (!static::$_enabled) {
return false;
return new NullEngine();
}

if (isset(static::$_registry->{$config})) {
Expand All @@ -154,10 +155,6 @@ public static function engine($config) {
*/
public static function gc($config = 'default', $expires = null) {
$engine = static::engine($config);
if (!$engine) {
return;
}

$engine->gc($expires);
}

Expand All @@ -181,7 +178,7 @@ public static function gc($config = 'default', $expires = null) {
*/
public static function write($key, $value, $config = 'default') {
$engine = static::engine($config);
if (!$engine || is_resource($value)) {
if (is_resource($value)) {
return false;
}

Expand Down Expand Up @@ -220,10 +217,6 @@ public static function write($key, $value, $config = 'default') {
*/
public static function writeMany($data, $config = 'default') {
$engine = static::engine($config);
if (!$engine) {
return false;
}

$return = $engine->writeMany($data);
foreach ($return as $key => $success) {
if ($success === false && !empty($data[$key])) {
Expand Down Expand Up @@ -257,10 +250,6 @@ public static function writeMany($data, $config = 'default') {
*/
public static function read($key, $config = 'default') {
$engine = static::engine($config);
if (!$engine) {
return false;
}

return $engine->read($key);
}

Expand All @@ -284,10 +273,6 @@ public static function read($key, $config = 'default') {
*/
public static function readMany($keys, $config = 'default') {
$engine = static::engine($config);
if (!$engine) {
return false;
}

return $engine->readMany($keys);
}

Expand All @@ -302,7 +287,7 @@ public static function readMany($keys, $config = 'default') {
*/
public static function increment($key, $offset = 1, $config = 'default') {
$engine = static::engine($config);
if (!$engine || !is_int($offset) || $offset < 0) {
if (!is_int($offset) || $offset < 0) {
return false;
}

Expand All @@ -320,7 +305,7 @@ public static function increment($key, $offset = 1, $config = 'default') {
*/
public static function decrement($key, $offset = 1, $config = 'default') {
$engine = static::engine($config);
if (!$engine || !is_int($offset) || $offset < 0) {
if (!is_int($offset) || $offset < 0) {
return false;
}

Expand All @@ -346,10 +331,6 @@ public static function decrement($key, $offset = 1, $config = 'default') {
*/
public static function delete($key, $config = 'default') {
$engine = static::engine($config);
if (!$engine) {
return false;
}

return $engine->delete($key);
}

Expand All @@ -373,10 +354,6 @@ public static function delete($key, $config = 'default') {
*/
public static function deleteMany($keys, $config = 'default') {
$engine = static::engine($config);
if (!$engine) {
return false;
}

return $engine->deleteMany($keys);
}

Expand All @@ -389,10 +366,6 @@ public static function deleteMany($keys, $config = 'default') {
*/
public static function clear($check = false, $config = 'default') {
$engine = static::engine($config);
if (!$engine) {
return false;
}

return $engine->clear($check);
}

Expand All @@ -405,10 +378,6 @@ public static function clear($check = false, $config = 'default') {
*/
public static function clearGroup($group, $config = 'default') {
$engine = static::engine($config);
if (!$engine) {
return false;
}

return $engine->clearGroup($group);
}

Expand Down
104 changes: 104 additions & 0 deletions src/Cache/Engine/NullEngine.php
@@ -0,0 +1,104 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Cache\Engine;

use Cake\Cache\CacheEngine;

/**
* Null cache engine, all operations return false.
*
* This is used internally for when Cache::disable() has been called.
*/
class NullEngine extends CacheEngine {

/**
* {@inheritDoc}
*/
public function init(array $config = []) {
}

/**
* {@inheritDoc}
*/
public function gc($expires = null) {
return false;
}

/**
* {@inheritDoc}
*/
public function write($key, $value) {
}

/**
* {@inheritDoc}
*/
public function writeMany($data) {
}

/**
* {@inheritDoc}
*/
public function read($key) {
return false;
}

/**
* {@inheritDoc}
*/
public function readMany($keys) {
return false;
}

/**
* {@inheritDoc}
*/
public function increment($key, $offset = 1) {
}

/**
* {@inheritDoc}
*/
public function decrement($key, $offset = 1) {
}

/**
* {@inheritDoc}
*/
public function delete($key) {
}

/**
* {@inheritDoc}
*/
public function deleteMany($keys) {
return false;
}

/**
* {@inheritDoc}
*/
public function clear($check) {
return false;
}

/**
* {@inheritDoc}
*/
public function clearGroup($group) {
return false;
}

}
19 changes: 16 additions & 3 deletions tests/TestCase/Cache/CacheTest.php
Expand Up @@ -69,6 +69,19 @@ public function testNonFatalErrorsWithCachedisable() {
Cache::delete('no_save', 'tests');
}

/**
* Check that a null instance is returned from engine() when caching is disabled.
*
* @return void
*/
public function testNullEngineWhenCacheDisable() {
$this->_configCache();
Cache::disable();

$result = Cache::engine('tests');
$this->assertInstanceOf('Cake\Cache\Engine\NullEngine', $result);
}

/**
* test configuring CacheEngines in App/libs
*
Expand Down Expand Up @@ -436,7 +449,7 @@ public function testCacheDisable() {

Cache::disable();

$this->assertFalse(Cache::write('key_2', 'hello', 'test_cache_disable_1'));
$this->assertNull(Cache::write('key_2', 'hello', 'test_cache_disable_1'));
$this->assertFalse(Cache::read('key_2', 'test_cache_disable_1'));

Cache::enable();
Expand All @@ -451,7 +464,7 @@ public function testCacheDisable() {
'path' => TMP . 'tests'
]);

$this->assertFalse(Cache::write('key_4', 'hello', 'test_cache_disable_2'));
$this->assertNull(Cache::write('key_4', 'hello', 'test_cache_disable_2'));
$this->assertFalse(Cache::read('key_4', 'test_cache_disable_2'));

Cache::enable();
Expand All @@ -460,7 +473,7 @@ public function testCacheDisable() {
$this->assertSame(Cache::read('key_5', 'test_cache_disable_2'), 'hello');

Cache::disable();
$this->assertFalse(Cache::write('key_6', 'hello', 'test_cache_disable_2'));
$this->assertNull(Cache::write('key_6', 'hello', 'test_cache_disable_2'));
$this->assertFalse(Cache::read('key_6', 'test_cache_disable_2'));

Cache::enable();
Expand Down

0 comments on commit 05d8a19

Please sign in to comment.