检测缓存驱动程序是否支持标签 [英] Detect if Cache Driver Supports Tags

查看:85
本文介绍了检测缓存驱动程序是否支持标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种干净的方法来确定当前的Cache引擎在Laravel中是否支持tags?我们依靠的是需要tags支持的开源模块/ServiceProvider,并且我想确保我们的系统是防弹的,以便切换缓存引擎不会导致致命错误.

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([]);

引发错误

Illuminate\Cache\FileStore没有方法tags

如果用户的系统配置了诸如memcached或redis之类的代码,则代码可以正常工作.

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');

,但这是对配置了cache服务的很多假设,w/r/t是,缓存服务使用的是驱动程序",驱动程序使用的是存储",而方法没有另一个目的.

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.

我还考虑过在try/catch中包装对Cache::get的调用,但是后来我依靠Laravel的"PHP错误引发异常"行为在以后的版本中不会改变.

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.
}

这篇关于检测缓存驱动程序是否支持标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆