瀏覽代碼

Implement a more correct way to clear the cache

Belle Aerni 2 年之前
父節點
當前提交
5896b3d150
共有 2 個文件被更改,包括 29 次插入12 次删除
  1. 21 3
      src/AntCMS/AntCache.php
  2. 8 9
      src/cron.php

+ 21 - 3
src/AntCMS/AntCache.php

@@ -28,7 +28,7 @@ class AntCache
             case 'auto':
             case 'auto':
                 if (extension_loaded('apcu') && apcu_enabled()) {
                 if (extension_loaded('apcu') && apcu_enabled()) {
                     $this->cacheType = self::apcuCache;
                     $this->cacheType = self::apcuCache;
-                    $this->cacheKeyApcu = 'AntCMS_' . hash('md5', __DIR__);
+                    $this->cacheKeyApcu = 'AntCMS_' . hash('md5', __DIR__) . '_';
                 } else {
                 } else {
                     $this->cacheType = self::fileCache;
                     $this->cacheType = self::fileCache;
                 }
                 }
@@ -38,7 +38,7 @@ class AntCache
                 break;
                 break;
             case 'apcu':
             case 'apcu':
                 $this->cacheType = self::apcuCache;
                 $this->cacheType = self::apcuCache;
-                $this->cacheKeyApcu = 'AntCMS_' . hash('md5', __DIR__);
+                $this->cacheKeyApcu = 'AntCMS_' . hash('md5', __DIR__) . '_';
                 break;
                 break;
             default:
             default:
                 throw new \Exception("Invalid cache type. Must be 'auto', 'filesystem', 'apcu', or 'none'.");
                 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);
+                }
+            }
+        }
     }
     }
 }
 }

+ 8 - 9
src/cron.php

@@ -1,11 +1,10 @@
 <?php
 <?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();