接下为将以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数据库连接信息,如以下代码所示。
Redis 配置 'REDIS_HOST'=>'192.168.0.2', 'REDIS_PORT'=>6379, 'REDIS_AUTH'=>123456, 'REDIS_DB_PREFIX'=>'',
1、增加数据
这里的增加数据包括Redis五大数据类型的数据添加。由于篇幅所限,这里不再详细介绍操作的实现原理,将通过代码演示操作方式。如以下代码所示。
1<?php 2/** 3* redis添加数据 4* Enter description here ... 5* @author Administrator 6* 7*/ 8class 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?> 71 72 73<?php 74/** 75 * redis添加数据 76 * Enter description here ... 77 * @author Administrator 78 * 79 */ 80class AddAction extends Action{ 81 /** 82 * list类型 83 * Enter description here ... 84 */ 85 public function lists(){ 86 $Redis=new RedisModel("list11"); 87 //一次只能推送一条 88 echo $Redis->add("ceiba"); 89 } 90 /** 91 * 字符串类型 92 * Enter description here ... 93 */ 94 public function string(){ 95 $Redis=new RedisModel(); 96 $data=array( 97 "str1"=>"ceiba", //一个key,对应一个值 98 "str2"=>"李开湧", 99 "str3"=>"李明", 100 ); 101 echo $Redis->type("string")->add($data); 102 } 103 /** 104 * HASH类型 105 * Enter description here ... 106 */ 107 public function hash(){ 108 $Redis=new RedisModel("user:1"); 109 $data=array( 110 "field1"=>"ceiba", //一个key,对应一个值 111 "field2"=>"李开湧", 112 "field3"=>"李明", 113 ); 114 //支持批量添加 115 echo $Redis->type("hash")->add($data); 116 } 117 /** 118 * 集合类型 119 * Enter description here ... 120 */ 121 public function sets(){ 122 $Redis=new RedisModel("sets:1"); 123 //一次只能推送一条 124 echo $Redis->type("sets")->add("ceiba"); 125 } 126 /** 127 * 有序集合 128 * Enter description here ... 129 */ 130 public function zset(){ 131 $Redis=new RedisModel("zset:1"); 132 //支持批量添加 133 $data=array( 134 //排序=>值 135 "10"=>"ceiba", 136 "11"=>"李开湧", 137 "12"=>"李明" 138 ); 139 echo $Redis->type("zset")->add($data); 140 } 141} 142?>
2、查询数据
1<?php 2// redis查询数据 3class 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} 87 88 89<?php 90// redis查询数据 91class IndexAction extends Action { 92 public function page(){ 93 $this->display(); 94 } 95 /** 96 * 列表类型,默认类型 97 * Enter description here ... 98 */ 99 public function lists(){ 100 //dump(C("REDIS_HOST")); 101 $Redis=new RedisModel("list1"); 102 $field=array( 103 "nmae","age","pro" 104 ); 105 $data=$Redis->field($field)->select(); 106 dump($data); 107 //获得队列中的记录总数 108 $count=$Redis->count(); 109 dump($count); 110 } 111 /** 112 * 字符串类型 113 * Enter description here ... 114 */ 115 public function string(){ 116 $Redis=new RedisModel(); 117 //field 表示每个key名称 118 $rows=$Redis->type("string")->field(array("str1","str2"))->select(); 119 dump($rows); 120 } 121 /** 122 * HASH类型 123 * Enter description here ... 124 */ 125 public function hash(){ 126 $Redis=new RedisModel("h9"); 127 //默认显示所有HASH字段,可以通过field连惯操作限制 128 $rows=$Redis->type("hash")->field(array("field1"))->select(); 129 dump($rows); 130 //统计总记录 131 $count=$Redis->type("hash")->count(); 132 dump($count); 133 } 134 /** 135 * 集合类型 136 * Enter description here ... 137 */ 138 public function sets(){ 139 $Redis=new RedisModel(); 140 $arr=array( 141 "s3","s4" 142 ); 143 $rows=$Redis->type("sets")->field($arr)->where("sinterstore")->select();//求交集 144 dump($rows); 145 $rows=$Redis->type("sets")->field($arr)->where("sunion")->select();//求并集 146 dump($rows); 147 $rows=$Redis->type("sets")->field($arr)->where("sdiff")->select();//求差集 148 dump($rows); 149 $Redis=new RedisModel("s3"); 150 $rows=$Redis->type("sets")->select(); //返回单个集合列表中的所有成员 151 dump($rows); 152 //统计记录 153 $Redis=new RedisModel("s3"); 154 $count=$Redis->type("sets")->count(); 155 dump($count); 156 } 157 /** 158 * 有序集合 159 * Enter description here ... 160 */ 161 public function zset(){ 162 $Redis=new RedisModel("z2"); 163 //默认显示0到20 164 $data=$Redis->type("zset")->limit("0,-1")->select(); 165 dump($data); 166 //使用zRevRange显示数据,数组第2个参数为true时显示排序号 167 $data=$Redis->type("zset")->limit("0,-1")->order(array("zRevRange",true))->select(); 168 dump($data); 169 //不设置limit时,将统计所有记录 170 $count=$Redis->type("zset")->limit("0,1")->count(); 171 dump($count); 172 173 } 174}
3、删除数据
1<?php 2/** 3* Redis删除数据 4* Enter description here ... 5* @author Administrator 6* 7*/ 8class 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之间的记录 18echo $Redis->type("list")->where(array("4","5"))->delete("ltrim"); 19 //lpop单条顺序弹出 20echo $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?> 66 67 68<?php 69/** 70 * Redis删除数据 71 * Enter description here ... 72 * @author Administrator 73 * 74 */ 75class DeleteAction extends Action{ 76 /** 77 * list类型 78 * Enter description here ... 79 */ 80 public function lists(){ 81 $Redis=new RedisModel("mylist"); 82 //根据索引号,删除指定的list元素 83 echo $Redis->where(3)->delete(); 84 //ltrim区间批量删除,保留4~5之间的记录 85echo $Redis->type("list")->where(array("4","5"))->delete("ltrim"); 86 //lpop单条顺序弹出 87echo $Redis->type("list")->delete("lpop"); 88 89 } 90 /** 91 * 字符串类型 92 * Enter description here ... 93 */ 94 public function string(){ 95 $Redis=new RedisModel(); 96 //直接删除key,这各方式适用于所有数据类型 97 echo $Redis->type("string")->field(array("str1","str2"))->delete(); 98 } 99 /** 100 * HASH类型 101 * Enter description here ... 102 */ 103 public function hash(){ 104 $Redis=new RedisModel("user:1"); 105 //删除指定hash中的指定字段(field),不支持批量删除 106 echo $Redis->type("hash")->where("field1")->delete(); 107 108 } 109 /** 110 * 集合类型 111 * Enter description here ... 112 */ 113 public function sets(){ 114 $Redis=new RedisModel("s1"); 115 //删除sets:1集合中名为age的value 116 echo $Redis->type("sets")->where("age")->delete(); 117 } 118 /** 119 * 有序集合 120 * Enter description here ... 121 */ 122 public function zset(){ 123 $Redis=new RedisModel("z1"); 124 //根据集合元素value进行删除 125 echo $Redis->type("zset")->where("two")->delete(); 126 //根据排序号进行区间批量删除,保留2~3之间的记录 127 echo $Redis->type("zset")->where(array("1","4"))->delete("zremRangeByScore"); 128 //根据索引号进行区间批量删除,保留2~3之间的记录 129 echo $Redis->type("zset")->where(array("1","3"))->delete("zRemRangeByRank"); 130 } 131} 132?>
在Redis中,更新数据与添加数据是可以相互转换的,所以这里不再介绍。更多的功能特性及使用方法,随着时间的推移,笔者会进行更新。本书读者可在配套网站中得到后续支持。