我使用redis
作为缓存数据的驱动程序。Laravel的数据库配置具有定义Redis连接信息的能力。
'redis' => array(
'cluster' => true,
'default' => array(
'host' => '127.0.0.1',
'port' => 6379,
'database' => 0,
),
),
但是,如果我想要定义多个连接并使用一个特定的connection
作为缓存,我如何在Laravel 4上做到这一点。在cache.php上没有连接配置,可以在这里指定redis连接名。它目前有一个connection
配置,如果缓存驱动程序是database
,将使用该配置。
编辑
我刚刚通过了Laravel代码,当初始化Redis驱动程序时,看起来Laravel没有查看连接。我的理解正确吗?
http://laravel.com/api/source-class-Illuminate.Cache.CacheManager.html#63-73
protected function createRedisDriver()
{
$redis = $this->app['redis'];
return $this->repository(new RedisStore($redis, $this->getPrefix()));
}
发布于 2013-08-26 16:32:05
能处理多个连接。见关于添加/使用多个数据库连接的问题/答案。
一旦为redis定义了多个连接,就需要做一些腿工作来访问代码中的这些连接。看起来可能是这样的:
$redisCache = App::make('cache'); // Assumes "redis" set as your cache
$redisCache->setConnection('some-connection'); // Your redis cache connection
$redisCache->put($key, $value');
编辑
我将在这里添加一点,让您了解如何做到这一点,这样就不需要到处都有连接逻辑了:
最简单的是,您可以在应用程序中的某个地方(可能是一个绑定实例或其他app/start/*..php文件)缓存您的redis缓存:
App::singleton('rediscache', function($app){
$redisCache = $app['cache'];
$redisCache->setConnection('some-connection'); // Your redis cache connection
return $redisCache;
});
然后,在代码中,您可以这样做来缓存:
$cache = App::make('rediscache');
$cache->put($key, $value); // Or whatever you need to do
如果您有代码的自己的应用程序库,也可以创建服务提供程序。您可以在其中注册'rediscache‘,然后在应用程序中以相同的方式使用它。
希望这可以作为一个开端--还有其他的代码体系结构--使用依赖注入,或者使用存储库来进一步组织代码。
https://stackoverflow.com/questions/18438974
复制相似问题