renoki-co/laravel-eloquent-query-cache

Cache Handling Between Applications

devajmeireles opened this issue · 5 comments

I'm rewriting an application based on CodeIgniter 4 for Laravel 8, and with that I took the opportunity to add the use of the LEQC package, for the benefits it gives us. However, I have a question and I would like to clarify it.

The cache is composed by the main application (of users), using Redis, and with this tag system:

    protected function getCacheBaseTags(): array
    {
        if (! Auth::check()) {
            return [
                (string) static::class,
            ];
        }

        return [
            'contacts_' . Auth::user()->id,
        ];
    }

My question is:

Taking into account the fact that this application will be composed of three applications: user application (app), user api (api) and administrative panel (admin), but each one of them will be a separate application (core) isolated:

  1. Having a connection established with the same Redis that stores the application (app) cache, how can I clean the cache when something is inserted/updated/deleted made by the api or administrative panel applications?

If all the apps are written with Laravel, you can set the same cache and database prefix for Redis:

REDIS_PREFIX=some_arbitrary_prefix
CACHE_PREFIX=some_arbitrary_prefix

And that's it. The only thing left is to have the same name for the models or set the same cache base tags.

i can use this method inside Model ?

protected function getCacheBaseTags(): array
  {
      return [
          'contacts_' . Auth::user()->id,
      ];
  }

If you guarantee the uniqueness of your tenants is based on the User model, then yes.

@rennokki thanks
Do I have a clear question? How can I tell if the cache is working properly?

You might want to scan the existing Redis keys in your database: https://redis.io/commands/KEYS