Skip to content

Commit d414259

Browse files
committed
add filesystem cache (adapter|simple) prune method
1 parent 0bcc5af commit d414259

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

src/Symfony/Component/Cache/Tests/Adapter/FilesystemAdapterTest.php

+57
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,61 @@ public static function rmdir($dir)
4949
}
5050
rmdir($dir);
5151
}
52+
53+
public function testPrune()
54+
{
55+
if (isset($this->skippedTests[__FUNCTION__])) {
56+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
57+
58+
return;
59+
}
60+
61+
$cache = $this->createCachePool();
62+
$this->assertSame(0, $cache->prune());
63+
$this->assertCount(2, scandir(sys_get_temp_dir().'/symfony-cache'));
64+
65+
$items = array(
66+
'foo' => 15,
67+
'bar' => 30,
68+
'baz' => 60,
69+
'qux' => 120,
70+
'quux' => 240,
71+
'corge' => 480,
72+
'grault' => 960,
73+
'garply' => 1920,
74+
'waldo' => 3840,
75+
'fred' => 7680,
76+
'plugh' => 15360,
77+
);
78+
79+
foreach ($items as $name => $intervalSpecSeconds) {
80+
$item = $cache->getItem($name);
81+
$item->expiresAfter(new \DateInterval(sprintf('PT%dS', $intervalSpecSeconds)));
82+
$item->set(sprintf('item-%s-value', $name));
83+
$cache->save($item);
84+
}
85+
86+
$this->assertGreaterThan(2, scandir(sys_get_temp_dir().'/symfony-cache'));
87+
$this->assertSame(0, $cache->prune(time() - 100));
88+
$this->assertSame(0, $cache->prune());
89+
$this->assertSame(2, $cache->prune(time() + 45));
90+
$this->assertSame(2, $cache->prune(time() + 180));
91+
$this->assertSame(1, $cache->prune(time() + 300));
92+
$this->assertSame(0, $cache->prune());
93+
$this->assertSame(3, $cache->prune(time() + 3000));
94+
$this->assertSame(0, $cache->prune());
95+
$this->assertSame(2, $cache->prune(time() + 10000));
96+
$this->assertCount(3, scandir(sys_get_temp_dir().'/symfony-cache'));
97+
$this->assertSame(1, $cache->prune(time() + 100000));
98+
$this->assertCount(2, scandir(sys_get_temp_dir().'/symfony-cache'));
99+
100+
foreach ($items as $name => $intervalSpecSeconds) {
101+
$this->assertFalse($cache->hasItem($name), sprintf('Cache should not have item "%s" as it should be pruned.', $name));
102+
}
103+
104+
$item = $cache->getItem('xyzzy');
105+
$item->set('item-xyzzy-value');
106+
$cache->save($item);
107+
$this->assertSame(0, $cache->prune());
108+
}
52109
}

src/Symfony/Component/Cache/Tests/Simple/FilesystemCacheTest.php

+52
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,56 @@ public function createSimpleCache($defaultLifetime = 0)
2222
{
2323
return new FilesystemCache('', $defaultLifetime);
2424
}
25+
26+
public function testPrune()
27+
{
28+
if (isset($this->skippedTests[__FUNCTION__])) {
29+
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
30+
31+
return;
32+
}
33+
34+
$cache = $this->createSimpleCache();
35+
$this->assertSame(0, $cache->prune());
36+
$this->assertCount(2, scandir(sys_get_temp_dir().'/symfony-cache'));
37+
38+
$items = array(
39+
'foo' => 15,
40+
'bar' => 30,
41+
'baz' => 60,
42+
'qux' => 120,
43+
'quux' => 240,
44+
'corge' => 480,
45+
'grault' => 960,
46+
'garply' => 1920,
47+
'waldo' => 3840,
48+
'fred' => 7680,
49+
'plugh' => 15360,
50+
);
51+
52+
foreach ($items as $name => $intervalSpecSeconds) {
53+
$cache->set($name, sprintf('item-%s-value', $name), new \DateInterval(sprintf('PT%dS', $intervalSpecSeconds)));
54+
}
55+
56+
$this->assertGreaterThan(2, scandir(sys_get_temp_dir().'/symfony-cache'));
57+
$this->assertSame(0, $cache->prune(time() - 100));
58+
$this->assertSame(0, $cache->prune());
59+
$this->assertSame(2, $cache->prune(time() + 45));
60+
$this->assertSame(2, $cache->prune(time() + 180));
61+
$this->assertSame(1, $cache->prune(time() + 300));
62+
$this->assertSame(0, $cache->prune());
63+
$this->assertSame(3, $cache->prune(time() + 3000));
64+
$this->assertSame(0, $cache->prune());
65+
$this->assertSame(2, $cache->prune(time() + 10000));
66+
$this->assertCount(3, scandir(sys_get_temp_dir().'/symfony-cache'));
67+
$this->assertSame(1, $cache->prune(time() + 100000));
68+
$this->assertCount(2, scandir(sys_get_temp_dir().'/symfony-cache'));
69+
70+
foreach ($items as $name => $intervalSpecSeconds) {
71+
$this->assertFalse($cache->has($name), sprintf('Cache should not have item "%s" as it should be pruned.', $name));
72+
}
73+
74+
$cache->set('xyzzy', 'item-xyzzy-value');
75+
$this->assertSame(0, $cache->prune());
76+
}
2577
}

src/Symfony/Component/Cache/Traits/FilesystemTrait.php

+27
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,33 @@ trait FilesystemTrait
2222
{
2323
use FilesystemCommonTrait;
2424

25+
/**
26+
* @param int|null $time
27+
*
28+
* @return int
29+
*/
30+
public function prune($time = null)
31+
{
32+
$time = $time ?: time();
33+
$i = 0;
34+
35+
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->directory, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::CHILD_FIRST, \RecursiveIteratorIterator::CATCH_GET_CHILD) as $file) {
36+
// remove empty directories
37+
if ($file->isDir() && 2 === count(scandir($file->getPathname()))) {
38+
@rmdir(rtrim($file->getPathname(), '/.'));
39+
40+
continue;
41+
}
42+
43+
// remove items older than passed (or current) time
44+
if ($time >= $file->getMTime() && @unlink($file) && !file_exists($file)) {
45+
++$i;
46+
}
47+
}
48+
49+
return $i;
50+
}
51+
2552
/**
2653
* {@inheritdoc}
2754
*/

0 commit comments

Comments
 (0)