定义redis参数
//redis对象
public $redis;
//redis连接IP
protected $host="127.0.0.1";
//redis连接端口
protected $port=6379;
//redis密码 没有是为空
protected $auth = "";
//超时时间
protected $timeout = 86400;
//查询的库
protected $datebase = 0;
//默认过期时间
protected $expire = 0;
链接redis
/**构造函数
* RedisCache constructor.
* @param array $config redis配置参数
*/
public function __construct($config = [])
{
//获取IP
$this->host = trim(isset($config["host"]) ? $config["host"] : $this->host);
//获取端口
$this->port = trim(isset($config["port"]) ? $config["port"] : $this->port );
//密码
$this->auth = trim(isset($config["auth"]) ? $config["auth"] : $this->auth );
//超时时间
$this->timeout = trim(isset($config["timeout"]) ? $config["timeout"] : $this->timeout );
//查询的库
$this->datebase = trim(isset($config["datebase"]) ? $config["datebase"] : $this->datebase );
//过期时间
$this->expire = trim(isset($config["expire"]) ? $config["expire"] : $this->expire );
//创建Reids对象
$this->redis = new Redis();
//连接redis
$this->redis->connect($this->host,$this->port,$this->timeout);
//判断是否需要密码
if ('' != $this->auth) {
//校验密码
$this->redis->auth($this->auth);
}
//判断所使用的库
if (0 != $this->datebase) {
//查询库
$this->redis->select($this->datebase);
}
}
设置单个Hash缓存
/**
* @Note 设置单个hash缓存
* @DATA 2020-03-24 15:40:36
* @Author shenzx
* @param string $table hash表
* @param string $index hash表索引
* @param string $value hash值
* @param int $expire 过期时间 不传默认为一天 为负默认不设置过期时间
* @return bool
*/
public function setHash($table = "",$index = "",$value = "",$expire = 0){
//判断必要参数是否为空
if(empty($table) || empty($index)){
return false;
}
//处理数据 数据json 防止参数是数组时报错
$value = $this->json($value);
//缓存数据
$result = $this->handler->hSet($table,$index,$value);
//判断缓存结果
if($result === false) return false;
//处理过期时间
$this->handleExpire($table,$expire);
//缓存成功 返回true
return true;
}
获取单个Hash缓存
/**
* @Note 获取单个hash缓存
* @DATA 2020-03-24 16:25:01
* @Author shenzx
* @param string $table
* @param string $index
* @return bool|string
*/
public function getHash($table = "" ,$index = ""){
//判断Hash表名是否为空
if(empty($table)){
return false;
}
//判断缓存是否存在
if(!$this->handler->Exists($table)){
return false;
}
//判断是否有传索引 没有时返回整个hash表数据 也可以抛出错误
if(empty($index)){
//获取整个hash表数据
$data = $this->handler->hGetAll($table);
}else{
//获取缓存数据
$data = $this->handler->hGet($table,$index);
}
//判断是否获取到缓存数据
if($data === false){
//获取失败 返回false
return false;
}
//判断是否是数组
if(is_array($data)){
//处理读取的缓存数据
foreach ($data as $k=>&$v){
//判断值是否失效
if(!empty($v)){
//数据反json处理
$v = $this->json($v,false);
}
}
}else{
//处理读取的缓存数据反json
$data = $this->json($data,false);
}
//返回缓存的数据
return $data;
}
设置多个hash缓存
/**
* @Note 设置多个个hash缓存
* @DATA 2020-03-24 15:40:36
* @Author shenzx
* @param string $table hash表
* @param string $index hash表索引
* @param string $value hash值
* @param int $expire 过期时间 不传默认为一天 为负默认不设置过期时间
* @return bool
*/
public function setMHash($table = "",$data = [],$expire = 0){
//判断表名或缓存数据是否为空
if(empty($table) || empty($data)){
return false;
}
//数据循环json处理
foreach ($data as &$v){
//数据json处理
$v = $this->json($v);
}
//缓存数据
$result = $this->handler->hMSet($table,$data);
//判断缓存结果
if($result === false) return false;
//处理过期时间
$this->handleExpire($table,$expire);
//缓存成功 返回true
return true;
}
获取多个缓存
/**
* @Note 获取多个hash缓存
* @DATA 2020-03-24 16:25:01
* @Author shenzx
* @param string $table
* @param string $index
* @return bool|string
*/
public function getMHash($table = "" ,$keys = []){
#判断值是否为空
if(empty($table)){
return false;
}
#判断缓存是否存在
if(!$this->handler->exists($table)){
return false;
}
#判断获取多个还是所有
if(empty($keys)){
#获取整个hash表
$data = $this->handler->hGetAll($table);
}else{
#获取缓存
$data = $this->handler->hMGet($table,$keys);
}
#判断是否获取到数据
if($data === false){
#获取失败 返回false
return false;
}
#处理读取的缓存数据
$list = [];
foreach ($data as $k=>$v){
#判断值是否失效
if(!empty($v)){
#数据反json处理
$list[$k] = $this->json($v,false);
}
}
#删除变量 释放内存
unset($data);
#返回缓存的数据
return $list;
}
删除缓存
/**
* @Note 从hash表中移除
* @DATA 2020-03-24 17:37:51
* @Author shenzx
* @param string $table 需要处理的hash表
* @param array $index 需要移除的hash索引 当索引为空时清除所有
* @return bool
*/
public function clearHash($table = "",$index = []){
#判断是否有传
if(empty($table)){
return false;
}
#判断是否删除所有
if(empty($index)){
$this->handler->del($table);
}else{
#处理所传的键
$index = is_array($index) ? $index : [$index];
#合并参数
$keys = array_merge([$table],$index);
#使用回调函数 移除缓存
call_user_func_array([$this->handler,"hDel"],$keys);
}
#当需要移除的数据过期过不存在时当做移除成功处理 移除成功 返回true
return true;
}
设置hash过期时间
hash数据的过期时间只能缓存整个hash表的过期时间,不能设置单个hash字段的过期时间,所以过期时间要根据实际业务来设置,可以考虑把过期时间和hash字段一起缓存,查询的时候在判断是否已过期,已过期的情况直接情况直接清除当前字段缓存。
/**
* @Note 设置当前键的过期时间
* @DATA 2020-03-24 16:53:54
* @Author shenzx
* @param $table
* @param $expire
* @return bool
*/
private function handleExpire($table,$expire){
#获取过期时间 判断是否使用默认时间
$expire = $expire == 0 ? $this->expire : $expire;
#判断是否设置过期时间
if($expire < 0) return false;
#设置过期时间
$this->handler->expire($table,$expire);
return true;
}
数据json处理
/**
* @Note 数据json处理
* @DATA 2020-03-24 16:44:24
* @Author shenzx
* @param $data
* @param bool $is_enCode 是否json化 反之json_decode
* @return false|mixed|string
*/
private function json($data,$is_enCode = true){
#判断处理类型
if($is_enCode){
#json处理
return json_encode($data);
}else{
#json处理
return json_decode($data,true);
}
}