The ThinkPHP framework implements the operation of adding, deleting, modifying and querying through redis

Time:2019-11-5

The example of this paper describes the method of adding, deleting, modifying and querying the ThinkPHP framework through redis. To share with you for your reference, as follows:

I. overview

Redis is a NoSQL database. Due to the difference of data types, curd operation should be implemented in MVC framework, which is more complex. In fact, in the ThinkPHP framework, only simple cache applications can be implemented. Unlike mongodb, it can realize curd operation of common databases. This article will realize the curd operation of redis through the extension way, so that we can realize redis programming like operating the ordinary MySQL database.

II. Implementation process

Next, we will take ThinkPHP as the MVC development framework and introduce the curd operation of redis in detail. It should be noted that in ThinkPHP, it does not support the redis development environment itself, but only supports the use of redis to develop simple data caching functions. Therefore, we must realize redis programming support through extended functions. In order to facilitate the study of readers, the author temporarily developed the corresponding module expansion and database expansion.

After decompressing the downloaded package, you will get the dbredis.class.php file and redismodel.class.php file. 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. Through the previous steps, we have completed the preparation of redis development in ThinkPHP. Next, we will demonstrate the curd operation of redis in detail in combination with the sample code.

1. Add data

The added data here includes the five data types of redis. Due to the limited space, the implementation principle of operation will not be introduced in detail here, and the operation mode will be demonstrated by 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");
    //Only one can be pushed at a time
    echo $Redis->add("ceiba");
  }
   /**
   *String type
   * Enter description here ...
   */
  public function string(){
    $Redis=new RedisModel();
    $data=array(
      "STR1" = > ceiba ", // a key, corresponding to a value
      "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 ", // a key, corresponding to a value
        "Field2" = > Li kaiyong ",
        "Field3" = > Li Ming ",
       );
       //Support batch adding
       echo $Redis->type("hash")->add($data);
  }
   /**
   *Collection type
   * Enter description here ...
   */
  public function sets(){
       $Redis=new RedisModel("sets:1");
    //Only one can be pushed at a time
    echo $Redis->type("sets")->add("ceiba");
  }
   /**
   *Ordered set
   * Enter description here ...
   */
  public function zset(){
    $Redis=new RedisModel("zset:1");
    //Support batch adding
    $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 hash fields are displayed by default, which can be limited by field continuous operation
      $rows=$Redis->type("hash")->field(array("field1"))->select();
      dump($rows);
      //General statistics 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 ("singlestore") - > select(); // intersection
     dump($rows);
     $rows = $redis - > type ("sets") - > field ($ARR) - > where ("Sun ion") - > select(); // Union
     dump($rows);
     $rows = $redis - > type ("sets") - > field ($ARR) - > where ("sdiff") - > select(); // subtraction set
     dump($rows);
     $Redis=new RedisModel("s3");
     $rows = $redis - > type ("sets") - > select(); // returns all members in a single set list
     dump($rows);
     //Statistical records
     $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 to display data, and display the sort number when the second parameter of the array is true
     $data=$Redis->type("zset")->limit("0,-1")->order(array("zRevRange",true))->select();
    dump($data);
    //When limit is not set, all records will be 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");
      //Delete the specified list element according to the index number
    echo $Redis->where(3)->delete();
    //Delete the ltrim interval in batches, and keep the records between 4 and 5
echo $Redis->type("list")->where(array("4","5"))->delete("ltrim");
    //Lpop single order pop-up
echo $Redis->type("list")->delete("lpop");
  }
   /**
   *String type
   * Enter description here ...
   */
  public function string(){
      $Redis=new RedisModel();
      //Delete the key directly, which is applicable 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 field in the specified hash. Batch deletion is not supported
       echo $Redis->type("hash")->where("field1")->delete();
  }
   /**
   *Collection type
   * Enter description here ...
   */
  public function sets(){
       $Redis=new RedisModel("s1");
    //Delete the value named age in the sets: 1 set
    echo $Redis->type("sets")->where("age")->delete();
  }
  /**
   *Ordered set
   * Enter description here ...
   */
  public function zset(){
    $Redis=new RedisModel("z1");
    //Delete according to set element value
    echo $Redis->type("zset")->where("two")->delete();
    //Batch delete the interval according to the sorting number, and keep the records between 2 and 3
    echo $Redis->type("zset")->where(array("1","4"))->delete("zremRangeByScore");
    //Delete the interval in batches according to the index number, and keep the records between 2 and 3
    echo $Redis->type("zset")->where(array("1","3"))->delete("zRemRangeByRank");
  }
}
?>

For more information about ThinkPHP, readers who are interested in it can see the following topics: introduction to ThinkPHP, summary of operation skills of ThinkPHP template, summary of common methods of ThinkPHP, introduction to CodeIgniter, advanced tutorial of CI (CodeIgniter) framework, introduction to Zend framework and technical summary of PHP template.

I hope that this article will be helpful for you to design your PHP program based on the think PHP framework.