Laravel cache with events and webhooks

55 Views Asked by At

I'm trying to implement the "processing" button functionality to use with a set of api calls called through a series of events and corresponding listeners.

I'm using Pusher with Echo, which seem to work fine, but to ensure the status is showing on page reload I also utilise Laravel's cache, which on the initial call sets the key with the value (Cache::put('key', 1)) and after the final call it should remove the entry using Cache::forget('key').

Both cache method are called from within the event subscriber class:

<?php

namespace App\Server\Listeners;

use App\Server\Events\InitialSyncServerRequested;
use App\Server\Events\SyncServerFinished;
use Illuminate\Events\Dispatcher;
use Illuminate\Support\Facades\Cache;

class SyncCacheSubscriber
{
    public function setCache(InitialSyncServerRequested $event): void
    {
        Cache::put('syncServer', $event->serverId);
    }

    public function clearCache(): void
    {
        Cache::forget('syncServer');
    }

    public function subscribe(Dispatcher $events): array
    {
        return [
            InitialSyncServerRequested::class => 'setCache',
            SyncServerFinished::class => 'clearCache',
        ];
    }
}

I've tried using different cache drivers to no avail.

I'm using Redis for both queuing and caching.

Events are being called in succession - once the previous listener finished processing it calls the next event so there is no asynchronous issue.

I've verified that all events are being dispatched and all in the correct order, however the clearing of cache does not take place.

1

There are 1 best solutions below

0
Sebastian Sulinski On

Ok mystery resolved!

The system I'm working with uses multi-tenancy and the issue was that when the first event was triggered, the cache prefix was generated using the application domain variable ADMIN_HOST i.e. mydomain_cache_:, wheres when queued it was using key generated using APP_NAME variable i.e. myapplication_cache_:. Hence, because of the cache prefix it set the cookie with the first event, but did not clear it with the second because it was looking at the wrong cache container.