Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding additional options for caching #405

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 81 additions & 1 deletion src/Cache/AbstractManifestCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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])) {
Expand All @@ -210,6 +228,8 @@ public function setAllCache($ignoreCache = []): void
if (!isset($ignoreCache[self::TYPE_GEOLOCATION])) {
$this->setCache(self::TYPE_GEOLOCATION);
}

$this->setCacheVersion();
}

/**
Expand All @@ -224,6 +244,8 @@ public function deleteAllCache(): void
foreach ($data as $cache) {
$this->deleteCache($cache);
}

$this->deleteCacheVersion();
}

/**
Expand Down Expand Up @@ -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.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Cache/ManifestCacheExample.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,14 @@ public function getCacheName(): string
{
return Config::getProjectTextDomain();
}

/**
* Get cache version.
*
* @return string
*/
public function getVersion(): string
{
return Config::getProjectVersion();
}
}