tp5实现Redis的简单使用

方法1:

Controller

<?php
namespace app\index\controller;

use think\Controller;
use think\session\driver\Redis;

class Index extends Controller
{
    public function index()
    {
        $redis = new Redis();
        if(is_null($redis->has('str'))){
            var_dump($redis->set('str','this is redis_str'));
        }else{
            var_dump($redis->get('str'));
        }
    }
}

方法2:

config.php

// +----------------------------------------------------------------------
    // | 缓存设置
    // +----------------------------------------------------------------------

    'cache'                  => [
        // 驱动方式
        'type'   => 'File',
        // 缓存保存目录
        'path'   => CACHE_PATH,
        // 缓存前缀
        'prefix' => '',
         //缓存有效期 0表示永久缓存
        'expire' => 0,
    ],

    'redis'   =>  [
        // 驱动方式
        'type'   => 'redis',
        // 服务器地址
        'host'   => '127.0.0.1',  //redis服务器ip
        'port'   => '6379',
        'password'=> "",
        'timeout' => 86400
    ],

Controller  

<?php
namespace app\index\controller;

use think\Cache;
use think\Controller;

class Index extends Controller
{
    public function index()
    {
        if(!Cache::has('str')){
            var_dump(Cache::set('str','this is redis_str'));
        }else{
            var_dump(Cache::get('str'));
        }
    }
} 

猜你喜欢

转载自www.cnblogs.com/yulongcode/p/11240885.html
今日推荐