php - How to change the connection info for Laravel 4 when using the redis driver? -
i using redis
driver caching data. database configuration of laravel has ability define redis connection information.
'redis' => array( 'cluster' => true, 'default' => array( 'host' => '127.0.0.1', 'port' => 6379, 'database' => 0, ), ),
but if wanted have multiple connections defined , use specific connection
use cache, how can on laravel 4. there no connection configuration on cache.php can specify redis connection name. has connection
config used if cache driver database
.
edit
i went through laravel code , when initializing redis driver, looks laravel not looking connection. understanding correct?
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())); }
laravel can handle multiple connections. see this question/answer on adding/using multiple database connections.
once define multiple connections redis, you'll need leg work access somewhere in code. might this:
$rediscache = app::make('cache'); // assumes "redis" set cache $rediscache->setconnection('some-connection'); // redis cache connection $rediscache->put($key, $value');
edit
i'll add little here give idea of how don't need connection logic everywhere:
most simply, can bind instance of redis cache somewhere (perhaps start.php or other app/start/*.php file) in app:
app::singleton('rediscache', function($app){ $rediscache = $app['cache']; $rediscache->setconnection('some-connection'); // redis cache connection return $rediscache; });
then, in code, can cache:
$cache = app::make('rediscache'); $cache->put($key, $value); // or whatever need
you can create service provider if have own application library of code. can register 'rediscache' within there , use in same way in application.
hope helps start - there other code architectures - using dependency injection , maybe repository organize code further.
Comments
Post a Comment