当前位置:首页 > PHP教程 > php高级应用 > 列表

PHP实现的Redis操作通用类示例

发布:smiling 来源: PHP粉丝网  添加日期:2021-10-05 13:53:37 浏览: 评论:0 

这篇文章主要介绍了PHP实现的Redis操作通用类,结合实例形式分析了php实现的redis连接、队列、集合、hash表、事务等相关操作封装技巧,需要的朋友可以参考下。

本文实例讲述了PHP实现的Redis操作通用类,分享给大家供大家参考,具体如下:

找到一个比较全的Redis PHP操作类库,分享给大家

  1. <?php 
  2. /** 
  3.  * redis操作类 
  4.  * 说明,任何为false的串,存在redis中都是空串。 
  5.  * 只有在key不存在时,才会返回false。 
  6.  * 这点可用于防止缓存穿透 
  7.  * 
  8.  */ 
  9. class Redis 
  10. { 
  11.   private $redis; 
  12.   //当前数据库ID号 
  13.   protected $dbId=0; 
  14.   //当前权限认证码 
  15.   protected $auth; 
  16.   /** 
  17.    * 实例化的对象,单例模式. 
  18.    * @var \iphp\db\Redis 
  19.    */ 
  20.   static private $_instance=array(); 
  21.   private $k; 
  22.   //连接属性数组 
  23.   protected $attr=array( 
  24.     //连接超时时间,redis配置文件中默认为300秒 
  25.     'timeout'=>30, 
  26.     //选择的数据库。 
  27.     'db_id'=>0, 
  28.   ); 
  29.   //什么时候重新建立连接 
  30.   protected $expireTime; 
  31.   protected $host; 
  32.   protected $port; 
  33.   private function __construct($config,$attr=array()) 
  34.   { 
  35.     $this->attr    =  array_merge($this->attr,$attr); 
  36.     $this->redis  =  new Redis(); 
  37.     $this->port    =  $config['port'] ? $config['port'] : 6379; 
  38.     $this->host    =  $config['host']; 
  39.     $this->redis->connect($this->host, $this->port, $this->attr['timeout']); 
  40.     if($config['auth']) 
  41.     { 
  42.       $this->auth($config['auth']); 
  43.       $this->auth  =  $config['auth']; 
  44.     } 
  45.     $this->expireTime  =  time() + $this->attr['timeout']; 
  46.   } 
  47.   /** 
  48.    * 得到实例化的对象. 
  49.    * 为每个数据库建立一个连接 
  50.    * 如果连接超时,将会重新建立一个连接 
  51.    * @param array $config 
  52.    * @param int $dbId 
  53.    * @return \iphp\db\Redis 
  54.    */ 
  55.   public static function getInstance($config, $attr = array()) 
  56.   { 
  57.     //如果是一个字符串,将其认为是数据库的ID号。以简化写法。 
  58.     if(!is_array($attr)) 
  59.     { 
  60.       $dbId  =  $attr; 
  61.       $attr  =  array(); 
  62.       $attr['db_id']  =  $dbId; 
  63.     } 
  64.     $attr['db_id']  =  $attr['db_id'] ? $attr['db_id'] : 0; 
  65.     $k  =  md5(implode('', $config).$attr['db_id']); 
  66.     if(! (static::$_instance[$k] instanceof self)) 
  67.     { 
  68.       static::$_instance[$k] = new self($config,$attr); 
  69.       static::$_instance[$k]->k    =  $k; 
  70.       static::$_instance[$k]->dbId  =  $attr['db_id']; 
  71.       //如果不是0号库,选择一下数据库。 
  72.       if($attr['db_id'] != 0){ 
  73.         static::$_instance[$k]->select($attr['db_id']); 
  74.       } 
  75.     } 
  76.     elseif( time() > static::$_instance[$k]->expireTime) 
  77.     { 
  78.       static::$_instance[$k]->close(); 
  79.       static::$_instance[$k]     =   new self($config,$attr); 
  80.       static::$_instance[$k]->k  =  $k; 
  81.       static::$_instance[$k]->dbId=  $attr['db_id']; 
  82.       //如果不是0号库,选择一下数据库。 
  83.       if($attr['db_id']!=0){ 
  84.         static::$_instance[$k]->select($attr['db_id']); 
  85.       } 
  86.     } 
  87.     return static::$_instance[$k]; 
  88.   } 
  89.   private function __clone(){} 
  90.   /** 
  91.    * 执行原生的redis操作 
  92.    * @return \Redis 
  93.    */ 
  94.   public function getRedis() 
  95.   { 
  96.     return $this->redis; 
  97.   } 
  98.   /*****************hash表操作函数*******************/ 
  99.   /** 
  100.    * 得到hash表中一个字段的值 
  101.    * @param string $key 缓存key 
  102.    * @param string $field 字段 
  103.    * @return string|false 
  104.    */ 
  105.   public function hGet($key,$field) 
  106.   { 
  107.     return $this->redis->hGet($key,$field); 
  108.   } 
  109.   /** 
  110.    * 为hash表设定一个字段的值 
  111.    * @param string $key 缓存key 
  112.    * @param string $field 字段 
  113.    * @param string $value 值。 
  114.    * @return bool  
  115.    */ 
  116.   public function hSet($key,$field,$value) 
  117.   { 
  118.     return $this->redis->hSet($key,$field,$value); 
  119.   } 
  120.   /** 
  121.    * 判断hash表中,指定field是不是存在 
  122.    * @param string $key 缓存key 
  123.    * @param string $field 字段 
  124.    * @return bool 
  125.    */ 
  126.   public function hExists($key,$field) 
  127.   { 
  128.     return $this->redis->hExists($key,$field); 
  129.   } 
  130.   /** 
  131.    * 删除hash表中指定字段 ,支持批量删除 
  132.    * @param string $key 缓存key 
  133.    * @param string $field 字段 
  134.    * @return int 
  135.    */ 
  136.   public function hdel($key,$field) 
  137.   { 
  138.     $fieldArr=explode(',',$field); 
  139.     $delNum=0; 
  140.     foreach($fieldArr as $row) 
  141.     { 
  142.       $row=trim($row); 
  143.       $delNum+=$this->redis->hDel($key,$row); 
  144.     } 
  145.     return $delNum; 
  146.   } 
  147.   /** 
  148.    * 返回hash表元素个数 
  149.    * @param string $key 缓存key 
  150.    * @return int|bool 
  151.    */ 
  152.   public function hLen($key) 
  153.   { 
  154.     return $this->redis->hLen($key); 
  155.   } 
  156.   /** 
  157.    * 为hash表设定一个字段的值,如果字段存在,返回false 
  158.    * @param string $key 缓存key 
  159.    * @param string $field 字段 
  160.    * @param string $value 值。 
  161.    * @return bool 
  162.    */ 
  163.   public function hSetNx($key,$field,$value) 
  164.   { 
  165.     return $this->redis->hSetNx($key,$field,$value); 
  166.   } 
  167.   /** 
  168.    * 为hash表多个字段设定值。 
  169.    * @param string $key 
  170.    * @param array $value 
  171.    * @return array|bool 
  172.    */ 
  173.   public function hMset($key,$value) 
  174.   { 
  175.     if(!is_array($value)) 
  176.       return false; 
  177.     return $this->redis->hMset($key,$value);  
  178.   } 
  179.   /** 
  180.    * 为hash表多个字段设定值。 
  181.    * @param string $key 
  182.    * @param array|string $value string以','号分隔字段 
  183.    * @return array|bool 
  184.    */ 
  185.   public function hMget($key,$field) 
  186.   { 
  187.     if(!is_array($field)) 
  188.       $field=explode(',', $field); 
  189.     return $this->redis->hMget($key,$field); 
  190.   } 
  191.   /** 
  192.    * 为hash表设这累加,可以负数 
  193.    * @param string $key 
  194.    * @param int $field 
  195.    * @param string $value 
  196.    * @return bool 
  197.    */ 
  198.   public function hIncrBy($key,$field,$value) 
  199.   { 
  200.     $value=intval($value); 
  201.     return $this->redis->hIncrBy($key,$field,$value); 
  202.   } 
  203.   /** 
  204.    * 返回所有hash表的所有字段 
  205.    * @param string $key 
  206.    * @return array|bool 
  207.    */ 
  208.   public function hKeys($key) 
  209.   { 
  210.     return $this->redis->hKeys($key); 
  211.   } 
  212.   /** 
  213.    * 返回所有hash表的字段值,为一个索引数组 
  214.    * @param string $key 
  215.    * @return array|bool 
  216.    */ 
  217.   public function hVals($key) 
  218.   { 
  219.     return $this->redis->hVals($key); 
  220.   } 
  221.   /** 
  222.    * 返回所有hash表的字段值,为一个关联数组 
  223.    * @param string $key 
  224.    * @return array|bool 
  225.    */ 
  226.   public function hGetAll($key) 
  227.   { 
  228.     return $this->redis->hGetAll($key); 
  229.   } 
  230.   /*********************有序集合操作*********************/ 
  231.   /** 
  232.    * 给当前集合添加一个元素 
  233.    * 如果value已经存在,会更新order的值。 
  234.    * @param string $key 
  235.    * @param string $order 序号 
  236.    * @param string $value 值 
  237.    * @return bool 
  238.    */ 
  239.   public function zAdd($key,$order,$value) 
  240.   { 
  241.     return $this->redis->zAdd($key,$order,$value);   
  242.   } 
  243.   /** 
  244.    * 给$value成员的order值,增加$num,可以为负数 
  245.    * @param string $key 
  246.    * @param string $num 序号 
  247.    * @param string $value 值 
  248.    * @return 返回新的order 
  249.    */ 
  250.   public function zinCry($key,$num,$value) 
  251.   { 
  252.     return $this->redis->zinCry($key,$num,$value); 
  253.   } 
  254.   /** 
  255.    * 删除值为value的元素 
  256.    * @param string $key 
  257.    * @param stirng $value 
  258.    * @return bool 
  259.    */ 
  260.   public function zRem($key,$value) 
  261.   { 
  262.     return $this->redis->zRem($key,$value); 
  263.   } 
  264.   /** 
  265.    * 集合以order递增排列后,0表示第一个元素,-1表示最后一个元素 
  266.    * @param string $key 
  267.    * @param int $start 
  268.    * @param int $end 
  269.    * @return array|bool 
  270.    */ 
  271.   public function zRange($key,$start,$end) 
  272.   { 
  273.     return $this->redis->zRange($key,$start,$end); 
  274.   } 
  275.   /** 
  276.    * 集合以order递减排列后,0表示第一个元素,-1表示最后一个元素 
  277.    * @param string $key 
  278.    * @param int $start 
  279.    * @param int $end 
  280.    * @return array|bool 
  281.    */ 
  282.   public function zRevRange($key,$start,$end) 
  283.   { 
  284.     return $this->redis->zRevRange($key,$start,$end); 
  285.   } 
  286.   /** 
  287.    * 集合以order递增排列后,返回指定order之间的元素。 
  288.    * min和max可以是-inf和+inf 表示最大值,最小值 
  289.    * @param string $key 
  290.    * @param int $start 
  291.    * @param int $end 
  292.    * @package array $option 参数 
  293.    *   withscores=>true,表示数组下标为Order值,默认返回索引数组 
  294.    *   limit=>array(0,1) 表示从0开始,取一条记录。 
  295.    * @return array|bool 
  296.    */ 
  297.   public function zRangeByScore($key,$start='-inf',$end="+inf",$option=array()) 
  298.   { 
  299.     return $this->redis->zRangeByScore($key,$start,$end,$option); 
  300.   } 
  301.   /** 
  302.    * 集合以order递减排列后,返回指定order之间的元素。 
  303.    * min和max可以是-inf和+inf 表示最大值,最小值 
  304.    * @param string $key 
  305.    * @param int $start 
  306.    * @param int $end 
  307.    * @package array $option 参数 
  308.    *   withscores=>true,表示数组下标为Order值,默认返回索引数组 
  309.    *   limit=>array(0,1) 表示从0开始,取一条记录。 
  310.    * @return array|bool 
  311.    */ 
  312.   public function zRevRangeByScore($key,$start='-inf',$end="+inf",$option=array()) 
  313.   { 
  314.     return $this->redis->zRevRangeByScore($key,$start,$end,$option); 
  315.   } 
  316.   /** 
  317.    * 返回order值在start end之间的数量 
  318.    * @param unknown $key 
  319.    * @param unknown $start 
  320.    * @param unknown $end 
  321.    */ 
  322.   public function zCount($key,$start,$end) 
  323.   { 
  324.     return $this->redis->zCount($key,$start,$end); 
  325.   } 
  326.   /** 
  327.    * 返回值为value的order值 
  328.    * @param unknown $key 
  329.    * @param unknown $value 
  330.    */ 
  331.   public function zScore($key,$value) 
  332.   { 
  333.     return $this->redis->zScore($key,$value); 
  334.   } 
  335.   /** 
  336.    * 返回集合以score递增加排序后,指定成员的排序号,从0开始。 
  337.    * @param unknown $key 
  338.    * @param unknown $value 
  339.    */ 
  340.   public function zRank($key,$value) 
  341.   { 
  342.     return $this->redis->zRank($key,$value); 
  343.   } 
  344.   /** 
  345.    * 返回集合以score递增加排序后,指定成员的排序号,从0开始。 
  346.    * @param unknown $key 
  347.    * @param unknown $value 
  348.    */ 
  349.   public function zRevRank($key,$value) 
  350.   { 
  351.     return $this->redis->zRevRank($key,$value); 
  352.   } 
  353.   /** 
  354.    * 删除集合中,score值在start end之间的元素 包括start end 
  355.    * min和max可以是-inf和+inf 表示最大值,最小值 
  356.    * @param unknown $key 
  357.    * @param unknown $start 
  358.    * @param unknown $end 
  359.    * @return 删除成员的数量。 
  360.    */ 
  361.   public function zRemRangeByScore($key,$start,$end) 
  362.   { 
  363.     return $this->redis->zRemRangeByScore($key,$start,$end); 
  364.   } 
  365.   /** 
  366.    * 返回集合元素个数。 
  367.    * @param unknown $key 
  368.    */ 
  369.   public function zCard($key) 
  370.   { 
  371.     return $this->redis->zCard($key); 
  372.   } 
  373.   /*********************队列操作命令************************/ 
  374.   /** 
  375.    * 在队列尾部插入一个元素 
  376.    * @param unknown $key 
  377.    * @param unknown $value 
  378.    * 返回队列长度 
  379.    */ 
  380.   public function rPush($key,$value) 
  381.   { 
  382.     return $this->redis->rPush($key,$value);  
  383.   } 
  384.   /** 
  385.    * 在队列尾部插入一个元素 如果key不存在,什么也不做 
  386.    * @param unknown $key 
  387.    * @param unknown $value 
  388.    * 返回队列长度 
  389.    */ 
  390.   public function rPushx($key,$value) 
  391.   { 
  392.     return $this->redis->rPushx($key,$value); 
  393.   } 
  394.   /** 
  395.    * 在队列头部插入一个元素 
  396.    * @param unknown $key 
  397.    * @param unknown $value 
  398.    * 返回队列长度 
  399.    */ 
  400.   public function lPush($key,$value) 
  401.   { 
  402.     return $this->redis->lPush($key,$value); 
  403.   } 
  404.   /** 
  405.    * 在队列头插入一个元素 如果key不存在,什么也不做 
  406.    * @param unknown $key 
  407.    * @param unknown $value 
  408.    * 返回队列长度 
  409.    */ 
  410.   public function lPushx($key,$value) 
  411.   { 
  412.     return $this->redis->lPushx($key,$value); 
  413.   } 
  414.   /** 
  415.    * 返回队列长度 
  416.    * @param unknown $key 
  417.    */ 
  418.   public function lLen($key) 
  419.   { 
  420.     return $this->redis->lLen($key);  
  421.   } 
  422.   /** 
  423.    * 返回队列指定区间的元素 
  424.    * @param unknown $key 
  425.    * @param unknown $start 
  426.    * @param unknown $end 
  427.    */ 
  428.   public function lRange($key,$start,$end) 
  429.   { 
  430.     return $this->redis->lrange($key,$start,$end); 
  431.   } 
  432.   /** 
  433.    * 返回队列中指定索引的元素 
  434.    * @param unknown $key 
  435.    * @param unknown $index 
  436.    */ 
  437.   public function lIndex($key,$index) 
  438.   { 
  439.     return $this->redis->lIndex($key,$index); 
  440.   } 
  441.   /** 
  442.    * 设定队列中指定index的值。 
  443.    * @param unknown $key 
  444.    * @param unknown $index 
  445.    * @param unknown $value 
  446.    */ 
  447.   public function lSet($key,$index,$value) 
  448.   { 
  449.     return $this->redis->lSet($key,$index,$value); 
  450.   } 
  451.   /** 
  452.    * 删除值为vaule的count个元素 
  453.    * PHP-REDIS扩展的数据顺序与命令的顺序不太一样,不知道是不是bug 
  454.    * count>0 从尾部开始 
  455.    * >0 从头部开始 
  456.    * =0 删除全部 
  457.    * @param unknown $key 
  458.    * @param unknown $count 
  459.    * @param unknown $value 
  460.    */ 
  461.   public function lRem($key,$count,$value) 
  462.   { 
  463.     return $this->redis->lRem($key,$value,$count); 
  464.   } 
  465.   /** 
  466.    * 删除并返回队列中的头元素。 
  467.    * @param unknown $key 
  468.    */ 
  469.   public function lPop($key) 
  470.   { 
  471.     return $this->redis->lPop($key); 
  472.   } 
  473.   /** 
  474.    * 删除并返回队列中的尾元素 
  475.    * @param unknown $key 
  476.    */ 
  477.   public function rPop($key) 
  478.   { 
  479.     return $this->redis->rPop($key); 
  480.   } 
  481.   /*************redis字符串操作命令*****************/ 
  482.   /** 
  483.    * 设置一个key 
  484.    * @param unknown $key 
  485.    * @param unknown $value 
  486.    */ 
  487.   public function set($key,$value) 
  488.   { 
  489.     return $this->redis->set($key,$value); 
  490.   } 
  491.   /** 
  492.    * 得到一个key 
  493.    * @param unknown $key 
  494.    */ 
  495.   public function get($key) 
  496.   { 
  497.     return $this->redis->get($key); 
  498.   } 
  499.   /** 
  500.    * 设置一个有过期时间的key 
  501.    * @param unknown $key 
  502.    * @param unknown $expire 
  503.    * @param unknown $value 
  504.    */ 
  505.   public function setex($key,$expire,$value) 
  506.   { 
  507.     return $this->redis->setex($key,$expire,$value); 
  508.   } 
  509.   /** 
  510.    * 设置一个key,如果key存在,不做任何操作. 
  511.    * @param unknown $key 
  512.    * @param unknown $value 
  513.    */ 
  514.   public function setnx($key,$value) 
  515.   { 
  516.     return $this->redis->setnx($key,$value); 
  517.   } 
  518.   /** 
  519.    * 批量设置key 
  520.    * @param unknown $arr 
  521.    */ 
  522.   public function mset($arr) 
  523.   { 
  524.     return $this->redis->mset($arr); 
  525.   } 
  526.   /*************redis 无序集合操作命令*****************/ 
  527.   /** 
  528.    * 返回集合中所有元素 
  529.    * @param unknown $key 
  530.    */ 
  531.   public function sMembers($key) 
  532.   { 
  533.     return $this->redis->sMembers($key); 
  534.   } 
  535.   /** 
  536.    * 求2个集合的差集 
  537.    * @param unknown $key1 
  538.    * @param unknown $key2 
  539.    */ 
  540.   public function sDiff($key1,$key2) 
  541.   { 
  542.     return $this->redis->sDiff($key1,$key2); 
  543.   } 
  544.   /** 
  545.    * 添加集合。由于版本问题,扩展不支持批量添加。这里做了封装 
  546.    * @param unknown $key 
  547.    * @param string|array $value 
  548.    */ 
  549.   public function sAdd($key,$value) 
  550.   { 
  551.     if(!is_array($value)) 
  552.       $arr=array($value); 
  553.     else 
  554.       $arr=$value; 
  555.     foreach($arr as $row) 
  556.       $this->redis->sAdd($key,$row); 
  557.   } 
  558.   /** 
  559.    * 返回无序集合的元素个数 
  560.    * @param unknown $key 
  561.    */ 
  562.   public function scard($key) 
  563.   { 
  564.     return $this->redis->scard($key); 
  565.   } 
  566.   /** 
  567.    * 从集合中删除一个元素 
  568.    * @param unknown $key 
  569.    * @param unknown $value 
  570.    */ 
  571.   public function srem($key,$value) 
  572.   { 
  573.     return $this->redis->srem($key,$value); 
  574.   } 
  575.   /*************redis管理操作命令*****************/ 
  576.   /** 
  577.    * 选择数据库 
  578.    * @param int $dbId 数据库ID号 
  579.    * @return bool 
  580.    */ 
  581.   public function select($dbId) 
  582.   { 
  583.     $this->dbId=$dbId; 
  584.     return $this->redis->select($dbId); 
  585.   } 
  586.   /** 
  587.    * 清空当前数据库 
  588.    * @return bool 
  589.    */ 
  590.   public function flushDB() 
  591.   { 
  592.     return $this->redis->flushDB(); 
  593.   } 
  594.   /** 
  595.    * 返回当前库状态 
  596.    * @return array 
  597.    */ 
  598.   public function info() 
  599.   { 
  600.     return $this->redis->info(); 
  601.   } 
  602.   /** 
  603.    * 同步保存数据到磁盘 
  604.    */ 
  605.   public function save() 
  606.   { 
  607.     return $this->redis->save(); 
  608.   } 
  609.   /** 
  610.    * 异步保存数据到磁盘 
  611.    */ 
  612.   public function bgSave() 
  613.   { 
  614.     return $this->redis->bgSave(); 
  615.   } 
  616.   /** 
  617.    * 返回最后保存到磁盘的时间 
  618.    */ 
  619.   public function lastSave() 
  620.   { 
  621.     return $this->redis->lastSave(); 
  622.   } 
  623.   /** 
  624.    * 返回key,支持*多个字符,?一个字符 
  625.    * 只有* 表示全部 
  626.    * @param string $key 
  627.    * @return array 
  628.    */ 
  629.   public function keys($key) 
  630.   { 
  631.     return $this->redis->keys($key); 
  632.   } 
  633.   /** 
  634.    * 删除指定key 
  635.    * @param unknown $key 
  636.    */ 
  637.   public function del($key) 
  638.   { 
  639.     return $this->redis->del($key); 
  640.   } 
  641.   /** 
  642.    * 判断一个key值是不是存在 
  643.    * @param unknown $key 
  644.    */ 
  645.   public function exists($key) 
  646.   { 
  647.     return $this->redis->exists($key); 
  648.   } 
  649.   /** 
  650.    * 为一个key设定过期时间 单位为秒 
  651.    * @param unknown $key 
  652.    * @param unknown $expire 
  653.    */ 
  654.   public function expire($key,$expire) 
  655.   { 
  656.     return $this->redis->expire($key,$expire); 
  657.   } 
  658.   /** 
  659.    * 返回一个key还有多久过期,单位秒 
  660.    * @param unknown $key 
  661.    */ 
  662.   public function ttl($key) 
  663.   { 
  664.     return $this->redis->ttl($key); 
  665.   } 
  666.   /** 
  667.    * 设定一个key什么时候过期,time为一个时间戳 
  668.    * @param unknown $key 
  669.    * @param unknown $time 
  670.    */ 
  671.   public function exprieAt($key,$time) 
  672.   { 
  673.     return $this->redis->expireAt($key,$time); 
  674.   } 
  675.   /** 
  676.    * 关闭服务器链接 
  677.    */ 
  678.   public function close() 
  679.   { 
  680.     return $this->redis->close(); 
  681.   } 
  682.   /** 
  683.    * 关闭所有连接 
  684.    */ 
  685.   public static function closeAll() 
  686.   { 
  687.     foreach(static::$_instance as $o) 
  688.     { 
  689.       if($o instanceof self) 
  690.         $o->close(); 
  691.     } 
  692.   } 
  693.   /** 这里不关闭连接,因为session写入会在所有对象销毁之后。 
  694.   public function __destruct() 
  695.   { 
  696.     return $this->redis->close(); 
  697.   } 
  698.   **/ 
  699.   /** 
  700.    * 返回当前数据库key数量 
  701.    */ 
  702.   public function dbSize() 
  703.   { 
  704.     return $this->redis->dbSize(); 
  705.   } 
  706.   /** 
  707.    * 返回一个随机key 
  708.    */ 
  709.   public function randomKey() 
  710.   { 
  711.     return $this->redis->randomKey(); 
  712.   } 
  713.   /** 
  714.    * 得到当前数据库ID 
  715.    * @return int 
  716.    */ 
  717.   public function getDbId() 
  718.   { 
  719.     return $this->dbId; 
  720.   } 
  721.   /** 
  722.    * 返回当前密码 
  723.    */ 
  724.   public function getAuth() 
  725.   { 
  726.     return $this->auth; 
  727.   } 
  728.   public function getHost() 
  729.   { 
  730.     return $this->host; 
  731.   } 
  732.   public function getPort() 
  733.   { 
  734.     return $this->port; 
  735.   } 
  736.   public function getConnInfo() 
  737.   { 
  738.     return array( 
  739.       'host'=>$this->host, 
  740.       'port'=>$this->port, 
  741.       'auth'=>$this->auth 
  742.     ); 
  743.   } 
  744.   /*********************事务的相关方法************************/ 
  745.   /** 
  746.    * 监控key,就是一个或多个key添加一个乐观锁 
  747.    * 在此期间如果key的值如果发生的改变,刚不能为key设定值 
  748.    * 可以重新取得Key的值。 
  749.    * @param unknown $key 
  750.    */ 
  751.   public function watch($key) 
  752.   { 
  753.     return $this->redis->watch($key); 
  754.   } 
  755.   /** 
  756.    * 取消当前链接对所有key的watch 
  757.    * EXEC 命令或 DISCARD 命令先被执行了的话,那么就不需要再执行 UNWATCH 了 
  758.    */ 
  759.   public function unwatch() 
  760.   { 
  761.     return $this->redis->unwatch(); 
  762.   } 
  763.   /** 
  764.    * 开启一个事务 
  765.    * 事务的调用有两种模式Redis::MULTI和Redis::PIPELINE, 
  766.    * 默认是Redis::MULTI模式, 
  767.    * Redis::PIPELINE管道模式速度更快,但没有任何保证原子性有可能造成数据的丢失 
  768.    */ 
  769.   public function multi($type=\Redis::MULTI) 
  770.   { 
  771.     return $this->redis->multi($type); 
  772.   } 
  773.   /** 
  774.    * 执行一个事务 
  775.    * 收到 EXEC 命令后进入事务执行,事务中任意命令执行失败,其余的命令依然被执行 
  776.    */ 
  777.   public function exec() 
  778.   { 
  779.     return $this->redis->exec(); 
  780.   } 
  781.   /** 
  782.    * 回滚一个事务 
  783.    */ 
  784.   public function discard() 
  785.   { 
  786.     return $this->redis->discard(); 
  787.   } 
  788.   /** 
  789.    * 测试当前链接是不是已经失效 
  790.    * 没有失效返回+PONG 
  791.    * 失效返回false 
  792.    */ 
  793.   public function ping() 
  794.   { 
  795.     return $this->redis->ping(); 
  796.   } 
  797.   public function auth($auth) 
  798.   { 
  799.     return $this->redis->auth($auth); 
  800.   } 
  801.   /*********************自定义的方法,用于简化操作************************/ 
  802.   /** 
  803.    * 得到一组的ID号 
  804.    * @param unknown $prefix 
  805.    * @param unknown $ids 
  806.    */ 
  807.   public function hashAll($prefix,$ids) 
  808.   { 
  809.     if($ids==false) 
  810.       return false; 
  811.     if(is_string($ids)) 
  812.       $ids=explode(',', $ids); 
  813.     $arr=array(); 
  814.     foreach($ids as $id) 
  815.     { 
  816.       $key=$prefix.'.'.$id; 
  817.       $res=$this->hGetAll($key); 
  818.       if($res!=false) 
  819.         $arr[]=$res; 
  820.     } 
  821.     return $arr; 
  822.   } 
  823.   /** 
  824.    * 生成一条消息,放在redis数据库中。使用0号库。 
  825.    * @param string|array $msg 
  826.    */ 
  827.   public function pushMessage($lkey,$msg) 
  828.   { 
  829.     if(is_array($msg)){ 
  830.       $msg  =  json_encode($msg); 
  831.     } 
  832.     $key  =  md5($msg); 
  833.     //如果消息已经存在,删除旧消息,已当前消息为准 
  834.     //echo $n=$this->lRem($lkey, 0, $key)."\n"; 
  835.     //重新设置新消息 
  836.     $this->lPush($lkey, $key); 
  837.     $this->setex($key, 3600, $msg); 
  838.     return $key; 
  839.   } 
  840.   /** 
  841.    * 得到条批量删除key的命令 
  842.    * @param unknown $keys 
  843.    * @param unknown $dbId 
  844.    */ 
  845.   public function delKeys($keys,$dbId) 
  846.   { 
  847.     $redisInfo=$this->getConnInfo(); 
  848.     $cmdArr=array( 
  849.       'redis-cli', 
  850.       '-a', 
  851.       $redisInfo['auth'], 
  852.       '-h', 
  853.       $redisInfo['host'], 
  854.       '-p', 
  855.       $redisInfo['port'], 
  856.       '-n', 
  857.       $dbId, 
  858.     ); 
  859.     $redisStr=implode(' ', $cmdArr); 
  860.     $cmd="{$redisStr} KEYS \"{$keys}\" | xargs {$redisStr} del"; 
  861.     return $cmd; 
  862.   } 
  863. }

Tags: Redis通用类

分享到: