【问题标题】:Add custom cache driver in standalone Laravel cache instance在独立的 Laravel 缓存实例中添加自定义缓存驱动程序
【发布时间】: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. 错误
  • 原来我的驱动程序名称有错字。您的解决方案有效,谢谢!如果需要,您可以将其发布为答案,我会将其标记为已接受。

标签: php laravel-5


【解决方案1】:

首先,您需要确保您的驱动程序扩展了Illuminate\Contracts\Cache\Store 接口。

那么您应该能够执行以下操作:

$cacheManager->extend('you-custom-driver-name', function ($app) use($cacheManager) {
    return $cacheManager->repository(new YourCustomDriver);
});

https://laravel.com/docs/5.4/cache#adding-custom-cache-drivers

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 2022-06-15
    • 2023-02-15
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多