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).
To clear all Laravel's cache, just run the following command: $ php artisan optimize:clear Compiled views cleared! Application cache cleared!
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With