Skip to content

Commit

Permalink
Making cacheAction array settings use action names instead of url pat…
Browse files Browse the repository at this point in the history
…hs. This removes the ability to set different durations for specific passed arguments. However, makes passed args, named params, and querystring params all behave the same in regard to cacheAction. Test cases updated.
  • Loading branch information
markstory committed Dec 29, 2009
1 parent 45a3eb2 commit 6749e11
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 38 deletions.
20 changes: 7 additions & 13 deletions cake/libs/view/helpers/cache.php
Expand Up @@ -70,19 +70,15 @@ function cache($file, $out, $cache = false) {
$check = Inflector::slug(Router::reverse($this->params));
$base = trim(str_replace('/', '_', $this->base), '_');
$check = trim(str_replace($base, '', $check), '_');
$match = $check;

$keys = str_replace('/', '_', array_keys($this->cacheAction));
$found = array_keys($this->cacheAction);
$keys = array_keys($this->cacheAction);
$index = null;
$count = 0;

foreach ($keys as $key => $value) {
if (strpos($check, rtrim($value, '_')) !== false) {
$index = $found[$count];
foreach ($keys as $action) {
if ($action == $this->params['action']) {
$index = $action;
break;
}
$count++;
}

if (!isset($index) && $this->action == 'index') {
Expand All @@ -92,19 +88,17 @@ function cache($file, $out, $cache = false) {
$options = $this->cacheAction;
if (isset($this->cacheAction[$index])) {
if (is_array($this->cacheAction[$index])) {
$options = array_merge(array('duration'=> 0, 'callbacks' => false), $this->cacheAction[$index]);
$options = array_merge(array('duration' => 0, 'callbacks' => false), $this->cacheAction[$index]);
} else {
$cacheTime = $this->cacheAction[$index];
}
}

if (array_key_exists('duration', $options)) {
if (isset($options['duration'])) {
$cacheTime = $options['duration'];
}
if (array_key_exists('callbacks', $options)) {
if (isset($options['callbacks'])) {
$useCallbacks = $options['callbacks'];
}

} else {
$cacheTime = $this->cacheAction;
}
Expand Down
47 changes: 22 additions & 25 deletions cake/tests/cases/libs/view/helpers/cache.test.php
Expand Up @@ -17,9 +17,6 @@
* @since CakePHP(tm) v 1.2.0.4206
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
}
App::import('Core', array('Controller', 'Model', 'View'));
App::import('Helper', 'Cache');

Expand Down Expand Up @@ -344,7 +341,7 @@ function testCacheActionArray() {

$this->Controller->cache_parsing();
$this->Controller->cacheAction = array(
'cache_parsing/' => 21600
'cache_parsing' => 21600
);
$this->Controller->here = '/cacheTest/cache_parsing';
$this->Controller->action = 'cache_parsing';
Expand All @@ -365,13 +362,13 @@ function testCacheActionArray() {
'controller' => 'cache_test',
'action' => 'cache_parsing',
'url' => array(),
'pass' => array(33),
'pass' => array(),
'named' => array()
);
$this->Controller->cacheAction = array(
'cache_parsing/33' => 21600
'some_other_action' => 21600
);
$this->Controller->here = '/cacheTest/cache_parsing/33';
$this->Controller->here = '/cacheTest/cache_parsing';
$this->Controller->action = 'cache_parsing';

$View = new View($this->Controller);
Expand All @@ -380,22 +377,32 @@ function testCacheActionArray() {
$this->assertNoPattern('/cake:nocache/', $result);
$this->assertNoPattern('/php echo/', $result);

$filename = CACHE . 'views' . DS . 'cachetest_cache_parsing_33.php';
$this->assertTrue(file_exists($filename));
@unlink($filename);
$filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
$this->assertFalse(file_exists($filename));
}

/**
* test that custom routes are respected when generating cache files.
*
* @return void
*/
function testCacheWithCustomRoutes() {
Router::reload();
Router::connect('/:lang/:controller/:action/*', array(), array('lang' => '[a-z]{3}'));

$this->Controller->cache_parsing();
$this->Controller->params = array(
'lang' => 'en',
'controller' => 'cache_test',
'action' => 'cache_parsing',
'url' => array(),
'pass' => array(),
'named' => array()
);
$this->Controller->cacheAction = array(
'cache_parsing/33' => 21600
'cache_parsing' => 21600
);
$this->Controller->here = '/cacheTest/cache_parsing';
$this->Controller->here = '/en/cache_test/cache_parsing';
$this->Controller->action = 'cache_parsing';

$View = new View($this->Controller);
Expand All @@ -404,19 +411,9 @@ function testCacheActionArray() {
$this->assertNoPattern('/cake:nocache/', $result);
$this->assertNoPattern('/php echo/', $result);

$filename = CACHE . 'views' . DS . 'cachetest_cache_parsing.php';
$this->assertFalse(file_exists($filename));
}

/**
* test that custom routes are respected when generating cache files.
*
* @return void
*/
function testCacheWithCustomRoutes() {
Router::reload();
Router::connect('/:lang/:controller/:action/*', array(), array('lang' => '[a-z]{3}'));

$filename = CACHE . 'views' . DS . 'en_cache_test_cache_parsing.php';
$this->assertTrue(file_exists($filename));
@unlink($filename);
}
/**
* testCacheEmptySections method
Expand Down

0 comments on commit 6749e11

Please sign in to comment.