封装一个redis操作类来操作hash格式

  最近项目要用redis,依然是基于tp3.2.

  发现thinkphp3.2自带的缓存类并不好使用,就自己封装了一个

  目前只支持hash格式,其他数据类型的操作后面用到的时候再补充

  

<?php
    /**
     * Author:laomiao
     * Time:2018/07/03
     */
    namespace Org\Util;

    /**
     * redis实现类
     * @category   ORG
     * @package  ORG
     * @subpackage  Util
     */

    class Redis{
        //redis对象
        public $redis=null;
        
        /**
         * 构造方法
         * 设置初始化redis服务器信息
         */
        public function __construct(string $ip=null,string $password=null,int $port=null)
        {
            $redis = $this->connect($ip,$password,$port);
            if(is_object($redis)){
                $this->redis = $redis;
            }else{
                return false;
            }
        }


        /**
         * redis连接方法
         * @param string $ip 服务器ip
         * @param string $password 授权密码
         * @param string $prot 服务器端口
         * @return object $redis 返回redis连接对象  
         */
        private function connect(string $ip=null,string $password=null,int $port=null)
        {
            $ip = $ip ?? C("REDIS_HOST");
            $password = $password ?? C("REDIS_AUTH");
            $port = $port ?? C("REDIS_PORT");

            $redis = new \Redis();
            $connect= $redis->connect($ip, $port);
            if($connect){
                $redis->auth($password);
                return $redis;
            }else{
                return "redis服务器连接失败";
            }
        }
        
        
        /**
         * 增加hash字段值
         * @param string $key    哈希的键
         * @param string $field  字段名
         * @param mixed  $value  存储的值
         * @return bool  
         */
        public function hset(string $key, string $field,$value)
        {
            if($key != "" && $field != ""){
                if(is_array($value))    $value = json_encode($value);
                return $this->redis->hset($key,$field,$value);
            }else{
                return false;
            }
        }

        
        /**
         * 获取hash的某个key的某个field
         * @param string $key  某个hash的键名
         * @param string $field hash的字段名
         * @return mixed
         */
        public function hget(string $key,string $field)
        {
            $value = $this->redis->hget($key,$field);
            if(!$value){
                return "";
            }else{
                if($this->is_json($value)){
                    return json_decode($value,true);
                }else{
                    return $value;
                }
            }
        }

        /**
         * 获取hash的某个key的某个field或者多个field
         * @param string $key  某个hash的键名
         * @param string $field hash的字段名
         * @return array
         */
        public function hmget(string $key,string ...$fields):array
        {
            // dump($fields);  t1   t2
            $this->key=$key;
            //通过array_map获取所有的field对应的数据,但是该结果却是索引形式
            $data = array_map(function(string $field){
                $list = $this->redis->hget($this->key,$field);
                if($this->is_json($list)){
                   $tmp = json_decode($list,true); 
                }else{
                   $tmp = $list; 
                }
                return $tmp;
            },$fields);

            //遍历上述结果 将field对应到结果上
            $result=[];
            foreach($data as $key=>$vo){
                $result[$fields[$key]] = $vo;
            }
            return $result;
        }


        /**
         * 获取hash的某个key的所有field
         * @param $key 键名
         * @return array
         */
        public function hgetall(string $key):array
        {
            $key = $key??'';
            $data = $this->redis->hgetall($key);
            if(count($data)>1){
                foreach($data as $key=>$vo){
                    if($this->is_json($vo)) $tmp[$key]=json_decode($vo,true);
                    else $tmp[$key] = $vo;
                }
                $data=$tmp;
            }
            return $data;
        }

        /**
         * 判断哈希某key的某个field是否存在
         * @param string $key  键名
         * @param string $field 字段名
         * @return bool
         */
        public function hexists(string $key,string $field):bool
        {
            $result = $this->redis->hexists($key,$field);
            if($result == 1){
                return true;
            }else{
                return false;
            }
        } 

        /**
         * 读取redis中某键的所有key
         * @param string $key
         * @return array/bool
         */
        public function hkeys(string $key)
        {
            if($key){
                $result = $this->redis->hkeys($key);
                if(count($result)>=1){
                    return $result;
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }
       
        /**
         * 判断哈希中某个键中存储多少个键值
         * @param string $key
         * @return int
         */
        public function hlen(string $key):int
        {
            if($key){
                return $this->redis->hlen($key);
            }else{
                return false;
            }
        }

        /**
         * 获取哈希中某键中存储的所有值
         * @param string $key
         * @return array/bool
         */
        public function hvals(string $key)
        {   
            if($key){
                $result = $this->redis->hvals($key);
                if(count($result)>=1){
                    return $result;
                }else{
                    return false;
                }
            }else{
                return false;
            }
        }

        /**
         * 删除hash中某个key中的某些个field
         * @param string $key
         * @param string $fields
         * @return bool
         */
        public function hdel(string $key,string ...$fields):bool
        {
            if(!$key) return false;
            $this->key = $key;
            array_map(function(string $field){
                return $this->redis->hdel($this->key,$field);
            },$fields);
            return true;            
        }
    
        /**
         * 判断字符串是否是json格式
         * @param string $str 要判断的字符串
         */
        private function is_json(string $str):bool
        {
            json_decode($str);
            return (json_last_error() == JSON_ERROR_NONE);
        }
    }

猜你喜欢

转载自www.cnblogs.com/sicada/p/9269146.html
今日推荐