8000 add filesystem cache (adapter|simple) prune method · symfony/symfony@41d099e · GitHub
[go: up one dir, main page]

Skip to content

Commit 41d099e

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

File tree

3 files changed

+137
-0
lines changed

3 files changed

+137
-0
lines changed

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

Lines changed: 57 additions & 0 deletions
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

Lines changed: 52 additions & 0 deletions
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_g A3DB et_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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Cache\Traits;
1313

1414
use Symfony\Component\Cache\Exception\CacheException;
15+
use Symfony\Component\VarDumper\VarDumper;
1516

1617
/**
1718
* @author Nicolas Grekas <p@tchwork.com>
@@ -22,6 +23,33 @@ trait FilesystemTrait
2223
{
2324
use FilesystemCommonTrait;
2425

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

0 commit comments

Comments
 (0)
0