Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
markvaneijk committed May 16, 2024
1 parent 04f362a commit d726208
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 1 deletion.
20 changes: 20 additions & 0 deletions src/CachesValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/PermanentCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down
75 changes: 75 additions & 0 deletions src/PermanentCacheJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Vormkracht10\PermanentCache;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Queue\SerializesModels;
use ReflectionClass;

class PermanentCacheJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public $connection;
public $queue;
public int $timeout;
public bool $failOnTimeout;
public int $tries;
public int $maxExceptions;

public function displayName(): string
{
return $this->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);
}
}

0 comments on commit d726208

Please sign in to comment.