laravel入门-redis

 

1. 安装redis

1 composer require predis/predis
2 
3 composer update

 

2. 配置redis信息

 

 1 cat config/database.php
 2 
 3 //  'redis' => [
 4     //     'client' => 'phpredis',
 5     //     'clusters' => [
 6     //         'default' => [
 7     //             [
 8     //                 'host' => env('REDIS_HOST', '127.0.0.1'),
 9     //                 'password' => env('REDIS_PASSWORD', null),
10     //                 'port' => env('REDIS_PORT', 6379),
11     //                 'database' => 0,
12     //                 'persistent' => true,
13     //                 'read_timeout' => 60
14     //             ]
15     //         ],
16     //     ],
17     // ],
18 
19     'redis' => [
20 
21         'client' => 'predis',
22 
23         'default' => [
24             'host' => env('REDIS_HOST', 'xxxxxxx'),
25             'password' => env('REDIS_PASSWORD', null),
26             'port' => env('REDIS_PORT', 6379),
27             'database' => env('REDIS_DB', 0),
28             'persistent' => true,
29             'read_timeout' => 60
30         ],
31 
32 
33 cat config/cache.php
34 
35 'redis' => [
36             'driver' => 'redis',
37             'connection' => 'default',
38         ],
39 
40 
41 cat .env 
42 
43 REDIS_HOST=xxxxx
44 REDIS_PASSWORD=null
45 REDIS_PORT=6379

 

3. php代码

cat welcomecontroller.php 

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class WelcomeController extends Controller
{
    public function index() 
    {
        $request = request();
        $name = $request->input('name');
        $d = Redis::set("b","1");   # $d返回OK
        return response("nihao ".$name);
    }

    public function getsex()
    {    
        $d = Redis::get("b");  #$d返回1
        return response("this man sex is 10");
    }
}

 

posted @ 2018-12-07 10:50  追求那份真  阅读(189)  评论(0编辑  收藏  举报