Home > Enterprise >  Forgetting Cache using Regular expression laravel
Forgetting Cache using Regular expression laravel

Time:07-16

I am using using laravel and Redis driver to store my cache and I keep my keys scoped to the current tenant. Below is how I set my cache; where $tenant is the current tenant.

$department_count = Cache::remember('dep_count:'.$tenant, 60 * 60 * 24, function () use ($tenant) {
       return Department::where('is_active', 'yes')->where('tenantID', $tenant)->count();
});

So on switch school, I want to clear all the cache for current tenant using cache::forget() but I don't know how to do that.

I've tried Cache::forget('*:'.$tenant) but it doesn't seem to work. Any help on how to go about it is much appreciated.

CodePudding user response:

If I understand you correctly you want to empty all of your cache so you could do that by using

Cache::flush();

for more info check it here from the docs

CodePudding user response:

If you want group cache and remove only specific group of cache you can use tags.

Please read this. It will be helpful

Cache::tags(['tag_'.$tenant])->remember('dep_count:'.$tenant, 60 * 60 * 24, function () use ($tenant) {
       return Department::where('is_active', 'yes')->where('tenantID', $tenant)->count();
});

For remove

Cache::tags(['tag_'.$tenant])->flush();
  • Related