Skip to content

Commit

Permalink
reactive cache should be able to queue when it is supposed to be updated
Browse files Browse the repository at this point in the history
  • Loading branch information
david-d-h committed May 25, 2024
1 parent d726208 commit 076bb67
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 97 deletions.
42 changes: 24 additions & 18 deletions src/CachesValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ trait CachesValue
*/
private $isUpdating = false;

/**
* The event this cache is currently updating with.
*
* @var mixed
*/
private $currentEvent = null;

/**
* Update the cached value, this method expects an event if
* the cacher is not static.
Expand All @@ -60,6 +67,11 @@ trait CachesValue
*/
final public function handle($event = null): void
{
if (isset($this->currentEvent)) {
$event = $this->currentEvent;
unset($this->currentEvent);
}

// disable possible active caching mechanisms
$cacheDefault = config('cache.default');
config(['cache.default' => null]);
Expand Down Expand Up @@ -133,21 +145,16 @@ final public static function update($parameters = [])
);
}

public function shouldQueue(): bool
{
return in_array(ShouldQueue::class, class_implements($this));
}

/**
* Update static cache after an event has been dispatched.
* Update a reactive cache after an event it is listening for
* has been dispatched.
*/
final public function updateAfterEvent($event = null)
final public function updateAfterEvent($event)
{
if ($this->shouldQueue()) {
PermanentCacheJob::dispatch($this, $event)
->delay($this->delay);
}
else {
if ($this instanceof ShouldQueue) {
$this->currentEvent = $event;
dispatch($this);
} else {
$this->handle($event);
}
}
Expand All @@ -169,7 +176,7 @@ final public static function updateAndGet($parameters = [])
/**
* Get the cached value this cacher provides.
*
* @param bool $update Whether the cache should update
* @param bool $update Whether the cache should update
* when it doesn't hold the value yet.
* @return V|mixed|null
*/
Expand All @@ -186,7 +193,7 @@ final public static function get($parameters = [], $default = null, bool $update
return static::updateAndGet($parameters ?? []);
}

return $cache->get($cacheKey, $default)?->value;
return $cache->get($cacheKey)?->value ?? $default;
}

final public function getMeta($parameters = []): mixed
Expand Down Expand Up @@ -215,9 +222,7 @@ final protected function value($default = null): mixed

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

return Cache::store($store)->get(
$cacheKey, $default,
)?->value;
return Cache::store($store)->get($cacheKey)?->value ?? $default;
}

public function getName(): string
Expand All @@ -231,7 +236,8 @@ public function getShortName(): string
}

/// Default implementation for the `\Scheduled::schedule` method.
/** @param CallbackEvent $callback */

/** @param CallbackEvent $callback */
public static function schedule($callback)
{
if (! is_a(static::class, Scheduled::class, true)) {
Expand Down
9 changes: 5 additions & 4 deletions src/PermanentCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ public function caches($registeredCaches): self

$cacheInstance = $this->app->make($cache, $parameters);

if ([] !== $events = $cacheInstance->getListenerEvents()) {
foreach($events as $event) {
Event::listen($event, fn ($e) => $cacheInstance->updateAfterEvent(event: $e));
}
foreach ($cacheInstance->getListenerEvents() as $event) {
Event::listen($event, function ($e) use ($cacheInstance) {
$cache = clone $cacheInstance;
$cache->updateAfterEvent($e);
});
}

$this->caches[$cacheInstance] = $parameters;
Expand Down
75 changes: 0 additions & 75 deletions src/PermanentCacheJob.php

This file was deleted.

0 comments on commit 076bb67

Please sign in to comment.