From bc641030a4f8d3a073f8a544a265b33f2d0207f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Ruz=CC=8Cevic=CC=81?= Date: Tue, 4 Jun 2024 20:48:29 +0200 Subject: [PATCH] adding additional options for caching --- src/Cache/AbstractManifestCache.php | 82 ++++++++++++++++++++++++++++- src/Cache/ManifestCacheExample.php | 10 ++++ 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/src/Cache/AbstractManifestCache.php b/src/Cache/AbstractManifestCache.php index 57a3e0a2b..052d93182 100644 --- a/src/Cache/AbstractManifestCache.php +++ b/src/Cache/AbstractManifestCache.php @@ -27,6 +27,13 @@ abstract class AbstractManifestCache implements ManifestCacheInterface */ private const TRANSIENT_NAME = 'eightshift_manifest_cache_'; + /** + * Cache key - version. + * + * @var string + */ + public const VERSION_KEY = 'version'; + /** * Cache key - blocks. * @@ -125,6 +132,13 @@ abstract class AbstractManifestCache implements ManifestCacheInterface */ abstract public function getCacheName(): string; + /** + * Get cache version. + * + * @return string. + */ + abstract public function getVersion(): string; + /** * Get cache duration. * Default is 0 = infinite. @@ -196,6 +210,10 @@ public function getManifestCacheSubItem(string $key, string $name, string $cache */ public function setAllCache($ignoreCache = []): void { + if (!$this->isCacheVersionValid()) { + $this->deleteAllCache(); + } + $ignoreCache = \array_flip($ignoreCache); if (!isset($ignoreCache[self::TYPE_BLOCKS])) { @@ -210,6 +228,8 @@ public function setAllCache($ignoreCache = []): void if (!isset($ignoreCache[self::TYPE_GEOLOCATION])) { $this->setCache(self::TYPE_GEOLOCATION); } + + $this->setCacheVersion(); } /** @@ -224,6 +244,8 @@ public function deleteAllCache(): void foreach ($data as $cache) { $this->deleteCache($cache); } + + $this->deleteCacheVersion(); } /** @@ -279,12 +301,70 @@ protected function getCache(string $cacheType = self::TYPE_BLOCKS): array $cache = \get_transient(self::TRANSIENT_NAME . $this->getCacheName() . "_{$cacheType}") ?: ''; // phpcs:ignore WordPress.PHP.DisallowShortTernary.Found if (!$cache) { - $this->setCache(); + $this->setCache($cacheType); } return \json_decode($cache, true) ?? []; } + /** + * Set version cache. + * + * @return void + */ + protected function setCacheVersion(): void + { + $name = self::TRANSIENT_NAME . $this->getCacheName() . '_' . self::VERSION_KEY; + + $cache = \get_transient($name); + + if (!$cache) { + \set_transient($name, $this->getVersion()); + } + } + + /** + * Get cache version. + * + * @return string + */ + protected function getCacheVersion(): string + { + $cache = \get_transient(self::TRANSIENT_NAME . $this->getCacheName() . '_' . self::VERSION_KEY) ?: ''; // phpcs:ignore WordPress.PHP.DisallowShortTernary.Found + + if (!$cache) { + $this->setCacheVersion(); + } + + return $cache; + } + + /** + * Unset cache version. + * + * @return void + */ + protected function deleteCacheVersion(): void + { + \delete_transient(self::TRANSIENT_NAME . $this->getCacheName() . '_' . self::VERSION_KEY); + } + + /** + * Check if cache version is valid. + * + * @return bool + */ + protected function isCacheVersionValid(): bool + { + $cache = $this->getCacheVersion(); + + if ($cache === $this->getVersion()) { + return true; + } + + return false; + } + /** * Get cache builder. * diff --git a/src/Cache/ManifestCacheExample.php b/src/Cache/ManifestCacheExample.php index e40d2b97c..36510a7bf 100644 --- a/src/Cache/ManifestCacheExample.php +++ b/src/Cache/ManifestCacheExample.php @@ -27,4 +27,14 @@ public function getCacheName(): string { return Config::getProjectTextDomain(); } + + /** + * Get cache version. + * + * @return string + */ + public function getVersion(): string + { + return Config::getProjectVersion(); + } }