首页 > 数据库 >ThinkPHP5 + predis 环境配置

ThinkPHP5 + predis 环境配置

时间:2023-01-11 18:05:42浏览次数:48  
标签:function name ThinkPHP5 配置 redis Client new table predis


 

首先,下载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

 

标签:function,name,ThinkPHP5,配置,redis,Client,new,table,predis
From: https://blog.51cto.com/jing/6002486

相关文章

  • thinkphp安装在子目录的nginx配置
    进行URL重写,将默认访问URL中的index.php?s=通过rewrite隐藏location/blog/{indexindex.phpindex.htmlindex.htm;if(!-e$request_filename){rewrite......
  • VUE项目的API项目的nginx配置
    #PROXY-START/apilocation/api{expires12h;if($request_uri~*"(php|jsp|cgi|asp|aspx)"){expi......
  • Windows配置NTP
    服务端Linux、Windows都可以,自行配置客户端此环境为WindowsServer2016,其他版本大同小异开始-运行-gpedit.msc,进入如下,首先配置NTP客户端启用NTP客户端更新......
  • Nacos共享配置(shared-configs)和扩展配(extension-config)
    一、共享配置(shared-configs)和扩展配(extension-config)日常开发中,多个模块可能会有很多共用的配置,比如数据库连接信息,Redis连接信息,RabbitMQ连接信息,监控配置等等。......
  • nginx配置
    #Formoreinformationonconfiguration,see#*OfficialEnglishDocumentation:http://nginx.org/en/docs/#*OfficialRussianDocumentation:http://ngin......
  • Centos7最小安装之网络配置
    1.获取权限命令行先进入​​su​​​(不进的话,每次输密码很烦2.查看网卡信息输入命令 ​​ls/etc/sysconfig/network-scripts​​可以看到有线网卡名称为​​ifcfg-enp3s0......
  • SpringBoot配置文件中spring
    SpringBoot配置文件中spring.profiles.active配置详解现象:在开发的时候测试和生产配置不一样每次加配置都需要更新生产原因:不方便解决:添加多套配置知识库:1、多环......
  • 【Azure 应用服务】Azure Function 部署槽交换时,一不小心把预生产槽上的配置参数交换
    问题描述部署Function代码先到预生产槽中,进行测试后通过交换方式,把预生产槽中的代码交换到生产槽上,因为在预生产槽中的设置参数值与生产槽有不同,但是在交换的时候,没有仔细......
  • python之路 58 linux文件配置相关
    网络不通排除流程1.确认⽹关地址是否通畅2.确认⽹卡配置是否正确vi/etc/sysconfig/network-scripts/ifcfg-eth03.确认⽹络管理服务关闭systemctlstopNe......
  • ubuntu20.04 安装 samba 并配置 win10映射
    1、安装samba软件并配置开机自启动apt-getinstallsambasamba-commonsystemctlenablesmbd2、创建sumba主文件夹mkdir/opt/data/sambachmod-R777/opt/data/sa......