分享

redis thinkphp

 丶平上 2016-12-08
  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. ?> 
  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.     } 
  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. ?> 
  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. ?> 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多