【发布时间】:2017-07-23 15:22:11
【问题描述】:
使用 Laravel 的缓存组件 (Illuminate/Cache) 作为我的应用程序的缓存后端,我如何注册一个新的自定义缓存驱动程序?由于我根本不使用 Laravel(仅 Illuminate/Cache),因此无法将其添加到服务提供者中,并且 Cache 门面返回并出错。
请注意,我通过使用 singleton 函数在空的 Illuminate\Container\Container 中传递配置等成功地使用了默认驱动程序(文件、memcached、redis):
编辑 - 我如何获得缓存存储的示例代码:
$app = new Illuminate\Container\Container();
// Where $config is an array of config values
$app->singleton('config', function() use ($config) {
return $config;
});
$app->singleton('files', function() {
return new Illuminate\Filesystem\Filesystem();
});
$cacheManager = new CacheManager($app);
// Where $storeName is linked to the configs values
return $cacheManager->store($storeName);
【问题讨论】:
-
如果您“在空的
Illuminate\Container\Container中传递配置等”,您能否向您展示代码? :) -
问题已编辑
-
laravel.com/docs/5.4/cache#adding-custom-cache-drivers 。只需将
Cache外观替换为您的CacheManager实例,例如$cacheManager->extend(...) -
我仍然收到
InvalidArgumentException: Driver [fooDriver] is not supported.错误 -
原来我的驱动程序名称有错字。您的解决方案有效,谢谢!如果需要,您可以将其发布为答案,我会将其标记为已接受。