Implement a more correct way to clear the cache

This commit is contained in:
Belle Aerni 2023-05-26 23:36:02 -07:00
parent 7f2269c385
commit 5896b3d150
2 changed files with 29 additions and 12 deletions

View file

@ -28,7 +28,7 @@ class AntCache
case 'auto':
if (extension_loaded('apcu') && apcu_enabled()) {
$this->cacheType = self::apcuCache;
$this->cacheKeyApcu = 'AntCMS_' . hash('md5', __DIR__);
$this->cacheKeyApcu = 'AntCMS_' . hash('md5', __DIR__) . '_';
} else {
$this->cacheType = self::fileCache;
}
@ -38,7 +38,7 @@ class AntCache
break;
case 'apcu':
$this->cacheType = self::apcuCache;
$this->cacheKeyApcu = 'AntCMS_' . hash('md5', __DIR__);
$this->cacheKeyApcu = 'AntCMS_' . hash('md5', __DIR__) . '_';
break;
default:
throw new \Exception("Invalid cache type. Must be 'auto', 'filesystem', 'apcu', or 'none'.");
@ -141,7 +141,25 @@ class AntCache
}
}
public function clearCache(): void
public static function clearCache(): void
{
$di = new \RecursiveDirectoryIterator(AntCachePath, \FilesystemIterator::SKIP_DOTS);
$ri = new \RecursiveIteratorIterator($di, \RecursiveIteratorIterator::CHILD_FIRST);
foreach ($ri as $file) {
$file->isDir() ? rmdir($file->getRealPath()) : unlink($file->getRealPath());
}
if (extension_loaded('apcu') && apcu_enabled()) {
$prefix = 'AntCMS_' . hash('md5', __DIR__) . '_';
$cacheInfo = apcu_cache_info();
$keys = $cacheInfo['cache_list'];
foreach ($keys as $keyInfo) {
$key = $keyInfo['info'];
if (str_starts_with($key, $prefix)) {
apcu_delete($key);
}
}
}
}
}

View file

@ -1,11 +1,10 @@
<?php
$cacheDir = __DIR__ . DIRECTORY_SEPARATOR . 'Cache' . DIRECTORY_SEPARATOR;
$di = new RecursiveDirectoryIterator($cacheDir, FilesystemIterator::SKIP_DOTS);
$ri = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($ri as $file) {
$file->isDir() ? rmdir($file->getRealPath()) : unlink($file->getRealPath());
}
if (extension_loaded('apcu') && apcu_enabled()) {
apcu_clear_cache();
}
require_once __DIR__ . DIRECTORY_SEPARATOR . 'Vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
$classMapPath = __DIR__ . DIRECTORY_SEPARATOR . 'Cache' . DIRECTORY_SEPARATOR . 'classMap.php';
$loader = new AntCMS\AntLoader($classMapPath);
$loader->addPrefix('AntCMS\\', __DIR__ . DIRECTORY_SEPARATOR . 'AntCMS');
$loader->checkClassMap();
$loader->register();
AntCMS\AntCache::clearCache();