`redis`有很广泛的应用,在`laravel`中该如何使用`redis`
首先需要安装扩展包 `composer require predis/predis`
然后在`.env`中添加配置
>REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
在`controller`中使用时需要 `use Illuminate\Support\Facades\Redis;`
然后`Redis::set('key','value');`
连接`多个redis`
在`config/database.php`中编辑
>'redis' => [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
'my-connection' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => 0,
],
],
使用
`$redis = Redis::connection('my-connection');`
`echo "key->".$redis->get('key');`