diff --git a/src/CachesValue.php b/src/CachesValue.php index 80e9659..b66a946 100644 --- a/src/CachesValue.php +++ b/src/CachesValue.php @@ -4,6 +4,7 @@ use Illuminate\Bus\Queueable; use Illuminate\Console\Scheduling\CallbackEvent; +use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Blade; use Illuminate\Support\Facades\Cache; @@ -132,6 +133,25 @@ 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. + */ + final public function updateAfterEvent($event = null) + { + if ($this->shouldQueue()) { + PermanentCacheJob::dispatch($this, $event) + ->delay($this->delay); + } + else { + $this->handle($event); + } + } + /** * Manually force a static cache to update. */ diff --git a/src/PermanentCache.php b/src/PermanentCache.php index 265b403..1c544b2 100755 --- a/src/PermanentCache.php +++ b/src/PermanentCache.php @@ -46,7 +46,7 @@ public function caches($registeredCaches): self if ([] !== $events = $cacheInstance->getListenerEvents()) { foreach($events as $event) { - Event::listen($event, fn ($e) => $cacheInstance->handle($e)); + Event::listen($event, fn ($e) => $cacheInstance->updateAfterEvent(event: $e)); } } diff --git a/src/PermanentCacheJob.php b/src/PermanentCacheJob.php new file mode 100644 index 0000000..5de601f --- /dev/null +++ b/src/PermanentCacheJob.php @@ -0,0 +1,75 @@ +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); + } +}