Skip to content

Commit

Permalink
Update Cake 2 configs to init Caching later.
Browse files Browse the repository at this point in the history
Addresses the question raised in #7.
  • Loading branch information
beporter committed Jun 18, 2015
1 parent 849bd15 commit 8367acd
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 31 deletions.
9 changes: 9 additions & 0 deletions app-cake2/Config/core-vagrant.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@
),
),

/**
* In development mode, caches should expire quickly.
*/
'Cache' => array(
'default' => array(
'duration' => '+2 minutes',
),
),

/**
* Vagrant environment hints.
*
Expand Down
84 changes: 53 additions & 31 deletions app-cake2/Config/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
));

/**
Expand Down Expand Up @@ -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);
13 changes: 13 additions & 0 deletions app-cake2/View/Pages/cache_config.ctp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h2>Dump of all Cache Configs</h2>

<?php
// Make sure debug is on so output is actually printed.
if (!Configure::read('debug')) {
Configure::write('debug', 1);
}

// Loop over all defined Caches and dump the config for each.
foreach (Cache::configured() as $c) {
echo '<h3>' . h($c) . '</h3>';
debug(Cache::config($c));
}
7 changes: 7 additions & 0 deletions app-cake2/View/Pages/home.ctp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ endif;
echo '<span class="notice success">';
echo __d('cake_dev', 'The %s is being used for core caching. To change the config edit %s', '<em>'. $settings['engine'] . 'Engine</em>', 'APP/Config/core.php');
echo '</span>';
echo '<span>';
echo $this->Html->link('View Cache config dumps', array(
'controller' => 'pages',
'action' => 'display',
'cache_config',
));
echo '</span>';
else:
echo '<span class="notice">';
echo __d('cake_dev', 'Your cache is NOT working. Please check the settings in %s', 'APP/Config/core.php');
Expand Down

0 comments on commit 8367acd

Please sign in to comment.