Skip to content

Commit

Permalink
rename cache driver to cache store, disable cache during updating and…
Browse files Browse the repository at this point in the history
… retrieve parameters as original instead of converting to an array
  • Loading branch information
markvaneijk committed May 10, 2024
1 parent 3bd1d80 commit 30b6552
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
2 changes: 1 addition & 1 deletion config/permanent-cache.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

return [
'driver' => env('PERMANENT_CACHE_DRIVER', 'file'),
'store' => env('PERMANENT_CACHE_STORE', 'file'),

'components' => [
// Add markers around the rendered value of Cached Components,
Expand Down
41 changes: 24 additions & 17 deletions src/CachesValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,13 @@ trait CachesValue
*/
final public function handle($event = null): void
{
// disable possible active caching mechanisms
$cacheDefault = config('cache.default');
config(['cache.default' => null]);

$this->isUpdating = true;

[$driver, $cacheKey] = $this->store($this->getParameters());
[$store, $cacheKey] = $this->store($this->getParameters());

PermanentCacheUpdating::dispatch($this);

Expand All @@ -73,14 +77,17 @@ final public function handle($event = null): void
$value = $this->run($event);
}

Cache::driver($driver)->forever($cacheKey, (object) [
Cache::store($store)->forever($cacheKey, (object) [
'value' => $value,
'updated_at' => now(),
]);

PermanentCacheUpdated::dispatch($this, $value);

$this->isUpdating = false;

// return cache store to original value
config(['cache.default' => $cacheDefault]);
}

public function getParameters()
Expand All @@ -89,11 +96,11 @@ public function getParameters()
->getProperties(\ReflectionProperty::IS_PUBLIC))
->filter(fn (\ReflectionProperty $p) => $p->class === static::class)
->mapWithKeys(fn (\ReflectionProperty $p) => [$p->name => $p->getValue($this)])
->toArray();
->all();
}

/**
* Get the driver and identifier specified in the $store property.
* Get the store and identifier specified in the $store property.
*
* @return array{string, string}
*/
Expand All @@ -106,9 +113,9 @@ public function isCached($parameters = []): bool
{
$parameters ??= $this->getParameters();

[$driver, $cacheKey] = self::store($parameters ?? []);
[$store, $cacheKey] = self::store($parameters ?? []);

$cache = Cache::driver($driver);
$cache = Cache::store($store);

return $cache->has($cacheKey);
}
Expand Down Expand Up @@ -148,9 +155,9 @@ final public static function updateAndGet($parameters = [])
*/
final public static function get($parameters = [], $default = null, bool $update = false): mixed
{
[$driver, $cacheKey] = self::store($parameters ?? []);
[$store, $cacheKey] = self::store($parameters ?? []);

$cache = Cache::driver($driver);
$cache = Cache::store($store);

if (
$update &&
Expand All @@ -164,9 +171,9 @@ final public static function get($parameters = [], $default = null, bool $update

final public function getMeta($parameters = []): mixed
{
[$driver, $cacheKey] = $this->store($parameters ?? []);
[$store, $cacheKey] = $this->store($parameters ?? []);

$cache = Cache::driver($driver);
$cache = Cache::store($store);

return $cache->get($cacheKey);
}
Expand All @@ -186,9 +193,9 @@ final protected function value($default = null): mixed
throw new \Exception("A cached component can't have a default return value");
}

[$driver, $cacheKey] = $this->store($this->getParameters());
[$store, $cacheKey] = $this->store($this->getParameters());

return Cache::driver($driver)->get(
return Cache::store($store)->get(
$cacheKey, $default,
)?->value;
}
Expand Down Expand Up @@ -262,27 +269,27 @@ public static function getCacheKey(?array $parameters = [], ?string $store = nul
! is_null($store) &&
strpos($store, ':')
) {
$cacheDriver = substr($store, 0, strpos($store, ':'));
$cacheStore = substr($store, 0, strpos($store, ':'));
$cacheKey = substr($store, strpos($store, ':') + 1);
} else {
$cacheKey = $store;
}

$cacheDriver ??= config('permanent-cache.driver') ?: config('cache.default');
$cacheStore ??= config('permanent-cache.driver') ?: config('cache.default');
$cacheKey ??= preg_replace('/[^A-Za-z0-9]+/', '_', strtolower(Str::snake($class)));

if ($parameters) {
$cacheKey .= ':'.http_build_query($parameters);
}

return [$cacheDriver, $cacheKey];
return [$cacheStore, $cacheKey];
}

public function getMarker(array $parameters = [], $close = false): string
{
[$cacheDriver, $cacheKey] = $this::store($parameters ?? $this->getParameters());
[$cacheStore, $cacheKey] = $this::store($parameters ?? $this->getParameters());

$marker = $cacheDriver.':'.$cacheKey;
$marker = $cacheStore.':'.$cacheKey;

if (config('permanent-cache.components.markers.hash')) {
$marker = md5($marker);
Expand Down

0 comments on commit 30b6552

Please sign in to comment.