首先,下载Predis源代码,地址:https://github.com/nrk/predis/tree/master。将代码至于 /vendor   目录下,代码结构如下:

 

 

ThinkPHP5 + predis 环境配置_redis

2. 创建Redis.php 文件【方便引用】

路径:\thinkphp\library\think\Redis.php

【下面是不完全版,可以随时补充Redis的语法】

<?php

namespace think;

use \Predis\Autoloader;
use \Predis\Client;

require __DIR__ . '/../../../vendor/predis/autoload.php';

// https://www.php.cn/php-weizijiaocheng-392744.html
class Redis
{

/**
* 构造函数
* @access protected
* @param array $options 参数
*/
protected function __construct($options = [])
{
Autoloader::register();
$redis = new Client();
}

//设置
public static function set($name, $value)
{
$redis = new Client();
return $redis->set($name, $value);
}

//取出
public static function get($name)
{
$redis = new Client();
return $redis->get($name);
}

//设置有效期为N秒的键值
public static function setex($name, $time = 60, $value)
{
$redis = new Client();
return $redis->setex($name, $time, $value);
}

// 在h表中 添加name字段 value为TK
public static function hSet($table, $name, $value)
{
$redis = new Client();
return $redis->hSet($table, $name, $value);
}

// 获取h表中name字段value
public static function hGet($table, $name)
{
$redis = new Client();
return $redis->hGet($table, $name);
}

//判断email 字段是否存在与表h 不存在返回false
public static function hExists($table, $name)
{
$redis = new Client();
return $redis->hExists($table, $name);
}

//获取h表中所有字段value
public static function hKeys($table)
{
$redis = new Client();
return $redis->hKeys($table);
}

//获取h表中所有字段value
public static function hVals($table)
{
$redis = new Client();
return $redis->hVals($table);
}

// 获取h表中所有字段和value 返回一个关联数组(字段为键值)
public static function hGetAll($table)
{
$redis = new Client();
return $redis->hGetAll($table);
}
// 删除h表中email 字段
public static function hDel($table, $name)
{
$redis = new Client();
return $redis->hDel($table, $name);
}
}

 

redis的使用:​https://www.php.cn/php-weizijiaocheng-392744.html​

 

用法:

<?php

namespace app\wxapi\controller;

use Exception;
use think\Db;
use think\Redis;

class Index extends Base
{


/*
* 获取首页数据
*/
public function home()
{

//获取店铺ID
$store_id = (int) input('store_id');


//redis
try{
if(Redis::hExists($store_id,'wxapi-index-home')){
exit(Redis::hGet($store_id,'wxapi-index-home'));
}
}catch(Exception $e){

}


//
//
// 接口业务逻辑代码
//
//



try{
Redis::hSet($store_id,'wxapi-index-home',json_encode(array('status' => 1, 'msg' => '获取成功', 'result' => $result),JSON_UNESCAPED_UNICODE));
}catch(Exception $e){

}

ajaxReturn(array('status' => 1, 'msg' => '获取成功', 'result' => $result));

}

}

这个就是 我们项目 首页 接口使用  Redis 的方法

接口效率从 600s 提升至 0.6s