[转]initphp redis类

  1. /********************************************************************************* 

  2.  * InitPHP 2.0 国产PHP开发框架  Dao-Nosql-Redis 

  3.  *------------------------------------------------------------------------------- 

  4.  * 版权所有: CopyRight By initphp.com 

  5.  * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己 

  6.  *------------------------------------------------------------------------------- 

  7.  * $Author:zhuli 

  8.  * $Dtime:2011-10-09  

  9. ***********************************************************************************/  

  10. class redisInit {  

  11.       

  12.     private $redis//redis对象  

  13.       

  14.     /** 

  15.      * 初始化Redis 

  16.      * $config = array( 

  17.      *  'server' => '127.0.0.1' 服务器 

  18.      *  'port'   => '6379' 端口号 

  19.      * ) 

  20.      * @param array $config 

  21.      */  

  22.     public function init($config = array()) {  

  23.         if ($config['server'] == '')  $config['server'] = '127.0.0.1';  

  24.         if ($config['port'] == '')  $config['port'] = '6379';  

  25.         $this->redis = new Redis();  

  26.         $this->redis->connect($config['server'], $config['port']);  

  27.         return $this->redis;  

  28.     }  

  29.       

  30.     /** 

  31.      * 设置值 

  32.      * @param string $key KEY名称 

  33.      * @param string|array $value 获取得到的数据 

  34.      * @param int $timeOut 时间 

  35.      */  

  36.     public function set($key$value$timeOut = 0) {  

  37.         $value = json_encode($value, TRUE);  

  38.         $retRes = $this->redis->set($key$value);  

  39.         if ($timeOut > 0) $this->redis->setTimeout($key$timeOut);  

  40.         return $retRes;  

  41.     }  

  42.   

  43.     /** 

  44.      * 通过KEY获取数据 

  45.      * @param string $key KEY名称 

  46.      */  

  47.     public function get($key) {  

  48.         $result = $this->redis->get($key);  

  49.         return json_decode($result, TRUE);  

  50.     }  

  51.       

  52.     /** 

  53.      * 删除一条数据 

  54.      * @param string $key KEY名称 

  55.      */  

  56.     public function delete($key) {  

  57.         return $this->redis->delete($key);  

  58.     }  

  59.       

  60.     /** 

  61.      * 清空数据 

  62.      */  

  63.     public function flushAll() {  

  64.         return $this->redis->flushAll();  

  65.     }  

  66.       

  67.     /** 

  68.      * 数据入队列 

  69.      * @param string $key KEY名称 

  70.      * @param string|array $value 获取得到的数据 

  71.      * @param bool $right 是否从右边开始入 

  72.      */  

  73.     public function push($key$value ,$right = true) {  

  74.         $value = json_encode($value);  

  75.         return $right ? $this->redis->rPush($key$value) : $this->redis->lPush($key$value);  

  76.     }  

  77.       

  78.     /** 

  79.      * 数据出队列 

  80.      * @param string $key KEY名称 

  81.      * @param bool $left 是否从左边开始出数据 

  82.      */  

  83.     public function pop($key , $left = true) {  

  84.         $val = $left ? $this->redis->lPop($key) : $this->redis->rPop($key);  

  85.         return json_decode($val);  

  86.     }  

  87.       

  88.     /** 

  89.      * 数据自增 

  90.      * @param string $key KEY名称 

  91.      */  

  92.     public function increment($key) {  

  93.         return $this->redis->incr($key);  

  94.     }  

  95.   

  96.     /** 

  97.      * 数据自减 

  98.      * @param string $key KEY名称 

  99.      */  

  100.     public function decrement($key) {  

  101.         return $this->redis->decr($key);  

  102.     }  

  103.       

  104.     /** 

  105.      * key是否存在,存在返回ture 

  106.      * @param string $key KEY名称 

  107.      */  

  108.     public function exists($key) {  

  109.         return $this->redis->exists($key);  

  110.     }  

  111.       

  112.     /** 

  113.      * 返回redis对象 

  114.      * redis有非常多的操作方法,我们只封装了一部分 

  115.      * 拿着这个对象就可以直接调用redis自身方法 

  116.      */  

  117.     public function redis() {  

  118.         return $this->redis;  

  119.     }  

  120. }  


你可能感兴趣的:(redis,initphp)