From 076bb67cb4389404fbaef46113951995bf0cd7d8 Mon Sep 17 00:00:00 2001 From: David <75451291+david-d-h@users.noreply.github.com> Date: Sat, 25 May 2024 13:41:38 +0200 Subject: [PATCH] reactive cache should be able to queue when it is supposed to be updated --- src/CachesValue.php | 42 ++++++++++++---------- src/PermanentCache.php | 9 ++--- src/PermanentCacheJob.php | 75 --------------------------------------- 3 files changed, 29 insertions(+), 97 deletions(-) delete mode 100644 src/PermanentCacheJob.php diff --git a/src/CachesValue.php b/src/CachesValue.php index b66a946..c3575c7 100644 --- a/src/CachesValue.php +++ b/src/CachesValue.php @@ -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. @@ -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]); @@ -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); } } @@ -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 */ @@ -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 @@ -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 @@ -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)) { diff --git a/src/PermanentCache.php b/src/PermanentCache.php index 1c544b2..dc03324 100755 --- a/src/PermanentCache.php +++ b/src/PermanentCache.php @@ -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; diff --git a/src/PermanentCacheJob.php b/src/PermanentCacheJob.php deleted file mode 100644 index 5de601f..0000000 --- a/src/PermanentCacheJob.php +++ /dev/null @@ -1,75 +0,0 @@ -permanentCache->getShortName(); - } - - public function tags(): array - { - return [ - 'event:'.(new ReflectionClass($this->event))->getShortName() - ]; - } - - public function middleware(): array - { - return method_exists($this->permanentCache, 'middleware') ? call_user_func_array([$this->permanentCache, 'middleware'], []) : []; - } - - public function __construct( - public $permanentCache, - public $event - ) - { - if($this->permanentCache->connection) { - $this->onConnection($this->permanentCache->connection); - } - - if($this->permanentCache->queue) { - $this->onQueue($this->permanentCache->queue); - } - - if($this->permanentCache->timeout) { - $this->timeout = $this->permanentCache->timeout; - } - - if($this->permanentCache->tries) { - $this->tries = $this->permanentCache->tries; - } - - if($this->permanentCache->failOnTimeout) { - $this->failOnTimeout = $this->permanentCache->failOnTimeout; - } - - if($this->permanentCache->maxExceptions) { - $this->maxExceptions = $this->permanentCache->maxExceptions; - } - } - - public function handle() - { - $this->permanentCache->handle($this->event); - } -}