Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Making FileEngine not greedily clear files in a directory that may be…
…long to another cache configuration. Tests added. Fixes #754
  • Loading branch information
markstory committed May 27, 2010
1 parent cd255d5 commit 8b6c974
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cake/libs/cache/file.php
Expand Up @@ -209,7 +209,11 @@ function clear($check) {
$now = time();
$threshold = $now - $this->settings['duration'];
}
$prefixLength = strlen($this->settings['prefix']);
while (($entry = $dir->read()) !== false) {
if (substr($entry, 0, $prefixLength) !== $this->settings['prefix']) {
continue;
}
if ($this->_setKey($entry) === false) {
continue;
}
Expand Down
28 changes: 28 additions & 0 deletions cake/tests/cases/libs/cache/file.test.php
Expand Up @@ -273,6 +273,34 @@ function testClear() {
Cache::config('default', array('engine' => 'File', 'path' => CACHE));
}

/**
* test that clear() doesn't wipe files not in the current engine's prefix.
*
* @return void
*/
function testClearWithPrefixes() {
$FileOne =& new FileEngine();
$FileOne->init(array(
'prefix' => 'prefix_one_',
'duration' => DAY
));
$FileTwo =& new FileEngine();
$FileTwo->init(array(
'prefix' => 'prefix_two_',
'duration' => DAY
));

$data1 = $data2 = $expected = 'content to cache';
$FileOne->write('key_one', $data1, DAY);
$FileTwo->write('key_two', $data2, DAY);

$this->assertEqual($FileOne->read('key_one'), $expected);
$this->assertEqual($FileTwo->read('key_two'), $expected);

$FileOne->clear(false);
$this->assertEqual($FileTwo->read('key_two'), $expected, 'secondary config was cleared by accident.');
}

/**
* testKeyPath method
*
Expand Down

0 comments on commit 8b6c974

Please sign in to comment.