Is there a clean way to determine if the current Cache engine supports tags
in Laravel? We're relying on an open source module/ServiceProvider that needs tags
support, and I want to make sure our system is bullet proof such that switching the cache engine won't cause fatal errors.
Right now, if a user has a system configured with the file or database caching engines, the following code
Cache::tags([]);
throws an error
Illuminate\Cache\FileStore
does not have a methodtags
If a user has a system configured with something like memcached or redis, the code works without issue.
Is there a way to cleanly detect if the currently configured cache engine supports tags? The best I've been able to come up with is
$app = app();
$has_tags = method_exists($app['cache']->driver()->getStore(), 'tags');
but that's making a lot of assumptions w/r/t to there being a cache
service configured, and that the cache service users a "driver", that the driver users a "store", and that the tags
method isn't there fore another purpose.
I've also thought about wrapping the call to Cache::get
in a try/catch, but then I'm relying on Laravel's "throw an exception for a PHP error" behavior not changing in a future version.
Is there an obvious solution I'm missing?
I know this is an old question, but for anyone else arriving here, the correct answer would be:
if(Cache::getStore() instanceof \Illuminate\Cache\TaggableStore) {
// We have a taggable cache.
}
There's a method for that since Laravel 8.10:
if (Cache::supportsTags()) {
// Do things
}
While the other answers work for the built-in cache drivers I've used a tagged file cache driver which has a store that unfortunately does not extend TaggableStore
The only way I could get this to work was by doing:
if (method_exists(Cache::store($type)->getStore(), 'tags')) {
// Supports tags
}
Reason is (I'm guessing) that TaggableStore
is an abstract class and not an interface so it kind of limits the options.
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