thinkphp 通過Redis實現增刪改查操作

thinkphp 通過Redis實現增刪改查操作

一、概述

Redis是一個NoSQL數據庫,由於其數據類型的差異,所以要在MVC框架中實現CURD操作,比較繁鎖。事實上在ThinkPHP框架中,只能實現簡單的緩存應用。而不像MongoDB那樣能夠實現常見數據庫的CURD操作。本文章將通過擴展的方式,實現Redis的CURD操作,這樣我們就可以像操作普通的Mysql數據庫那樣實現Redis的編程了。

二、實現過程

接下爲將以ThinkPHP作爲MVC開發框架,詳細介紹Redis的CURD操作。需要說明的是,在ThinkPHP中本身並不支持Redis開發環境,只支持使用Redis開發簡單的數據緩存功能。所以我們必須要通過擴展功能,實現Redis的編程支持。爲了方便讀者學習,筆者臨時開發了相應的模塊擴展及數據庫擴展。

解壓下載後的壓縮包,將得到DbRedis.class.php文件及RedisModel.class.php文件。將DbRedis.class.php文件複製到ThinkPHP/Extend/Driver/Db目錄;將RedisModel.class.php文件複製到ThinkPHP/Extend/Model目錄。然後在項目配置文件中加入Redis數據庫連接信息,如以下代碼所示。

  1. 'REDIS_HOST'=>'192.168.0.2', 
  2. 'REDIS_PORT'=>6379, 
  3. 'REDIS_AUTH'=>123456, 
  4. 'REDIS_DB_PREFIX'=>'', 
[html] view plain copy
  1. 'REDIS_HOST'=>'192.168.0.2',  
  2. 'REDIS_PORT'=>6379,  
  3. 'REDIS_AUTH'=>123456,  
  4. 'REDIS_DB_PREFIX'=>'',  

讀者可根據實際環境填寫即可。通過前面步驟,至此就完成了在ThinkPHP中進行Redis開發的前期準備,接下來將結合示例代碼,詳細演示Redis的CURD操作。

1、增加數據

這裏的增加數據包括Redis五大數據類型的數據添加。由於篇幅所限,這裏不再詳細介紹操作的實現原理,將通過代碼演示操作方式。如以下代碼所示。

  1. <?php 
  2. /** 
  3. * redis添加數據 
  4. * Enter description here ... 
  5. * @author Administrator 
  6. */ 
  7. class AddAction extends Action{ 
  8.     /** 
  9.      * list類型 
  10.      * Enter description here ... 
  11.      */ 
  12.     public function lists(){ 
  13.         $Redis=new RedisModel("list11"); 
  14.         //一次只能推送一條       
  15.         echo $Redis->add("ceiba"); 
  16.     } 
  17.      /** 
  18.      * 字符串類型 
  19.      * Enter description here ... 
  20.      */ 
  21.     public function string(){ 
  22.         $Redis=new RedisModel(); 
  23.         $data=array
  24.             "str1"=>"ceiba", //一個key,對應一個值 
  25.             "str2"=>"李開湧", 
  26.             "str3"=>"李明", 
  27.         ); 
  28.         echo $Redis->type("string")->add($data); 
  29.     } 
  30.     /** 
  31.      * HASH類型 
  32.      * Enter description here ... 
  33.      */ 
  34.     public function hash(){ 
  35.         $Redis=new RedisModel("user:1"); 
  36.              $data=array
  37.                "field1"=>"ceiba", //一個key,對應一個值 
  38.                "field2"=>"李開湧", 
  39.                "field3"=>"李明", 
  40.              ); 
  41.              //支持批量添加 
  42.              echo $Redis->type("hash")->add($data);        
  43.     } 
  44.      /** 
  45.      * 集合類型 
  46.      * Enter description here ... 
  47.      */ 
  48.     public function sets(){ 
  49.              $Redis=new RedisModel("sets:1"); 
  50.         //一次只能推送一條       
  51.         echo $Redis->type("sets")->add("ceiba"); 
  52.     } 
  53.       /** 
  54.      * 有序集合 
  55.      * Enter description here ... 
  56.      */ 
  57.     public function zset(){  
  58.         $Redis=new RedisModel("zset:1"); 
  59.         //支持批量添加 
  60.         $data=array
  61.             //排序=>值 
  62.             "10"=>"ceiba", 
  63.             "11"=>"李開湧", 
  64.             "12"=>"李明" 
  65.         );       
  66.         echo $Redis->type("zset")->add($data); 
  67.     } 
  68. ?> 
[html] view plain copy
  1. <?php  
  2. /**  
  3.  * redis添加數據  
  4.  * Enter description here ...  
  5.  * @author Administrator  
  6.  *  
  7.  */  
  8. class AddAction extends Action{  
  9.     /**  
  10.      * list類型  
  11.      * Enter description here ...  
  12.      */  
  13.     public function lists(){  
  14.         $Redis=new RedisModel("list11");  
  15.         //一次只能推送一條        
  16.         echo $Redis->add("ceiba");  
  17.     }  
  18.      /**  
  19.      * 字符串類型  
  20.      * Enter description here ...  
  21.      */  
  22.     public function string(){  
  23.         $Redis=new RedisModel();  
  24.         $data=array(  
  25.             "str1"=>"ceiba", //一個key,對應一個值  
  26.             "str2"=>"李開湧",  
  27.             "str3"=>"李明",  
  28.         );  
  29.         echo $Redis->type("string")->add($data);  
  30.     }  
  31.     /**  
  32.      * HASH類型  
  33.      * Enter description here ...  
  34.      */  
  35.     public function hash(){  
  36.         $Redis=new RedisModel("user:1");  
  37.              $data=array(  
  38.                "field1"=>"ceiba", //一個key,對應一個值  
  39.                "field2"=>"李開湧",  
  40.                "field3"=>"李明",  
  41.              );  
  42.              //支持批量添加  
  43.              echo $Redis->type("hash")->add($data);         
  44.     }  
  45.      /**  
  46.      * 集合類型  
  47.      * Enter description here ...  
  48.      */  
  49.     public function sets(){  
  50.              $Redis=new RedisModel("sets:1");  
  51.         //一次只能推送一條        
  52.         echo $Redis->type("sets")->add("ceiba");  
  53.     }  
  54.       /**  
  55.      * 有序集合  
  56.      * Enter description here ...  
  57.      */  
  58.     public function zset(){   
  59.         $Redis=new RedisModel("zset:1");  
  60.         //支持批量添加  
  61.         $data=array(  
  62.             //排序=>值  
  63.             "10"=>"ceiba",  
  64.             "11"=>"李開湧",  
  65.             "12"=>"李明"  
  66.         );        
  67.         echo $Redis->type("zset")->add($data);  
  68.     }  
  69. }  
  70. ?>  

2、查詢數據

  1. <?php 
  2. // redis查詢數據 
  3. class IndexAction extends Action { 
  4.     public function page(){ 
  5.         $this->display(); 
  6.     } 
  7.     /** 
  8.      * 列表類型,默認類型 
  9.      * Enter description here ... 
  10.      */ 
  11.     public function lists(){ 
  12.         //dump(C("REDIS_HOST"));  
  13.         $Redis=new RedisModel("list1"); 
  14.         $field=array
  15.             "nmae","age","pro" 
  16.         ); 
  17.         $data=$Redis->field($field)->select(); 
  18.         dump($data); 
  19.         //獲得隊列中的記錄總數 
  20.         $count=$Redis->count(); 
  21.         dump($count); 
  22.     } 
  23.     /** 
  24.      * 字符串類型 
  25.      * Enter description here ... 
  26.      */ 
  27.     public function string(){ 
  28.             $Redis=new RedisModel(); 
  29.             //field 表示每個key名稱 
  30.             $rows=$Redis->type("string")->field(array("str1","str2"))->select(); 
  31.             dump($rows); 
  32.     } 
  33.     /** 
  34.      * HASH類型 
  35.      * Enter description here ... 
  36.      */ 
  37.     public function hash(){ 
  38.             $Redis=new RedisModel("h9"); 
  39.             //默認顯示所有HASH字段,可以通過field連慣操作限制 
  40.             $rows=$Redis->type("hash")->field(array("field1"))->select(); 
  41.             dump($rows); 
  42.             //統計總記錄 
  43.             $count=$Redis->type("hash")->count(); 
  44.             dump($count);        
  45.     } 
  46.     /** 
  47.      * 集合類型 
  48.      * Enter description here ... 
  49.      */ 
  50.     public function sets(){ 
  51.             $Redis=new RedisModel(); 
  52.             $arr=array
  53.             "s3","s4" 
  54.             ); 
  55.        $rows=$Redis->type("sets")->field($arr)->where("sinterstore")->select();//求交集 
  56.           dump($rows); 
  57.           $rows=$Redis->type("sets")->field($arr)->where("sunion")->select();//求並集 
  58.           dump($rows); 
  59.           $rows=$Redis->type("sets")->field($arr)->where("sdiff")->select();//求差集 
  60.           dump($rows); 
  61.           $Redis=new RedisModel("s3"); 
  62.           $rows=$Redis->type("sets")->select(); //返回單個集合列表中的所有成員 
  63.           dump($rows); 
  64.           //統計記錄 
  65.           $Redis=new RedisModel("s3"); 
  66.           $count=$Redis->type("sets")->count();  
  67.           dump($count);      
  68.     } 
  69.     /** 
  70.      * 有序集合 
  71.      * Enter description here ... 
  72.      */ 
  73.     public function zset(){  
  74.         $Redis=new RedisModel("z2");  
  75.         //默認顯示0到20      
  76.         $data=$Redis->type("zset")->limit("0,-1")->select(); 
  77.         dump($data); 
  78.         //使用zRevRange顯示數據,數組第2個參數爲true時顯示排序號 
  79.          $data=$Redis->type("zset")->limit("0,-1")->order(array("zRevRange",true))->select(); 
  80.         dump($data); 
  81.         //不設置limit時,將統計所有記錄 
  82.         $count=$Redis->type("zset")->limit("0,1")->count(); 
  83.         dump($count); 
  84.          
  85.     } 
[html] view plain copy
  1. <?php  
  2. // redis查詢數據  
  3. class IndexAction extends Action {  
  4.     public function page(){  
  5.         $this->display();  
  6.     }  
  7.     /**  
  8.      * 列表類型,默認類型  
  9.      * Enter description here ...  
  10.      */  
  11.     public function lists(){  
  12.         //dump(C("REDIS_HOST"));   
  13.         $Redis=new RedisModel("list1");  
  14.         $field=array(  
  15.             "nmae","age","pro"  
  16.         );  
  17.         $data=$Redis->field($field)->select();  
  18.         dump($data);  
  19.         //獲得隊列中的記錄總數  
  20.         $count=$Redis->count();  
  21.         dump($count);  
  22.     }  
  23.     /**  
  24.      * 字符串類型  
  25.      * Enter description here ...  
  26.      */  
  27.     public function string(){  
  28.             $Redis=new RedisModel();  
  29.             //field 表示每個key名稱  
  30.             $rows=$Redis->type("string")->field(array("str1","str2"))->select();  
  31.             dump($rows);  
  32.     }  
  33.     /**  
  34.      * HASH類型  
  35.      * Enter description here ...  
  36.      */  
  37.     public function hash(){  
  38.             $Redis=new RedisModel("h9");  
  39.             //默認顯示所有HASH字段,可以通過field連慣操作限制  
  40.             $rows=$Redis->type("hash")->field(array("field1"))->select();  
  41.             dump($rows);  
  42.             //統計總記錄  
  43.             $count=$Redis->type("hash")->count();  
  44.             dump($count);         
  45.     }  
  46.     /**  
  47.      * 集合類型  
  48.      * Enter description here ...  
  49.      */  
  50.     public function sets(){  
  51.             $Redis=new RedisModel();  
  52.             $arr=array(  
  53.             "s3","s4"  
  54.             );  
  55.        $rows=$Redis->type("sets")->field($arr)->where("sinterstore")->select();//求交集  
  56.           dump($rows);  
  57.           $rows=$Redis->type("sets")->field($arr)->where("sunion")->select();//求並集  
  58.           dump($rows);  
  59.           $rows=$Redis->type("sets")->field($arr)->where("sdiff")->select();//求差集  
  60.           dump($rows);  
  61.           $Redis=new RedisModel("s3");  
  62.           $rows=$Redis->type("sets")->select(); //返回單個集合列表中的所有成員  
  63.           dump($rows);  
  64.           //統計記錄  
  65.           $Redis=new RedisModel("s3");  
  66.           $count=$Redis->type("sets")->count();   
  67.           dump($count);       
  68.     }  
  69.     /**  
  70.      * 有序集合  
  71.      * Enter description here ...  
  72.      */  
  73.     public function zset(){   
  74.         $Redis=new RedisModel("z2");   
  75.         //默認顯示0到20       
  76.         $data=$Redis->type("zset")->limit("0,-1")->select();  
  77.         dump($data);  
  78.         //使用zRevRange顯示數據,數組第2個參數爲true時顯示排序號  
  79.          $data=$Redis->type("zset")->limit("0,-1")->order(array("zRevRange",true))->select();  
  80.         dump($data);  
  81.         //不設置limit時,將統計所有記錄  
  82.         $count=$Redis->type("zset")->limit("0,1")->count();  
  83.         dump($count);  
  84.           
  85.     }  
  86. }  

3、刪除數據

  1. <?php 
  2. /** 
  3. * Redis刪除數據 
  4. * Enter description here ... 
  5. * @author Administrator 
  6. */ 
  7. class DeleteAction extends Action{ 
  8.     /** 
  9.      * list類型 
  10.      * Enter description here ... 
  11.      */ 
  12.     public function lists(){ 
  13.         $Redis=new RedisModel("mylist"); 
  14.             //根據索引號,刪除指定的list元素          
  15.         echo $Redis->where(3)->delete(); 
  16.         //ltrim區間批量刪除,保留4~5之間的記錄 
  17. echo $Redis->type("list")->where(array("4","5"))->delete("ltrim");  
  18.         //lpop單條順序彈出     
  19. echo $Redis->type("list")->delete("lpop");  
  20.          
  21.     } 
  22.      /** 
  23.      * 字符串類型 
  24.      * Enter description here ... 
  25.      */ 
  26.     public function string(){ 
  27.            $Redis=new RedisModel(); 
  28.            //直接刪除key,這各方式適用於所有數據類型 
  29.            echo $Redis->type("string")->field(array("str1","str2"))->delete(); 
  30.     } 
  31.     /** 
  32.      * HASH類型 
  33.      * Enter description here ... 
  34.      */ 
  35.     public function hash(){ 
  36.         $Redis=new RedisModel("user:1");         
  37.              //刪除指定hash中的指定字段(field),不支持批量刪除 
  38.              echo $Redis->type("hash")->where("field1")->delete();  
  39.      
  40.     } 
  41.      /** 
  42.      * 集合類型 
  43.      * Enter description here ... 
  44.      */ 
  45.     public function sets(){ 
  46.              $Redis=new RedisModel("s1"); 
  47.         //刪除sets:1集合中名爲age的value     
  48.         echo $Redis->type("sets")->where("age")->delete(); 
  49.     } 
  50.     /** 
  51.      * 有序集合 
  52.      * Enter description here ... 
  53.      */ 
  54.     public function zset(){  
  55.         $Redis=new RedisModel("z1"); 
  56.         //根據集合元素value進行刪除 
  57.         echo $Redis->type("zset")->where("two")->delete();  
  58.         //根據排序號進行區間批量刪除,保留2~3之間的記錄 
  59.         echo $Redis->type("zset")->where(array("1","4"))->delete("zremRangeByScore");  
  60.         //根據索引號進行區間批量刪除,保留2~3之間的記錄 
  61.         echo $Redis->type("zset")->where(array("1","3"))->delete("zRemRangeByRank");  
  62.     } 
  63. ?> 
[html] view plain copy
  1. <?php  
  2. /**  
  3.  * Redis刪除數據  
  4.  * Enter description here ...  
  5.  * @author Administrator  
  6.  *  
  7.  */  
  8. class DeleteAction extends Action{  
  9.     /**  
  10.      * list類型  
  11.      * Enter description here ...  
  12.      */  
  13.     public function lists(){  
  14.         $Redis=new RedisModel("mylist");  
  15.             //根據索引號,刪除指定的list元素           
  16.         echo $Redis->where(3)->delete();  
  17.         //ltrim區間批量刪除,保留4~5之間的記錄  
  18. echo $Redis->type("list")->where(array("4","5"))->delete("ltrim");   
  19.         //lpop單條順序彈出      
  20. echo $Redis->type("list")->delete("lpop");   
  21.           
  22.     }  
  23.      /**  
  24.      * 字符串類型  
  25.      * Enter description here ...  
  26.      */  
  27.     public function string(){  
  28.            $Redis=new RedisModel();  
  29.            //直接刪除key,這各方式適用於所有數據類型  
  30.            echo $Redis->type("string")->field(array("str1","str2"))->delete();  
  31.     }  
  32.     /**  
  33.      * HASH類型  
  34.      * Enter description here ...  
  35.      */  
  36.     public function hash(){  
  37.         $Redis=new RedisModel("user:1");          
  38.              //刪除指定hash中的指定字段(field),不支持批量刪除  
  39.              echo $Redis->type("hash")->where("field1")->delete();   
  40.       
  41.     }  
  42.      /**  
  43.      * 集合類型  
  44.      * Enter description here ...  
  45.      */  
  46.     public function sets(){  
  47.              $Redis=new RedisModel("s1");  
  48.         //刪除sets:1集合中名爲age的value      
  49.         echo $Redis->type("sets")->where("age")->delete();  
  50.     }  
  51.     /**  
  52.      * 有序集合  
  53.      * Enter description here ...  
  54.      */  
  55.     public function zset(){   
  56.         $Redis=new RedisModel("z1");  
  57.         //根據集合元素value進行刪除  
  58.         echo $Redis->type("zset")->where("two")->delete();   
  59.         //根據排序號進行區間批量刪除,保留2~3之間的記錄  
  60.         echo $Redis->type("zset")->where(array("1","4"))->delete("zremRangeByScore");   
  61.         //根據索引號進行區間批量刪除,保留2~3之間的記錄  
  62.         echo $Redis->type("zset")->where(array("1","3"))->delete("zRemRangeByRank");   
  63.     }  
  64. }  
  65. ?>  
發佈了49 篇原創文章 · 獲贊 16 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章