使用ThinkPHP扩展,实现Redis的CURD操作。

一、概述

Redis是一个NoSQL数据库,由于其数据类型的差异,所以要在MVC框架中实现CURD操作,比较繁锁。事实上在ThinkPHP框架中,只能实现简单的缓存应用。而不像MongoDB那样能够实现常见数据库的CURD操作。本文章将通过扩展的方式,实现Redis的CURD操作,这样我们就可以像操作普通的Mysql数据库那样实现Redis的编程了。


二、实现过程

接下为将以ThinkPHP作为MVC开发框架,详细介绍Redis的CURD操作。需要说明的是,在ThinkPHP中本身并不支持Redis开发环境,只支持使用Redis开发简单的数据缓存功能。所以我们必须要通过扩展功能,实现Redis的编程支持。为了方便读者学习,笔者临时开发了相应的模块扩展及数据库扩展。

下载址为  http://beauty-soft.net/book/php_mvc/code/thinkphp_redis.html

解压下载后的压缩包,将得到DbRedis.class.php文件及RedisModel.class.php文件。将DbRedis.class.php文件复制到ThinkPHP/Extend/Driver/Db目录;将RedisModel.class.php文件复制到ThinkPHP/Extend/Model目录。然后在项目配置文件中加入Redis数据库连接信息,如以下代码所示。

[html]   view plain copy
  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五大数据类型的数据添加。由于篇幅所限,这里不再详细介绍操作的实现原理,将通过代码演示操作方式。如以下代码所示。

[html]   view plain copy
  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 在CODE上查看代码片 派生到我的代码片
  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、查询数据

[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.     } 
[html]   view plain copy 在CODE上查看代码片 派生到我的代码片
  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、删除数据

[html]   view plain copy
  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 在CODE上查看代码片 派生到我的代码片
  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. ?>  

在Redis中,更新数据与添加数据是可以相互转换的,所以这里不再介绍。更多的功能特性及使用方法,随着时间的推移,笔者会进行更新。本书读者可在配套网站中得到后续支持。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值