- <?php
- /**
- * redis添加數(shù)據(jù)
- * Enter description here ...
- * @author Administrator
- *
- */
- class AddAction extends Action{
- /**
- * list類型
- * Enter description here ...
- */
- public function lists(){
- $Redis=new RedisModel("list11");
- //一次只能推送一條
- echo $Redis->add("ceiba");
- }
- /**
- * 字符串類型
- * Enter description here ...
- */
- public function string(){
- $Redis=new RedisModel();
- $data=array(
- "str1"=>"ceiba", //一個key,對應一個值
- "str2"=>"李開湧",
- "str3"=>"李明",
- );
- echo $Redis->type("string")->add($data);
- }
- /**
- * HASH類型
- * Enter description here ...
- */
- public function hash(){
- $Redis=new RedisModel("user:1");
- $data=array(
- "field1"=>"ceiba", //一個key,對應一個值
- "field2"=>"李開湧",
- "field3"=>"李明",
- );
- //支持批量添加
- echo $Redis->type("hash")->add($data);
- }
- /**
- * 集合類型
- * Enter description here ...
- */
- public function sets(){
- $Redis=new RedisModel("sets:1");
- //一次只能推送一條
- echo $Redis->type("sets")->add("ceiba");
- }
- /**
- * 有序集合
- * Enter description here ...
- */
- public function zset(){
- $Redis=new RedisModel("zset:1");
- //支持批量添加
- $data=array(
- //排序=>值
- "10"=>"ceiba",
- "11"=>"李開湧",
- "12"=>"李明"
- );
- echo $Redis->type("zset")->add($data);
- }
- }
- ?>
- <?php
- /**
- * redis添加數(shù)據(jù)
- * Enter description here ...
- * @author Administrator
- *
- */
- class AddAction extends Action{
- /**
- * list類型
- * Enter description here ...
- */
- public function lists(){
- $Redis=new RedisModel("list11");
- //一次只能推送一條
- echo $Redis->add("ceiba");
- }
- /**
- * 字符串類型
- * Enter description here ...
- */
- public function string(){
- $Redis=new RedisModel();
- $data=array(
- "str1"=>"ceiba", //一個key,對應一個值
- "str2"=>"李開湧",
- "str3"=>"李明",
- );
- echo $Redis->type("string")->add($data);
- }
- /**
- * HASH類型
- * Enter description here ...
- */
- public function hash(){
- $Redis=new RedisModel("user:1");
- $data=array(
- "field1"=>"ceiba", //一個key,對應一個值
- "field2"=>"李開湧",
- "field3"=>"李明",
- );
- //支持批量添加
- echo $Redis->type("hash")->add($data);
- }
- /**
- * 集合類型
- * Enter description here ...
- */
- public function sets(){
- $Redis=new RedisModel("sets:1");
- //一次只能推送一條
- echo $Redis->type("sets")->add("ceiba");
- }
- /**
- * 有序集合
- * Enter description here ...
- */
- public function zset(){
- $Redis=new RedisModel("zset:1");
- //支持批量添加
- $data=array(
- //排序=>值
- "10"=>"ceiba",
- "11"=>"李開湧",
- "12"=>"李明"
- );
- echo $Redis->type("zset")->add($data);
- }
- }
- ?>
2、查詢數(shù)據(jù)
- <?php
- // redis查詢數(shù)據(jù)
- class IndexAction extends Action {
- public function page(){
- $this->display();
- }
- /**
- * 列表類型,默認類型
- * Enter description here ...
- */
- public function lists(){
- //dump(C("REDIS_HOST"));
- $Redis=new RedisModel("list1");
- $field=array(
- "nmae","age","pro"
- );
- $data=$Redis->field($field)->select();
- dump($data);
- //獲得隊列中的記錄總數(shù)
- $count=$Redis->count();
- dump($count);
- }
- /**
- * 字符串類型
- * Enter description here ...
- */
- public function string(){
- $Redis=new RedisModel();
- //field 表示每個key名稱
- $rows=$Redis->type("string")->field(array("str1","str2"))->select();
- dump($rows);
- }
- /**
- * HASH類型
- * Enter description here ...
- */
- public function hash(){
- $Redis=new RedisModel("h9");
- //默認顯示所有HASH字段,可以通過field連慣操作限制
- $rows=$Redis->type("hash")->field(array("field1"))->select();
- dump($rows);
- //統(tǒng)計總記錄
- $count=$Redis->type("hash")->count();
- dump($count);
- }
- /**
- * 集合類型
- * Enter description here ...
- */
- public function sets(){
- $Redis=new RedisModel();
- $arr=array(
- "s3","s4"
- );
- $rows=$Redis->type("sets")->field($arr)->where("sinterstore")->select();//求交集
- dump($rows);
- $rows=$Redis->type("sets")->field($arr)->where("sunion")->select();//求并集
- dump($rows);
- $rows=$Redis->type("sets")->field($arr)->where("sdiff")->select();//求差集
- dump($rows);
- $Redis=new RedisModel("s3");
- $rows=$Redis->type("sets")->select(); //返回單個集合列表中的所有成員
- dump($rows);
- //統(tǒng)計記錄
- $Redis=new RedisModel("s3");
- $count=$Redis->type("sets")->count();
- dump($count);
- }
- /**
- * 有序集合
- * Enter description here ...
- */
- public function zset(){
- $Redis=new RedisModel("z2");
- //默認顯示0到20
- $data=$Redis->type("zset")->limit("0,-1")->select();
- dump($data);
- //使用zRevRange顯示數(shù)據(jù),數(shù)組第2個參數(shù)為true時顯示排序號
- $data=$Redis->type("zset")->limit("0,-1")->order(array("zRevRange",true))->select();
- dump($data);
- //不設置limit時,將統(tǒng)計所有記錄
- $count=$Redis->type("zset")->limit("0,1")->count();
- dump($count);
-
- }
- }
- <?php
- // redis查詢數(shù)據(jù)
- class IndexAction extends Action {
- public function page(){
- $this->display();
- }
- /**
- * 列表類型,默認類型
- * Enter description here ...
- */
- public function lists(){
- //dump(C("REDIS_HOST"));
- $Redis=new RedisModel("list1");
- $field=array(
- "nmae","age","pro"
- );
- $data=$Redis->field($field)->select();
- dump($data);
- //獲得隊列中的記錄總數(shù)
- $count=$Redis->count();
- dump($count);
- }
- /**
- * 字符串類型
- * Enter description here ...
- */
- public function string(){
- $Redis=new RedisModel();
- //field 表示每個key名稱
- $rows=$Redis->type("string")->field(array("str1","str2"))->select();
- dump($rows);
- }
- /**
- * HASH類型
- * Enter description here ...
- */
- public function hash(){
- $Redis=new RedisModel("h9");
- //默認顯示所有HASH字段,可以通過field連慣操作限制
- $rows=$Redis->type("hash")->field(array("field1"))->select();
- dump($rows);
- //統(tǒng)計總記錄
- $count=$Redis->type("hash")->count();
- dump($count);
- }
- /**
- * 集合類型
- * Enter description here ...
- */
- public function sets(){
- $Redis=new RedisModel();
- $arr=array(
- "s3","s4"
- );
- $rows=$Redis->type("sets")->field($arr)->where("sinterstore")->select();//求交集
- dump($rows);
- $rows=$Redis->type("sets")->field($arr)->where("sunion")->select();//求并集
- dump($rows);
- $rows=$Redis->type("sets")->field($arr)->where("sdiff")->select();//求差集
- dump($rows);
- $Redis=new RedisModel("s3");
- $rows=$Redis->type("sets")->select(); //返回單個集合列表中的所有成員
- dump($rows);
- //統(tǒng)計記錄
- $Redis=new RedisModel("s3");
- $count=$Redis->type("sets")->count();
- dump($count);
- }
- /**
- * 有序集合
- * Enter description here ...
- */
- public function zset(){
- $Redis=new RedisModel("z2");
- //默認顯示0到20
- $data=$Redis->type("zset")->limit("0,-1")->select();
- dump($data);
- //使用zRevRange顯示數(shù)據(jù),數(shù)組第2個參數(shù)為true時顯示排序號
- $data=$Redis->type("zset")->limit("0,-1")->order(array("zRevRange",true))->select();
- dump($data);
- //不設置limit時,將統(tǒng)計所有記錄
- $count=$Redis->type("zset")->limit("0,1")->count();
- dump($count);
-
- }
- }
3、刪除數(shù)據(jù)
- <?php
- /**
- * Redis刪除數(shù)據(jù)
- * Enter description here ...
- * @author Administrator
- *
- */
- class DeleteAction extends Action{
- /**
- * list類型
- * Enter description here ...
- */
- public function lists(){
- $Redis=new RedisModel("mylist");
- //根據(jù)索引號,刪除指定的list元素
- echo $Redis->where(3)->delete();
- //ltrim區(qū)間批量刪除,保留4~5之間的記錄
- echo $Redis->type("list")->where(array("4","5"))->delete("ltrim");
- //lpop單條順序彈出
- echo $Redis->type("list")->delete("lpop");
-
- }
- /**
- * 字符串類型
- * Enter description here ...
- */
- public function string(){
- $Redis=new RedisModel();
- //直接刪除key,這各方式適用于所有數(shù)據(jù)類型
- echo $Redis->type("string")->field(array("str1","str2"))->delete();
- }
- /**
- * HASH類型
- * Enter description here ...
- */
- public function hash(){
- $Redis=new RedisModel("user:1");
- //刪除指定hash中的指定字段(field),不支持批量刪除
- echo $Redis->type("hash")->where("field1")->delete();
-
- }
- /**
- * 集合類型
- * Enter description here ...
- */
- public function sets(){
- $Redis=new RedisModel("s1");
- //刪除sets:1集合中名為age的value
- echo $Redis->type("sets")->where("age")->delete();
- }
- /**
- * 有序集合
- * Enter description here ...
- */
- public function zset(){
- $Redis=new RedisModel("z1");
- //根據(jù)集合元素value進行刪除
- echo $Redis->type("zset")->where("two")->delete();
- //根據(jù)排序號進行區(qū)間批量刪除,保留2~3之間的記錄
- echo $Redis->type("zset")->where(array("1","4"))->delete("zremRangeByScore");
- //根據(jù)索引號進行區(qū)間批量刪除,保留2~3之間的記錄
- echo $Redis->type("zset")->where(array("1","3"))->delete("zRemRangeByRank");
- }
- }
- ?>
- <?php
- /**
- * Redis刪除數(shù)據(jù)
- * Enter description here ...
- * @author Administrator
- *
- */
- class DeleteAction extends Action{
- /**
- * list類型
- * Enter description here ...
- */
- public function lists(){
- $Redis=new RedisModel("mylist");
- //根據(jù)索引號,刪除指定的list元素
- echo $Redis->where(3)->delete();
- //ltrim區(qū)間批量刪除,保留4~5之間的記錄
- echo $Redis->type("list")->where(array("4","5"))->delete("ltrim");
- //lpop單條順序彈出
- echo $Redis->type("list")->delete("lpop");
-
- }
- /**
- * 字符串類型
- * Enter description here ...
- */
- public function string(){
- $Redis=new RedisModel();
- //直接刪除key,這各方式適用于所有數(shù)據(jù)類型
- echo $Redis->type("string")->field(array("str1","str2"))->delete();
- }
- /**
- * HASH類型
- * Enter description here ...
- */
- public function hash(){
- $Redis=new RedisModel("user:1");
- //刪除指定hash中的指定字段(field),不支持批量刪除
- echo $Redis->type("hash")->where("field1")->delete();
-
- }
- /**
- * 集合類型
- * Enter description here ...
- */
- public function sets(){
- $Redis=new RedisModel("s1");
- //刪除sets:1集合中名為age的value
- echo $Redis->type("sets")->where("age")->delete();
- }
- /**
- * 有序集合
- * Enter description here ...
- */
- public function zset(){
- $Redis=new RedisModel("z1");
- //根據(jù)集合元素value進行刪除
- echo $Redis->type("zset")->where("two")->delete();
- //根據(jù)排序號進行區(qū)間批量刪除,保留2~3之間的記錄
- echo $Redis->type("zset")->where(array("1","4"))->delete("zremRangeByScore");
- //根據(jù)索引號進行區(qū)間批量刪除,保留2~3之間的記錄
- echo $Redis->type("zset")->where(array("1","3"))->delete("zRemRangeByRank");
- }
- }
- ?>
|