Detailed explanation of the method of adding deleting modifying and checking through Redis in thinkPHP framework

  • 2021-12-11 07:11:54
  • OfStack

In this paper, an example is given to describe the method of adding, deleting, modifying and checking the thinkPHP framework through Redis. Share it for your reference, as follows:

1. Overview

Redis is an NoSQL database. Because of the difference of its data types, it is complicated to implement CURD operation in MVC framework. In fact, in the ThinkPHP framework, only simple caching applications can be implemented. Unlike MongoDB, it can implement CURD operation of common databases. This article will expand the way to achieve Redis CURD operation, so that we can operate the common Mysql database as the implementation of Redis programming.

2. Implementation process

Next, we will use ThinkPHP as the development framework of MVC, and introduce CURD operation of Redis in detail. It should be noted that ThinkPHP itself does not support Redis development environment, but only supports Redis to develop simple data caching function. Therefore, we must extend the functions to realize the programming support of Redis. In order to facilitate readers' learning, the author temporarily developed the corresponding module extension and database extension.

Unzip the downloaded compressed package, and you will get DbRedis. class. php files and RedisModel. class. php files. Copy the DbRedis. class. php file to the ThinkPHP/Extend/Driver/Db directory; Copy the RedisModel. class. php file to the ThinkPHP/Extend/Model directory. Then add the Redis database connection information to the project configuration file, as shown in the following code.


'REDIS_HOST'=>'192.168.0.2',
'REDIS_PORT'=>6379,
'REDIS_AUTH'=>123456,
'REDIS_DB_PREFIX'=>'',

Readers can fill in according to the actual environment. With the previous steps, we have completed the preliminary preparation for Redis development in ThinkPHP. Next, we will demonstrate the CURD operation of Redis in detail with sample code.

1. Add data

The added data here includes the data addition of Redis5 big data type. Due to the limited space, the implementation principle of the operation will not be introduced in detail here, and the operation mode will be demonstrated through code. As shown in the following code.


<?php
/**
* redis Add data 
* Enter description here ...
* @author Administrator
*
*/
class AddAction extends Action{
  /**
   * list Type 
   * Enter description here ...
   */
  public function lists(){
    $Redis=new RedisModel("list11");
    //1 You can only push this time 1 Article 
    echo $Redis->add("ceiba");
  }
   /**
   *  String type 
   * Enter description here ...
   */
  public function string(){
    $Redis=new RedisModel();
    $data=array(
      "str1"=>"ceiba", //1 A key , corresponding 1 Values 
      "str2"=>" Li Kaiyong ",
      "str3"=>" Li Ming ",
    );
    echo $Redis->type("string")->add($data);
  }
  /**
   * HASH Type 
   * Enter description here ...
   */
  public function hash(){
    $Redis=new RedisModel("user:1");
       $data=array(
        "field1"=>"ceiba", //1 A key , corresponding 1 Values 
        "field2"=>" Li Kaiyong ",
        "field3"=>" Li Ming ",
       );
       // Support batch addition 
       echo $Redis->type("hash")->add($data);
  }
   /**
   *  Collection type 
   * Enter description here ...
   */
  public function sets(){
       $Redis=new RedisModel("sets:1");
    //1 You can only push this time 1 Article 
    echo $Redis->type("sets")->add("ceiba");
  }
   /**
   *  Ordered set 
   * Enter description here ...
   */
  public function zset(){
    $Redis=new RedisModel("zset:1");
    // Support batch addition 
    $data=array(
      // Sort => Value 
      "10"=>"ceiba",
      "11"=>" Li Kaiyong ",
      "12"=>" Li Ming "
    );
    echo $Redis->type("zset")->add($data);
  }
}
?>

2. Query data


<?php
// redis Query data 
class IndexAction extends Action {
  public function page(){
    $this->display();
  }
  /**
   *  List type, default type 
   * 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);
    // Get the total number of records in the queue 
    $count=$Redis->count();
    dump($count);
  }
  /**
   *  String type 
   * Enter description here ...
   */
  public function string(){
      $Redis=new RedisModel();
      //field  Represents each key Name 
      $rows=$Redis->type("string")->field(array("str1","str2"))->select();
      dump($rows);
  }
  /**
   * HASH Type 
   * Enter description here ...
   */
  public function hash(){
      $Redis=new RedisModel("h9");
      // All are displayed by default HASH Field, you can use the field Inertial operation limit 
      $rows=$Redis->type("hash")->field(array("field1"))->select();
      dump($rows);
      // Statistical total record 
      $count=$Redis->type("hash")->count();
      dump($count);
  }
  /**
   *  Collection type 
   * Enter description here ...
   */
  public function sets(){
      $Redis=new RedisModel();
      $arr=array(
      "s3","s4"
      );
    $rows=$Redis->type("sets")->field($arr)->where("sinterstore")->select();// Find intersection 
     dump($rows);
     $rows=$Redis->type("sets")->field($arr)->where("sunion")->select();// Find union 
     dump($rows);
     $rows=$Redis->type("sets")->field($arr)->where("sdiff")->select();// Difference set 
     dump($rows);
     $Redis=new RedisModel("s3");
     $rows=$Redis->type("sets")->select(); // Returns all members of a single collection list 
     dump($rows);
     // Statistical record 
     $Redis=new RedisModel("s3");
     $count=$Redis->type("sets")->count();
     dump($count);
  }
  /**
   *  Ordered set 
   * Enter description here ...
   */
  public function zset(){
    $Redis=new RedisModel("z2");
    // Default display 0 To 20
    $data=$Redis->type("zset")->limit("0,-1")->select();
    dump($data);
    // Use zRevRange Display data, array number 2 Parameters are true Displays the sort number when 
     $data=$Redis->type("zset")->limit("0,-1")->order(array("zRevRange",true))->select();
    dump($data);
    // Do not set limit All records are counted 
    $count=$Redis->type("zset")->limit("0,1")->count();
    dump($count);
  }
}

3. Delete data


<?php
/**
* Redis Delete data 
* Enter description here ...
* @author Administrator
*
*/
class DeleteAction extends Action{
  /**
   * list Type 
   * Enter description here ...
   */
  public function lists(){
    $Redis=new RedisModel("mylist");
      // Depending on the index number, deletes the specified list Element 
    echo $Redis->where(3)->delete();
    //ltrim Batch deletion and retention of intervals 4~5 Records between 
echo $Redis->type("list")->where(array("4","5"))->delete("ltrim");
    //lpop Single sequential pop-up 
echo $Redis->type("list")->delete("lpop");
  }
   /**
   *  String type 
   * Enter description here ...
   */
  public function string(){
      $Redis=new RedisModel();
      // Direct deletion key Which applies to all data types 
      echo $Redis->type("string")->field(array("str1","str2"))->delete();
  }
  /**
   * HASH Type 
   * Enter description here ...
   */
  public function hash(){
    $Redis=new RedisModel("user:1");
       // Delete the specified hash The specified field in the (field), Bulk deletion is not supported 
       echo $Redis->type("hash")->where("field1")->delete();
  }
   /**
   *  Collection type 
   * Enter description here ...
   */
  public function sets(){
       $Redis=new RedisModel("s1");
    // Delete sets:1 Collection named age Adj. value
    echo $Redis->type("sets")->where("age")->delete();
  }
  /**
   *  Ordered set 
   * Enter description here ...
   */
  public function zset(){
    $Redis=new RedisModel("z1");
    // Depending on the collection element value Delete 
    echo $Redis->type("zset")->where("two")->delete();
    // Delete interval batch according to sorting number and keep it 2~3 Records between 
    echo $Redis->type("zset")->where(array("1","4"))->delete("zremRangeByScore");
    // Delete interval batch according to index number and keep it 2~3 Records between 
    echo $Redis->type("zset")->where(array("1","3"))->delete("zRemRangeByRank");
  }
}
?>

For more readers interested in thinkPHP related contents, please check the topics of this site: "Introduction to ThinkPHP", "Summary of thinkPHP Template Operation Skills", "Summary of ThinkPHP Common Methods", "Introduction to codeigniter", "Advanced Course of CI (CodeIgniter) Framework", "Introduction to Zend FrameWork Framework" and "Summary of PHP Template Technology".

Hope that this article is based on ThinkPHP framework of PHP programming help.


Related articles: