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?
If you'd like to generate a list of the stores that support tagging, use this:
These are the answers for the default cache stores (on 5.2 installation, other versions seem the same):
I know this is an old question, but for anyone else arriving here, the correct answer would be:
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:
Reason is (I'm guessing) that
TaggableStore
is an abstract class and not an interface so it kind of limits the options.