laravel 安装配置使用redis

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-cli
redis> set foo bar
OK
redis> 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,
    ],

],
三、实例测试使用


namespace 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);
}
}
}

你可能感兴趣的:(laravel)