From 8367acde560dde2bb322a713f36e3699f0510e43 Mon Sep 17 00:00:00 2001 From: Brian Porter Date: Thu, 18 Jun 2015 14:47:37 -0500 Subject: [PATCH] Update Cake 2 configs to init Caching later. Addresses the question raised in #7. --- app-cake2/Config/core-vagrant.php | 9 +++ app-cake2/Config/core.php | 84 +++++++++++++++++---------- app-cake2/View/Pages/cache_config.ctp | 13 +++++ app-cake2/View/Pages/home.ctp | 7 +++ 4 files changed, 82 insertions(+), 31 deletions(-) create mode 100644 app-cake2/View/Pages/cache_config.ctp diff --git a/app-cake2/Config/core-vagrant.php b/app-cake2/Config/core-vagrant.php index 2dd7928..38c1832 100644 --- a/app-cake2/Config/core-vagrant.php +++ b/app-cake2/Config/core-vagrant.php @@ -57,6 +57,15 @@ ), ), + /** + * In development mode, caches should expire quickly. + */ + 'Cache' => array( + 'default' => array( + 'duration' => '+2 minutes', + ), + ), + /** * Vagrant environment hints. * diff --git a/app-cake2/Config/core.php b/app-cake2/Config/core.php index 8e4bada..5ac64e8 100755 --- a/app-cake2/Config/core.php +++ b/app-cake2/Config/core.php @@ -328,40 +328,43 @@ * Note: 'default' and other application caches should be configured in app/Config/bootstrap.php. * Please check the comments in bootstrap.php for more info on the cache engines available * and their settings. + * + * Converted to Cake 3 style setup. + * + * The Cache configurations will actually be initialized later after + * environment-specific configs are loaded. */ -$engine = 'File'; - -// In development mode, caches should expire quickly. -$duration = '+999 days'; -if (Configure::read('debug') > 0) { - $duration = '+10 seconds'; -} - -// Prefix each application on the same server with a different string, to avoid Memcache and APC conflicts. -$prefix = 'cake-2x_'; +Configure::write('Cache', array( + /** + * Provides baseline settings for each named config, + * each of which will be array_merge()d on top of this. + */ + 'default' => array( + 'engine' => 'File', // In Cake 3, this key name changes from `engine` to `className`. + 'path' => CACHE, + 'duration' => '+999 days', + 'prefix' => 'cake-2x_', + ), -/** - * Configure the cache used for general framework caching. Path information, - * object listings, and translation cache files are stored with this configuration. - */ -Cache::config('_cake_core_', array( - 'engine' => $engine, - 'prefix' => $prefix . 'cake_core_', - 'path' => CACHE . 'persistent' . DS, - 'serialize' => ($engine === 'File'), - 'duration' => $duration -)); + /** + * Configure the cache used for general framework caching. Path information, + * object listings, and translation cache files are stored with this configuration. + */ + '_cake_core_' => array( + 'prefix' => 'cake-2x_cake_core_', + 'path' => CACHE . 'persistent' . DS, + 'serialize' => true, + ), -/** - * Configure the cache for model and datasource caches. This cache configuration - * is used to store schema descriptions, and table listings in connections. - */ -Cache::config('_cake_model_', array( - 'engine' => $engine, - 'prefix' => $prefix . 'cake_model_', - 'path' => CACHE . 'models' . DS, - 'serialize' => ($engine === 'File'), - 'duration' => $duration + /** + * Configure the cache for model and datasource caches. This cache configuration + * is used to store schema descriptions, and table listings in connections. + */ + '_cake_model_' => array( + 'prefix' => 'cake-2x_cake_model_', + 'path' => CACHE . 'models' . DS, + 'serialize' => true, + ), )); /** @@ -473,3 +476,22 @@ if (is_readable(dirname(__FILE__) . DS . 'core-local.php')) { Configure::load('core-local'); } + + +/** + * Initialize Caching late in the process so it can be made environment-aware. + * + * Tries to match how Cake 3 approaches Cache configuration. + */ + +// Extract the defaults so we don't loop over them. +$cacheDefaults = Configure::read('Cache.default'); +Configure::delete('Cache.default'); + +// Load each named config, merging defaults. +foreach (Configure::read('Cache') as $name => $configs) { + Cache::config($name, array_merge($cacheDefaults, $configs)); +} + +// Clean up after ourselves. +unset($cacheDefaults); diff --git a/app-cake2/View/Pages/cache_config.ctp b/app-cake2/View/Pages/cache_config.ctp new file mode 100644 index 0000000..bb828cc --- /dev/null +++ b/app-cake2/View/Pages/cache_config.ctp @@ -0,0 +1,13 @@ +

Dump of all Cache Configs

+ +' . h($c) . ''; + debug(Cache::config($c)); +} diff --git a/app-cake2/View/Pages/home.ctp b/app-cake2/View/Pages/home.ctp index 4a78d80..0b89b5d 100755 --- a/app-cake2/View/Pages/home.ctp +++ b/app-cake2/View/Pages/home.ctp @@ -56,6 +56,13 @@ endif; echo ''; echo __d('cake_dev', 'The %s is being used for core caching. To change the config edit %s', ''. $settings['engine'] . 'Engine', 'APP/Config/core.php'); echo ''; + echo ''; + echo $this->Html->link('View Cache config dumps', array( + 'controller' => 'pages', + 'action' => 'display', + 'cache_config', + )); + echo ''; else: echo ''; echo __d('cake_dev', 'Your cache is NOT working. Please check the settings in %s', 'APP/Config/core.php');