laravel 安装配置使用redis

来源:互联网 发布:淘宝丝芙兰代购骗局 编辑:程序博客网 时间:2024/05/21 01:47
1.下载安装redis   下载地址及官方文档命令:https://redis.io/download

一、安装启动Redis

$ wget http://download.redis.io/releases/redis-4.0.1.tar.gz$ tar xzf redis-4.0.1.tar.gz$ cd redis-4.0.1$ make
启动
$ src/redis-server
客户端测试

You can interact with Redis using the built-in client:

$ src/redis-cliredis> set foo barOKredis> get foo"bar"
二、composer安装predis 
composer require predis/predis
配置redis

 config/database.php 

'redis' => [    'client' => 'predis',    'default' => [        'host' => env('REDIS_HOST', 'localhost'),        'password' => env('REDIS_PASSWORD', null),        'port' => env('REDIS_PORT', 6379),        'database' => 0,    ],],
三、实例测试使用
<?phpnamespace App\Http\Controllers;use Illuminate\Support\Facades\Redis;use App\Http\Controllers\Controller;class UserController extends Controller{    /**     * Show the profile for the given user.     *     * @param  int  $id     * @return Response     */    public function showProfile($id)    {        $user = Redis::get('user:profile:'.$id);   
        return view('user.profile', ['user' => $user]);
    }
public function showUser ($id){
$cacheUsers = Redis::get('userlist');
if( !$cacheUsers ){
$users = $this->model->fetchAll();
Redis::set('userList', $users);
}
}
}