[轉]initphp redis類

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

  2.  * InitPHP 2.0 國產PHP開發框架  Dao-Nosql-Redis redis

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

  4.  * 版權全部: CopyRight By initphp.com json

  5.  * 您能夠自由使用該源碼,可是在使用過程當中,請保留做者信息。尊重他人勞動成果就是尊重本身 服務器

  6.  *------------------------------------------------------------------------------- 框架

  7.  * $Author:zhuli this

  8.  * $Dtime:2011-10-09  spa

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

  10. class redisInit {  server

  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. }  

相關文章
相關標籤/搜索