Skip to content

Commit 8a81903

Browse files
committed
Fix FileEngine not clearing keys when groups are used.
Fixes cakephp#3930
1 parent 17b2538 commit 8a81903

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

lib/Cake/Cache/Engine/FileEngine.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,28 @@ public function clear($check) {
223223
if (!$this->_init) {
224224
return false;
225225
}
226-
$dir = dir($this->settings['path']);
226+
$threshold = $now = false;
227227
if ($check) {
228228
$now = time();
229229
$threshold = $now - $this->settings['duration'];
230230
}
231+
$this->_clearDirectory($this->settings['path'], $now, $threshold);
232+
foreach ($this->settings['groups'] as $group) {
233+
$this->_clearDirectory($this->settings['path'] . $group . DS, $now, $threshold);
234+
}
235+
return true;
236+
}
237+
238+
/**
239+
* Used to clear a directory of matching files.
240+
*
241+
* @param string $path The path to search.
242+
* @param integer $now The current timestamp
243+
* @param integer $threshold Any file not modified after this value will be deleted.
244+
* @return void
245+
*/
246+
protected function _clearDirectory($path, $now, $threshold) {
247+
$dir = dir($path);
231248
$prefixLength = strlen($this->settings['prefix']);
232249
while (($entry = $dir->read()) !== false) {
233250
if (substr($entry, 0, $prefixLength) !== $this->settings['prefix']) {
@@ -236,7 +253,7 @@ public function clear($check) {
236253
if ($this->_setKey($entry) === false) {
237254
continue;
238255
}
239-
if ($check) {
256+
if ($threshold) {
240257
$mtime = $this->_File->getMTime();
241258

242259
if ($mtime > $threshold) {
@@ -256,7 +273,6 @@ public function clear($check) {
256273
}
257274
}
258275
$dir->close();
259-
return true;
260276
}
261277

262278
/**

lib/Cake/Test/Case/Cache/Engine/FileEngineTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,10 @@ public function testClearWithGroups() {
267267
'duration' => DAY,
268268
'groups' => array('short')
269269
));
270-
$engine->write('test_key', 'it works', DAY);
270+
$key = 'cake_test_test_key';
271+
$engine->write($key, 'it works', DAY);
271272
$engine->clear(false);
272-
$this->assertFalse($engine->read('test_key'), 'Key should have been removed');
273+
$this->assertFalse($engine->read($key), 'Key should have been removed');
273274
}
274275

275276
/**

0 commit comments

Comments
 (0)