Kaynağa Gözat

refactor: 扩展缓存改存 think Cache(tag=sixshop_extensions),think clear 不影响;sixshop:clear-extension-cache 清 redis 缓存

runphp 1 ay önce
ebeveyn
işleme
3e5e514339

+ 3 - 3
src/Command/ClearExtensionCacheCommand.php

@@ -12,12 +12,12 @@ class ClearExtensionCacheCommand extends Command
     public function configure(): void
     {
         $this->setName('sixshop:clear-extension-cache')
-            ->setDescription('Clear extension composer cache files in runtime/');
+            ->setDescription('Clear extension composer cache');
     }
 
     public function handle(): void
     {
-        $count = CoreService::clearExtensionCache();
-        $this->output->writeln('Cleared ' . $count . ' extension cache file(s).');
+        CoreService::clearExtensionCache();
+        $this->output->writeln('Extension cache cleared.');
     }
 }

+ 2 - 0
src/Plugin.php

@@ -10,6 +10,8 @@ class Plugin
 {
     public const string EXTENSION_TYPE = 'sixshop-extension';
 
+    public const string CACHE_TAG = 'sixshop_extensions';
+
     public static array $installedSixShopExtensions = [];
 
     /**

+ 8 - 17
src/Service/CoreService.php

@@ -12,6 +12,7 @@ use SixShop\Core\Command\ClearExtensionCacheCommand;
 use SixShop\Core\Exception\ExceptionHandle;
 use SixShop\Core\Plugin;
 use SixShop\Core\Request;
+use think\Cache;
 use think\event\HttpRun;
 use think\exception\Handle;
 use think\Service;
@@ -60,20 +61,15 @@ class CoreService extends Service
         if (!empty(self::$extensionComposerMap)) {
             return;
         }
-        $runtimePath = $this->app->getRootPath() . 'runtime/';
         $reference = Plugin::getInstalledSixShopExtensions()['root']['reference'];
-        $extensionComposerFile = $runtimePath . 'extension_' . $reference . '.php';
-        if (file_exists($extensionComposerFile)) {
-            self::$extensionComposerMap = require $extensionComposerFile;
+        $cacheKey = 'extension_composer_' . $reference;
+        $cache = $this->app->make(Cache::class);
+        if ($cache->has($cacheKey)) {
+            self::$extensionComposerMap = $cache->get($cacheKey);
             return;
         }
-        foreach (glob($runtimePath . 'extension_*.php') ?: [] as $file) {
-            unlink($runtimePath . basename($file));
-        }
         self::$extensionComposerMap = self::buildExtensionComposerMap();
-        $header = '// This file is automatically generated at:' . date('Y-m-d H:i:s') . PHP_EOL . 'declare (strict_types = 1);' . PHP_EOL;
-        $content = '<?php ' . PHP_EOL . $header . "return " . var_export(self::$extensionComposerMap, true) . ';';
-        file_put_contents($extensionComposerFile, $content);
+        $cache->tag(Plugin::CACHE_TAG)->set($cacheKey, self::$extensionComposerMap, 0);
     }
 
     /**
@@ -92,15 +88,10 @@ class CoreService extends Service
         return $map;
     }
 
-    public static function clearExtensionCache(): int
+    public static function clearExtensionCache(): void
     {
-        $count = 0;
-        foreach (glob(dirname(self::$extensionPath) . '/runtime/extension_*.php') ?: [] as $file) {
-            unlink($file);
-            $count++;
-        }
+        app()->make(Cache::class)->tag(Plugin::CACHE_TAG)->clear();
         self::$extensionComposerMap = [];
-        return $count;
     }
 
     /**