Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if Cache Driver Supports Tags

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 method tags

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?

like image 600
Alan Storm Avatar asked Sep 08 '14 17:09

Alan Storm


3 Answers

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.
}
like image 127
Harry Mustoe-Playfair Avatar answered Nov 14 '22 22:11

Harry Mustoe-Playfair


There's a method for that since Laravel 8.10:

if (Cache::supportsTags()) {
    // Do things
}
like image 44
Epoc Avatar answered Nov 14 '22 22:11

Epoc


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.

like image 1
apokryfos Avatar answered Nov 14 '22 20:11

apokryfos