2017-03-03 29 views
0

使用Laravel的缓存组件(Illuminate/Cache)作为我的应用的后端缓存,如何注册新的自定义缓存驱动程序?由于我根本不使用Laravel(只有Illuminate/Cache),所以我无法将其添加到服务提供商,并且Cache外观返回并显示错误。在独立Laravel缓存实例中添加自定义缓存驱动程序

请注意,我成功地使用默认的驱动程序(文件,memcached的,Redis的)使用singleton函数传递配置和这样的空Illuminate\Container\Container内:

编辑 - 示例代码我如何获得高速缓存商店:

$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); 
+0

你可以告诉你代码,如果你“在一个空的'Illuminate \ Container \ Container'中传递配置等等”? :) –

+0

编辑问题 –

+0

https://laravel.com/docs/5.4/cache#adding-custom-cache-drivers。只需用'CacheManager'实例替换'Cache'外观,例如'$ cacheManager-> extend(...)' –

回答

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

希望这有助于!

+0

'$ this-> repository('接缝也可以工作。 –