Refine cache purgring
This commit is contained in:
@@ -12,11 +12,12 @@ namespace dokuwiki\plugin\luxtools;
|
||||
class CacheInvalidation
|
||||
{
|
||||
/**
|
||||
* Purge the complete DokuWiki cache directory.
|
||||
* Purge the DokuWiki cache directory, preserving the luxtools subfolder.
|
||||
*
|
||||
* This removes all files and subdirectories inside the configured
|
||||
* cache directory ($conf['cachedir']). It is equivalent to running
|
||||
* `rm -rf data/cache/*` on the server.
|
||||
* Removes all files and subdirectories inside $conf['cachedir'] except
|
||||
* the `luxtools/` subdirectory, which contains plugin-specific caches
|
||||
* (thumbnails, pagelink mapping) that are expensive to rebuild and are
|
||||
* managed separately via {@see purgePagelinks()} and {@see purgeThumbs()}.
|
||||
*
|
||||
* @return int Number of removed entries.
|
||||
*/
|
||||
@@ -29,17 +30,90 @@ class CacheInvalidation
|
||||
return 0;
|
||||
}
|
||||
|
||||
return self::removeDirectoryEntries($cacheDir);
|
||||
return self::removeDirectoryEntries($cacheDir, ['luxtools']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge the luxtools pagelink cache file.
|
||||
*
|
||||
* Deletes {@see PageLink::CACHE_FILE} from the luxtools cache sub-directory
|
||||
* ($conf['cachedir']/luxtools/pagelink_cache.json). The file is rebuilt
|
||||
* automatically on the next page request that needs it.
|
||||
*
|
||||
* @return int 1 if the file was removed, 0 otherwise.
|
||||
*/
|
||||
public static function purgePagelinks(): int
|
||||
{
|
||||
global $conf;
|
||||
|
||||
$cacheDir = rtrim((string)($conf['cachedir'] ?? ''), '/');
|
||||
if ($cacheDir === '') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$file = $cacheDir . '/luxtools/pagelink_cache.json';
|
||||
if (!file_exists($file)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return @unlink($file) ? 1 : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge the luxtools thumbnail cache directory.
|
||||
*
|
||||
* Removes all files and sub-directories inside
|
||||
* $conf['cachedir']/luxtools/thumbs. Thumbnails are regenerated
|
||||
* on demand by file.php.
|
||||
*
|
||||
* @return int Number of removed entries.
|
||||
*/
|
||||
public static function purgeThumbs(): int
|
||||
{
|
||||
global $conf;
|
||||
|
||||
$cacheDir = rtrim((string)($conf['cachedir'] ?? ''), '/');
|
||||
if ($cacheDir === '') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$thumbsDir = $cacheDir . '/luxtools/thumbs';
|
||||
if (!is_dir($thumbsDir)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return self::removeDirectoryEntries($thumbsDir);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a selective cache purge.
|
||||
*
|
||||
* Always purges the DokuWiki cache (skipping the luxtools subfolder).
|
||||
* Optionally also purges the pagelinks cache and/or the thumbnail cache.
|
||||
*
|
||||
* @param bool $pagelinks Whether to also purge the pagelink cache.
|
||||
* @param bool $thumbs Whether to also purge the thumbnail cache.
|
||||
* @return array{dokuwiki: int, pagelinks: int|null, thumbs: int|null}
|
||||
* Removed-entry counts per component; pagelinks/thumbs are null when not purged.
|
||||
*/
|
||||
public static function purgeSelected(bool $pagelinks, bool $thumbs): array
|
||||
{
|
||||
return [
|
||||
'dokuwiki' => self::purgeAll(),
|
||||
'pagelinks' => $pagelinks ? self::purgePagelinks() : null,
|
||||
'thumbs' => $thumbs ? self::purgeThumbs() : null,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all files and subdirectories inside a directory
|
||||
* without removing the directory itself.
|
||||
*
|
||||
* @param string $directory
|
||||
* @param string $directory
|
||||
* @param string[] $skip Entry names (not paths) to leave untouched.
|
||||
* @return int Number of removed files/directories.
|
||||
*/
|
||||
public static function removeDirectoryEntries(string $directory): int
|
||||
public static function removeDirectoryEntries(string $directory, array $skip = []): int
|
||||
{
|
||||
if ($directory === '' || !is_dir($directory) || !is_readable($directory)) {
|
||||
return 0;
|
||||
@@ -51,6 +125,7 @@ class CacheInvalidation
|
||||
$removed = 0;
|
||||
foreach ($entries as $entry) {
|
||||
if ($entry === '.' || $entry === '..') continue;
|
||||
if ($skip !== [] && in_array($entry, $skip, true)) continue;
|
||||
|
||||
$path = $directory . '/' . $entry;
|
||||
if (is_dir($path) && !is_link($path)) {
|
||||
|
||||
Reference in New Issue
Block a user