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

 

转载于:https://www.cnblogs.com/nika86/p/10081577.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值