PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > PHP教程 > 正文

    php redis缓存操作类

    作者:admin来源:网络浏览:时间:2021-01-28 16:47:15我要评论
    导读:封装php redis缓存操作类,集成了连接redis并判断连接是否成功,redis数据库选择,检测redis键是否存在,获取值,写入值,设置生存时间和删除清空操作。
    封装php redis缓存操作类,集成了连接redis并判断连接是否成功,redis数据库选择,检测redis键是否存在,获取值,写入值,设置生存时间和删除清空操作。

    php redis类代码:

    1. <?php 
    2. /** 
    3. *  redisdrive.class.php 
    4. * php redis 操作类 
    5. **/ 
    6. class redisdrive{ 
    7.     //键名 
    8.     public $key; 
    9.     //值 
    10.     public $value; 
    11.     //默认生存时间 
    12.     public $expire = 86400; /*60*60*24*/ 
    13.     //连接是否成功 
    14.     public $redis; 
    15.     //连接redis服务器ip 
    16.     public $ip = '127.0.0.1'
    17.     //端口 
    18.     public $port = 6379; 
    19.     //密码 
    20.     private $password = null
    21.     //数据库 
    22.     public $dbindex = 0; 
    23.   
    24.     /** 
    25.      * 自动连接到redis缓存 
    26.      */ 
    27.     public function __construct(){ 
    28.         //判断php是否支持redis扩展 
    29.         if(extension_loaded('redis')){ 
    30.             //实例化redis 
    31.             if($this->redis = new redis()){ 
    32.                 //ping连接 
    33.                 if(!$this->ping()){ 
    34.                     $this->redis = false
    35.                 }else
    36.                         //连接通后的数据库选择和密码验证操作 
    37.                     $this->redis -> select($this->dbindex); 
    38.                     $this->redis->auth($this->password); 
    39.                 } 
    40.             }else
    41.                 $this->redis = false
    42.             } 
    43.         }else
    44.             $this->redis = false
    45.         } 
    46.     } 
    47.   
    48.     /** 
    49.      * ping redis 的连通性 
    50.      */ 
    51.     public function ping(){ 
    52.         if($this->redis->connect($this->ip,$this->port)){ 
    53.             return true
    54.         }else
    55.            return false
    56.         } 
    57.     } 
    58.   
    59.     /** 
    60.      * 检测redis键是否存在 
    61.      */ 
    62.     public function exists(){ 
    63.         if($this->redis->exists($this->key)){ 
    64.             return true
    65.         }else
    66.             return false
    67.         } 
    68.     } 
    69.   
    70.     /** 
    71.      * 获取redis键的值 
    72.      */ 
    73.     public function get(){ 
    74.         if($this->exists()){ 
    75.             return json_decode($this->redis->get($this->key),true); 
    76.         }else
    77.             return false
    78.         } 
    79.     } 
    80.   
    81.     /** 
    82.      * 带生存时间写入key 
    83.      */ 
    84.     public function setex(){ 
    85.         return $this->redis->setex($this->key,$this->expire,json_encode($this->value)); 
    86.     } 
    87.   
    88.     /** 
    89.      * 设置redis键值 
    90.      */ 
    91.     public function set(){ 
    92.         if($this->redis->set($this->key,json_encode($this->value))){ 
    93.             return $this->redis->expire($this->key,$this->expire); 
    94.         }else
    95.             return false
    96.         } 
    97.     } 
    98.   
    99.     /** 
    100.      * 获取key生存时间 
    101.      */ 
    102.     public function ttl(){ 
    103.         return $this->redis->ttl($this->key); 
    104.     } 
    105.   
    106.     /** 
    107.      *删除key 
    108.      */ 
    109.     public function del(){ 
    110.         return $this->redis->del($this->key); 
    111.     } 
    112.   
    113.     /** 
    114.      * 清空所有数据 
    115.      */ 
    116.     public function flushall(){ 
    117.         return $this->redis->flushall(); 
    118.     } 
    119.   
    120.     /** 
    121.      * 获取所有key 
    122.      */ 
    123.     public function keys(){ 
    124.         return $this->redis->keys('*'); 
    125.     } 
    126.   

    实例化调用:

            判断redis是否可用和当设置了redis session 时redis不可用转回session文件操作。当redis可用时获取指定的键值如果存在就从redis中获取数据,如果不能存在就重新从数据库获取数据再写入redis,redis不可用时直接从数据库获取操作。

     

    1. //实例化 
    2. $redis = new redisdrive(); 
    3. $redis = false
    4. //是否可用判断 
    5. if($redis->redis==false){ 
    6.     $redis = false
    7.     //如果把session存入了redis则在redis不可用时把session切换回文件存储 
    8.     ini_set('session.save_handler','files'); 
    9.     ini_set('session.save_path','/tmp'); 
    10. }else
    11.     $redis = true
    12.   
    13. //redis判断和数据读取缓存操作设置 
    14. if($redis){ 
    15.     //设置redis键 
    16.     $redis->key = 'res'
    17.     //获取redis键的值 
    18.     $res_mysql = $redis->get(); 
    19.     //如果没获取到redis键的值得数据则从数据库获取并写入缓存 
    20.     if(!$res){ 
    21.         //设置生存时间 
    22.         $redis->expire = 60*60*6; 
    23.         //设置键 
    24.         $redis->key = 'res'
    25.   
    26.         //数据库获取数据$res 
    27.          
    28.         //获取到数据$res,赋值 
    29.         $redis->value = $res; 
    30.         //写入到redis中 
    31.         $redis->set();     
    32.     } 
    33. }else
    34.     //数据库获取数据 
    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-10-6492-1.html
    相关热词搜索: redis缓存