Skip to content

Commit

Permalink
Remove Cache.disable from Configure.
Browse files Browse the repository at this point in the history
Give it a set of named methods on Cache. This configure value always
felt out of place and fits much better with Cache's feature set.
  • Loading branch information
markstory committed Aug 17, 2013
1 parent b1a2f86 commit 7cf7901
Show file tree
Hide file tree
Showing 19 changed files with 79 additions and 50 deletions.
2 changes: 1 addition & 1 deletion App/Config/cache.php
Expand Up @@ -21,7 +21,7 @@
* Turn off all caching application-wide.
*
*/
//Configure::write('Cache.disable', true);
// Cache::disable();

/**
* Enable cache checking.
Expand Down
44 changes: 41 additions & 3 deletions lib/Cake/Cache/Cache.php
Expand Up @@ -82,6 +82,13 @@
*/
class Cache {

/**
* Flag for tracking whether or not caching is enabled.
*
* @var boolean
*/
protected static $_enabled = true;

/**
* Configuraiton backup.
*
Expand Down Expand Up @@ -221,8 +228,8 @@ public static function configured() {
/**
* Drops a constructed cache engine.
*
* The engine's configuration will remain in Configure. If you wish to re-configure a
* cache engine you should drop it, change configuration and then re-use it.
* If you wish to re-configure a cache engine you should drop it,
* change configuration and then re-use it.
*
* @param string $config A currently configured cache config you wish to remove.
* @return boolean success of the removal, returns false when the config does not exist.
Expand All @@ -245,7 +252,7 @@ public static function drop($config) {
* @return Cake\Cache\Engine
*/
public static function engine($config) {
if (Configure::read('Cache.disable')) {
if (!static::$_enabled) {
return false;
}

Expand Down Expand Up @@ -594,4 +601,35 @@ public static function groupConfigs($group = null) {
throw new Error\Exception(__d('cake_dev', 'Invalid cache group %s', $group));
}

/**
* Re-enable caching.
*
* If caching has been disabled with Cache::disable() this method will reverse that effect.
*
* @return void
*/
public static function enable() {
static::$_enabled = true;
}

/**
* Disable caching.
*
* When disabled all cache operations will return null.
*
* @return void
*/
public static function disable() {
static::$_enabled = false;
}

/**
* Check whether or not caching is enabled.
*
* @return boolean
*/
public static function enabled() {
return static::$_enabled;
}

}
3 changes: 2 additions & 1 deletion lib/Cake/Console/Command/BakeShell.php
Expand Up @@ -15,6 +15,7 @@
namespace Cake\Console\Command;

use Cake\Console\Shell;
use Cake\Cache\Cache;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Model\Model;
Expand Down Expand Up @@ -54,7 +55,7 @@ class BakeShell extends Shell {
public function startup() {
parent::startup();
Configure::write('debug', 2);
Configure::write('Cache.disable', 1);
Cache::disable();

$task = Inflector::classify($this->command);
if (isset($this->{$task}) && !in_array($task, array('Project', 'DbConfig'))) {
Expand Down
8 changes: 4 additions & 4 deletions lib/Cake/Console/Command/SchemaShell.php
Expand Up @@ -15,6 +15,7 @@
namespace Cake\Console\Command;

use Cake\Console\Shell;
use Cake\Cache\Cache;
use Cake\Core\Configure;
use Cake\Model\ConnectionManager;
use Cake\Model\Schema;
Expand Down Expand Up @@ -60,7 +61,7 @@ public function startup() {

throw new \Cake\Error\Exception('Schema shell is not working at this time.');

Configure::write('Cache.disable', 1);
Cache::disable();

$name = $path = $connection = $plugin = null;
if (!empty($this->params['name'])) {
Expand Down Expand Up @@ -152,13 +153,12 @@ public function generate() {
}
}

$cacheDisable = Configure::read('Cache.disable');
Configure::write('Cache.disable', true);
Cache::enable();

$content = $this->Schema->read($options);
$content['file'] = $this->params['file'];

Configure::write('Cache.disable', $cacheDisable);
Cache::disable();

if (!empty($this->params['exclude']) && !empty($content)) {
$excluded = String::tokenize($this->params['exclude']);
Expand Down
3 changes: 2 additions & 1 deletion lib/Cake/Console/Command/Task/BakeTask.php
Expand Up @@ -19,6 +19,7 @@
namespace Cake\Console\Command\Task;

use Cake\Console\Shell;
use Cake\Cache\Cache;
use Cake\Core\Configure;

/**
Expand Down Expand Up @@ -57,7 +58,7 @@ class BakeTask extends Shell {
*/
public function startup() {
Configure::write('debug', 2);
Configure::write('Cache.disable', 1);
Cache::disable();
parent::startup();
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Console/Templates/skel/Config/cache.php
Expand Up @@ -20,7 +20,7 @@
* Turn off all caching application-wide.
*
*/
//Configure::write('Cache.disable', true);
// Cache::disable();

/**
* Enable cache checking.
Expand Down
10 changes: 3 additions & 7 deletions lib/Cake/Test/TestCase/BasicsTest.php
Expand Up @@ -19,6 +19,7 @@
*/
namespace Cake\Test\TestCase;

use Cake\Cache\Cache;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Log\Log;
Expand Down Expand Up @@ -281,17 +282,14 @@ public function testAm() {
* @return void
*/
public function testCache() {
$_cacheDisable = Configure::read('Cache.disable');
$this->skipIf($_cacheDisable, 'Cache is disabled, skipping cache() tests.');

Configure::write('Cache.disable', true);
Cache::disable();
$result = cache('basics_test', 'simple cache write');
$this->assertNull($result);

$result = cache('basics_test');
$this->assertNull($result);

Configure::write('Cache.disable', false);
Cache::enable();
$result = cache('basics_test', 'simple cache write');
$this->assertTrue((boolean)$result);
$this->assertTrue(file_exists(CACHE . 'basics_test'));
Expand All @@ -306,8 +304,6 @@ public function testCache() {
sleep(2);
$result = cache('basics_test', null, '+1 second');
$this->assertNull($result);

Configure::write('Cache.disable', $_cacheDisable);
}

/**
Expand Down
16 changes: 8 additions & 8 deletions lib/Cake/Test/TestCase/Cache/CacheTest.php
Expand Up @@ -35,7 +35,7 @@ class CacheTest extends TestCase {
*/
public function setUp() {
parent::setUp();
Configure::write('Cache.disable', false);
Cache::enable();
}

/**
Expand All @@ -62,7 +62,7 @@ protected function _configCache() {
* @return void
*/
public function testNonFatalErrorsWithCachedisable() {
Configure::write('Cache.disable', true);
Cache::disable();
$this->_configCache();

Cache::write('no_save', 'Noooo!', 'tests');
Expand Down Expand Up @@ -378,7 +378,7 @@ public function testWriteTriggerError() {
* @return void
*/
public function testCacheDisable() {
Configure::write('Cache.disable', false);
Cache::enable();
Cache::config('test_cache_disable_1', [
'engine' => 'File',
'path' => TMP . 'tests'
Expand All @@ -387,17 +387,17 @@ public function testCacheDisable() {
$this->assertTrue(Cache::write('key_1', 'hello', 'test_cache_disable_1'));
$this->assertSame(Cache::read('key_1', 'test_cache_disable_1'), 'hello');

Configure::write('Cache.disable', true);
Cache::disable();

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

Configure::write('Cache.disable', false);
Cache::enable();

$this->assertTrue(Cache::write('key_3', 'hello', 'test_cache_disable_1'));
$this->assertSame(Cache::read('key_3', 'test_cache_disable_1'), 'hello');

Configure::write('Cache.disable', true);
Cache::disable();
Cache::config('test_cache_disable_2', [
'engine' => 'File',
'path' => TMP . 'tests'
Expand All @@ -406,12 +406,12 @@ public function testCacheDisable() {
$this->assertFalse(Cache::write('key_4', 'hello', 'test_cache_disable_2'));
$this->assertFalse(Cache::read('key_4', 'test_cache_disable_2'));

Configure::write('Cache.disable', false);
Cache::enable();

$this->assertTrue(Cache::write('key_5', 'hello', 'test_cache_disable_2'));
$this->assertSame(Cache::read('key_5', 'test_cache_disable_2'), 'hello');

Configure::write('Cache.disable', true);
Cache::disable();

$this->assertFalse(Cache::write('key_6', 'hello', 'test_cache_disable_2'));
$this->assertFalse(Cache::read('key_6', 'test_cache_disable_2'));
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/TestCase/Cache/Engine/ApcEngineTest.php
Expand Up @@ -42,7 +42,7 @@ public function setUp() {
$this->skipIf(!ini_get('apc.enable_cli'), 'APC is not enabled for the CLI.');
}

Configure::write('Cache.disable', false);
Cache::enable();
Cache::config('apc', ['engine' => 'Apc', 'prefix' => 'cake_']);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/TestCase/Cache/Engine/FileEngineTest.php
Expand Up @@ -44,7 +44,7 @@ class FileEngineTest extends TestCase {
*/
public function setUp() {
parent::setUp();
Configure::write('Cache.disable', false);
Cache::enable();
Cache::config('file_test', [
'engine' => 'File',
'path' => TMP . 'tests',
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/TestCase/Cache/Engine/MemcacheEngineTest.php
Expand Up @@ -61,7 +61,7 @@ public function setUp() {
parent::setUp();
$this->skipIf(!class_exists('Memcache'), 'Memcache is not installed or configured properly.');

Configure::write('Cache.disable', false);
Cache::enable();
Cache::config('memcache', array(
'className' => 'Memcache',
'prefix' => 'cake_',
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/TestCase/Cache/Engine/RedisEngineTest.php
Expand Up @@ -35,7 +35,7 @@ public function setUp() {
parent::setUp();
$this->skipIf(!class_exists('Redis'), 'Redis is not installed or configured properly.');

Configure::write('Cache.disable', false);
Cache::enable();
Cache::config('redis', array(
'engine' => 'Cake\Cache\Engine\RedisEngine',
'prefix' => 'cake_',
Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/TestCase/Cache/Engine/WincacheEngineTest.php
Expand Up @@ -38,7 +38,7 @@ class WincacheEngineTest extends TestCase {
public function setUp() {
parent::setUp();
$this->skipIf(!function_exists('wincache_ucache_set'), 'Wincache is not installed or configured properly.');
Configure::write('Cache.disable', false);
Cache::enable();
Cache::config('wincache', ['engine' => 'Wincache', 'prefix' => 'cake_']);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Cake/Test/TestCase/Cache/Engine/XcacheEngineTest.php
Expand Up @@ -39,7 +39,7 @@ public function setUp() {
if (!function_exists('xcache_set')) {
$this->markTestSkipped('Xcache is not installed or configured properly');
}
Configure::write('Cache.disable', false);
Cache::enable();
Cache::config('xcache', ['engine' => 'Xcache', 'prefix' => 'cake_']);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/Cake/Test/TestCase/Core/ConfigureTest.php
Expand Up @@ -42,7 +42,7 @@ class ConfigureTest extends TestCase {
*/
public function setUp() {
parent::setUp();
Configure::write('Cache.disable', true);
Cache::disable();
App::build();
App::objects('Plugin', null, true);
}
Expand Down Expand Up @@ -339,7 +339,7 @@ public function testLoadPlugin() {
* @return void
*/
public function testStoreAndRestore() {
Configure::write('Cache.disable', false);
Cache::enable();
Cache::config('configure', [
'className' => 'File',
'path' => TMP . 'tests'
Expand All @@ -364,7 +364,7 @@ public function testStoreAndRestore() {
* @return void
*/
public function testStoreAndRestoreWithData() {
Configure::write('Cache.disable', false);
Cache::enable();
Cache::config('configure', [
'className' => 'File',
'path' => TMP . 'tests'
Expand Down
9 changes: 1 addition & 8 deletions lib/Cake/Test/TestCase/Model/ModelWriteTest.php
Expand Up @@ -260,12 +260,8 @@ public function testAllowSimulatedFields() {
* @return void
*/
public function testCacheClearOnSave() {
$_back = array(
'check' => Configure::read('Cache.check'),
'disable' => Configure::read('Cache.disable'),
);
Configure::write('Cache.check', true);
Configure::write('Cache.disable', false);
Cache::enable();

$this->loadFixtures('OverallFavorite');
$OverallFavorite = new OverallFavorite();
Expand All @@ -286,9 +282,6 @@ public function testCacheClearOnSave() {

$this->assertFalse(file_exists(CACHE . 'views/some_dir_overallfavorites_index.php'));
$this->assertFalse(file_exists(CACHE . 'views/some_dir_overall_favorites_index.php'));

Configure::write('Cache.check', $_back['check']);
Configure::write('Cache.disable', $_back['disable']);
}

/**
Expand Down
8 changes: 3 additions & 5 deletions lib/Cake/Test/TestCase/Routing/DispatcherTest.php
Expand Up @@ -18,6 +18,7 @@
*/
namespace Cake\Test\TestCase\Routing;

use Cake\Cache\Cache;
use Cake\Controller\Controller;
use Cake\Core\App;
use Cake\Core\Configure;
Expand Down Expand Up @@ -266,17 +267,13 @@ public function setUp() {
parent::setUp();
$_GET = array();

$this->_app = Configure::read('App');
Configure::write('App.base', false);
Configure::write('App.baseUrl', false);
Configure::write('App.dir', 'app');
Configure::write('App.webroot', 'webroot');
Configure::write('App.namespace', 'TestApp');

$this->_cache = Configure::read('Cache');
Configure::write('Cache.disable', true);

$this->_debug = Configure::read('debug');
Cache::disable();

App::build();
App::objects('Plugin', null, false);
Expand Down Expand Up @@ -944,6 +941,7 @@ public static function cacheActionProvider() {
* @return void
*/
public function testFullPageCachingDispatch($url) {
Cache::enable();
Configure::write('Cache.disable', false);
Configure::write('Cache.check', true);
Configure::write('debug', 2);
Expand Down

0 comments on commit 7cf7901

Please sign in to comment.