Преглед изворни кода

Update Cache\FileCache

Add cache cleaning by condition
Visman пре 3 година
родитељ
комит
a23ab87106
1 измењених фајлова са 22 додато и 1 уклоњено
  1. 22 1
      app/Core/Cache/FileCache.php

+ 22 - 1
app/Core/Cache/FileCache.php

@@ -28,7 +28,7 @@ class FileCache implements CacheInterface
      */
     protected $cacheDir;
 
-    public function __construct(string $dir)
+    public function __construct(string $dir, /* string */ $resetMark)
     {
         $dir = \rtrim($dir, '\\/');
 
@@ -41,6 +41,10 @@ class FileCache implements CacheInterface
         }
 
         $this->cacheDir = $dir;
+
+        if (\is_string($resetMark)) {
+            $this->resetIfRequired($resetMark);
+        }
     }
 
     /**
@@ -237,4 +241,21 @@ class FileCache implements CacheInterface
             throw new InvalidArgumentException('Expects a iterable, got: ' . \gettype($iterable));
         }
     }
+
+    /**
+     * Сбрасывает кеш при изменении $resetMark
+     */
+    protected function resetIfRequired(string $resetMark): void
+    {
+        if (empty($resetMark)) {
+            return;
+        }
+
+        $hash = \sha1($resetMark);
+
+        if ($this->get('reset_mark_hash') !== $hash) {
+            $this->clear();
+            $this->set('reset_mark_hash', $hash);
+        }
+    }
 }