Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting from Laravel 4 cache using pattern for key?

For my package we make use of the Laravel cache,

Every cache key we create is prefixed, so we get mypackage-config, mypackage-md5ofafilename At times I need to flush all cache files that my package has created, the issue? I only know the pattern of the cache keys, I don't know the entire key!

So, I need a way to go Cache::forget('mypackage-*') or similar, is this possible?

If it was just for my system I know I am using the file cache, so I could manually unlink the files, but because it is a generic package I don't know what cache method the end user is using, I just know the interface (aka the Laravel cache interface).

like image 405
Hailwood Avatar asked Feb 14 '13 11:02

Hailwood


People also ask

Which command is used to reset the cache in Laravel?

To clear all Laravel's cache, just run the following command: $ php artisan optimize:clear Compiled views cleared! Application cache cleared!

Where is Laravel config cache stored?

The cache configuration is located at config/cache. php . In this file you may specify which cache driver you would like used by default throughout your application. Laravel supports popular caching backends like Memcached and Redis out of the box.

What is cache :: Remember?

Cache::remember() You can also use this method to retrieve cached data by checking the cache server using the key provided. If the data is stored in the cache, it will retrieve it. Otherwise, it will retrieve the data from the Database Server and cache it.


1 Answers

Another solution: as long as you are not using file or database cache you can make use of Cache Tags.

Just tag every cache entry with your package name:

Cache::tags('myPackage')->put('config', $config, $minutes);
Cache::tags('myPackage')->put('md5ofafilename', $md5, $minutes);

(You can also use the tags method with remember, forever, and rememberForever)

When it's time to flush the cache of your package's entries just do

Cache::tags('myPackage')->flush();

Note: When you need to access the cache entries you still need to reference the tags. E.g.

$myConfig = Cache::tags('myPackage')->get('config');

That way, another cache entry with key config having a different tag (e.g. hisPackage) will not conflict with yours.

like image 91
harryg Avatar answered Oct 09 '22 16:10

harryg