Cache: simplify cached content cleanup, improve tests
Signed-off-by: VirtualTam <virtualtam@flibidi.net>
This commit is contained in:
parent
01e48f269d
commit
aedd62e2b8
2 changed files with 23 additions and 15 deletions
|
@ -7,26 +7,18 @@
|
||||||
* Purges all cached pages
|
* Purges all cached pages
|
||||||
*
|
*
|
||||||
* @param string $pageCacheDir page cache directory
|
* @param string $pageCacheDir page cache directory
|
||||||
|
*
|
||||||
|
* @return mixed an error string if the directory is missing
|
||||||
*/
|
*/
|
||||||
function purgeCachedPages($pageCacheDir)
|
function purgeCachedPages($pageCacheDir)
|
||||||
{
|
{
|
||||||
if (! is_dir($pageCacheDir)) {
|
if (! is_dir($pageCacheDir)) {
|
||||||
return;
|
$error = 'Cannot purge '.$pageCacheDir.': no directory';
|
||||||
|
error_log($error);
|
||||||
|
return $error;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: check write access to the cache directory
|
array_map('unlink', glob($pageCacheDir.'/*.cache'));
|
||||||
|
|
||||||
$handler = opendir($pageCacheDir);
|
|
||||||
if ($handler == false) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (($filename = readdir($handler)) !== false) {
|
|
||||||
if (endsWith($filename, '.cache')) {
|
|
||||||
unlink($pageCacheDir.'/'.$filename);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
closedir($handler);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -27,11 +27,14 @@ public function setUp()
|
||||||
{
|
{
|
||||||
if (! is_dir(self::$testCacheDir)) {
|
if (! is_dir(self::$testCacheDir)) {
|
||||||
mkdir(self::$testCacheDir);
|
mkdir(self::$testCacheDir);
|
||||||
|
} else {
|
||||||
|
array_map('unlink', glob(self::$testCacheDir.'/*'));
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (self::$pages as $page) {
|
foreach (self::$pages as $page) {
|
||||||
file_put_contents(self::$testCacheDir.'/'.$page.'.cache', $page);
|
file_put_contents(self::$testCacheDir.'/'.$page.'.cache', $page);
|
||||||
}
|
}
|
||||||
|
file_put_contents(self::$testCacheDir.'/intru.der', 'ShouldNotBeThere');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -42,7 +45,20 @@ public function testPurgeCachedPages()
|
||||||
purgeCachedPages(self::$testCacheDir);
|
purgeCachedPages(self::$testCacheDir);
|
||||||
foreach (self::$pages as $page) {
|
foreach (self::$pages as $page) {
|
||||||
$this->assertFileNotExists(self::$testCacheDir.'/'.$page.'.cache');
|
$this->assertFileNotExists(self::$testCacheDir.'/'.$page.'.cache');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->assertFileExists(self::$testCacheDir.'/intru.der');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Purge cached pages - missing directory
|
||||||
|
*/
|
||||||
|
public function testPurgeCachedPagesMissingDir()
|
||||||
|
{
|
||||||
|
$this->assertEquals(
|
||||||
|
'Cannot purge tests/dummycache_missing: no directory',
|
||||||
|
purgeCachedPages(self::$testCacheDir.'_missing')
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue