ThinkPHP5 + predis 环境配置

11 篇文章 0 订阅
4 篇文章 0 订阅

 

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

 

 

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

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值