封裝一個redis操做類來操做hash格式

  最近項目要用redis,依然是基於tp3.2.php

  發現thinkphp3.2自帶的緩存類並很差使用,就本身封裝了一個redis

  目前只支持hash格式,其餘數據類型的操做後面用到的時候再補充thinkphp

  

<?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);
        }
    }
相關文章
相關標籤/搜索