redis Common syntax in php

  • 2020-06-12 11:09:17
  • OfStack

Redis is a service of the structure type C/S, C refers to the client, S refers to the server, and the client and the server can communicate through the network. For redis, the redis service needs to be installed on the server. What about the client? In fact, redis provides API for many languages, which can communicate between client and server through language. For php language, we can realize the communication between client and server by installing redis extension.

String type operation

string is the most basic type of redis, and the string type is base 2 safe. The string of redis can contain any data. Such as jpg images or serialized objects


$redis->set('key','TK');
$redis->set('number','1');
$redis->setex('key',5,'TK'); // Set the expiry date to 5 The second key values 
$redis->psetex('key',5000,'TK'); // Set the expiry date to 5000 ms ( with 5 seconds ) The key value 
$redis->setnx('key','XK'); // Returns if the key value exists false  No return true
$redis->delete('key');  The delete key value   You can pass in an array  array('key1','key2') Delete multiple keys 
$redis->getSet('key','XK'); // The key key Is set to XK .   And returns the original value of the key TK
 $ret = $redis->multi() // Batch transaction processing , The atomicity of processing data is not guaranteed 
  ->set('key1', 'val1')
  ->get('key1')
  ->setnx('key', 'val2')
  ->get('key2')
  ->exec();
$redis->watch('key'); //  Monitoring key key  Whether modified by another client 
        if KEY In the call watch() and exec() Is modified between, exec failure 
$redis->publish('chan-1', 'hello, world!'); // send message. 
$redis->exists('key'); // Verify the key exists and return it true
$redis->incr('number'); // The key value added 1
$redis->incrby('number',-10); // The key value add and subtract 10
$redis->incrByFloat('number', +/- 1.5); // Key values plus or minus decimals 
$redis->decr('number'); //  The key value minus 1
$redis->decrBy('number',10); //  The key value minus 10
$mget = $redis->mget(array('number','key')); //  Get key values in batch , return 1 An array 
$redis->mset(array('key0' => 'value0', 'key1' => 'value1')); //  Batch set key values 
$redis->msetnx(array('key0' => 'value0', 'key1' => 'value1')); 
          //  Batch set key value, similar to will setnx() Method Batch operation 
$redis->append('key', '-Smudge'); // The original key TK , appends the value to the key value, which is TK-Smudge
$redis->getRange('key', 0, 5); //  Key value interception from 0 Position start to 5 End of the position 
$redis->getRange('key', -6, -1); //  String interception -6( From the first 6 location ) Start to -1( From the first 1 location ) The end of the 
$redis->setRange('key', 0, 'Smudge'); 
         //  Replace a string with a key value, 0 Says from the 0 Position to start 
           How many characters are replaced by how many positions, which Chinese characters occupy 2 A position 
$redis->strlen('key'); // The key value length 
$redis->getBit('key');
$redis->setBit('key');

list linked list operation


$redis->delete('list-key'); //  To delete a linked list 
$redis->lPush('list-key', 'A'); // Insert the list header / On the left, return the list length 
$redis->rPush('list-key', 'B'); // Insert the end of the list / On the right, return the length of the list 
$redis->lPushx('list-key', 'C'); 
     //  Insert the list header / On the left side of the , The linked list does not return 0 , insert success exists, return the current list length 
$redis->rPushx('list-key', 'C'); 
     //  Insert the end of the list / On the right side , The linked list does not return 0 , insert success exists, return the current list length 
$redis->lPop('list-key'); // return LIST Top (left) VALUE , After the first out ( The stack )
$redis->rPop('list-key'); // return LIST Tail (right) VALUE , First in, first out (queue) 
$redis->blPop();
$redis->brPop();
$redis->lSize('list-key'); 
     //  If it is a linked list, the length of the list is returned 0 
       Returns if it is not a linked list or is not empty false , Determine non-linked lists  " === false "       
$redis->lGet('list-key',-1); //  Gets linked list elements by index  0 Access to the left 1 a  -1 To obtain the final 1 a 
$redis->lSet('list-key', 0, 'X'); //0 Replace the position element with  X
$redis->lRange('list-key', 0, 3); 
     // List the interception   from 0 start  3 End of the position   , the end position is -1  Gets everything after the start location 
$redis->lTrim('list-key', 0, 1); //  Intercept list ( The irreversible )  from 0 The index began  1 The index ended  
$redis->lRem('list-key', 'C', 2); // The linked list deletes elements from the left 2 a C
$redis->lInsert('list-key', Redis::BEFORE, 'C', 'X'); 
     //  in C Insert in front of the element X , Redis::AfTER( That means insert after ) 
       The insert fails if the list does not exist   return 0  Returns if the element does not exist -1
$redis->rpoplpush('list-key', 'list-key2'); 
     // From the source LIST The last pop of 1 An element 
       And take this element from the target LIST The top (left) of the press into the target LIST .  
$redis->brpoplpush();
     //rpoplpush Blocking version, which has a row 3 Three parameters are used to set the blocking time 
       Namely, if the source LIST Is null, then the listener can be blocked timeout If there is an element, the operation is performed. 

Set collection type


set Unordered collection   Duplicate elements are not allowed   The server can implement more than one   Set operations 
$redis->sMembers('key'); // Access to the container key All elements in 
$redis->sAdd('key' , 'TK');
     // ( Insert from the left , The last element to be inserted is at 0 location ), It already exists in the set TK  It returns false 
       There is no add success   return true
$redis->sRem('key' , 'TK'); //  Remove from the container TK
$redis->sMove('key','key1','TK'); // Will be easy to key The elements in the TK  Move to container key1  Operation successful return TRUE
$redis->sIsMember('key','TK'); // check VALUE Whether it is SET Member in the container 
$redis->sCard('key'); // return SET Number of container members 
$redis->sPop('key'); // Random returns to the container 1 Element, and remove the element 
$redis->sRandMember('key');// Random returns to the container 1 Element, do not remove the element 
$redis->sInter('key','key1'); 
  //  Returns the intersection of two sets   No intersection return 1 An empty array if the argument has only one 1 , returns the complete array corresponding to the collection 
$redis->sInterStore('store','key','key1'); // The collection key And collection key1 The intersection of   Deposited in the container store  Successfully returns 1
$redis->sUnion('key','key1'); // A collection of key And collection key1 And set   Note that even multiple collections have the same element   Only keep 1 a 

$redis->sUnionStore('store','key','key1'); 
   // A collection of key And collection key1 The union of is stored in the collection store In the ,  Note that even multiple collections have the same element   Only keep 1 a 
$redis->sDiff('key','key1','key2'); // Returns an array whose elements are present in key Set but does not exist in the set key1 key2

Zset data type

**(stored set) and set 1 are collections of strings, except that each element is associated with an score of type double
The list type of redis is essentially a bi-directional linked list of type string for every child element. * *


$redis->zAdd('tkey', 1, 'A'); 
       //  Insert a collection tkey , A Elements associated 1 Score, insert successful return 1
         Also, set elements cannot be repeated ,  Returns if the element already exists  0
$redis->zRange('tkey',0,-1); //  Gets the collection element from 0 location   to  -1  location 
$redis->zRange('tkey',0,-1, true); 
     //  Gets the collection element from 0 location   to  -1  location ,  return 1 Associative array   Mixed number  
      array([A] => 0.01,[B] => 0.02,[D] => 0.03)  Where the decimal comes from zAdd Methods the first 2 A parameter 
$redis->zDelete('tkey', 'B'); //  Remove the collection tkey Elements in the B  Successfully returns 1  Failure to return  0
$redis->zRevRange('tkey', 0, -1); //  Gets the collection element from 0 location   to  -1  Position of the array according to score Descending order processing 

$redis->zRevRange('tkey', 0, -1,true); 
    //  Gets the collection element from 0 location   to  -1  Position of the array according to score Descending order processing   return score An associative array 
$redis->zRangeByScore('tkey', 0, 0.2,array('withscores' => true)); 
   // To get a few tkey In the score In the interval [0,0.2] The element  ,score Sort from low to high ,
     The elements have the same score , it will be in lexicographic order  , withscores  The control returns an associative array 
$redis->zRangeByScore('tkey', 0.1, 0.36, array('withscores' => TRUE, 'limit' => array(0, 1)));
    // Among them limit In the  0 and 1  I'm going to take the set of conditions   from 0 Start the position and scan backwards 1 a   Return associative array 
$redis->zCount('tkey', 2, 10); //  To obtain tkey In the score In the interval [2, 10] Number of elements 
$redis->zRemRangeByScore('tkey', 1, 3); //  remove tkey In the score In the interval [1, 3]( Including boundary ) The elements of the 
$redis->zRemRangeByRank('tkey', 0, 1); 
       // The default element score It's increasing. Remove tkey Elements in the   from 0 Start to -1 End of the position 
$redis->zSize('tkey'); // Return is stored in key The number of elements in the corresponding ordered set 
$redis->zScore('tkey', 'A'); //  Returns the collection tkey Elements in the A the score value 
$redis->zRank('tkey', 'A'); 
      //  Returns the collection tkey Elements in the A The index of the value  
       z The elements in the set follow score Rank them from low to high   That's the lowest score index The index for 0
$redis->zIncrBy('tkey', 2.5, 'A'); //  The collection tkey Elements in the A the score value   add  2.5
$redis->zUnion('union', array('tkey', 'tkey1')); 
  //  The collection tkey And collection tkey1 Elements are merged into a collection union ,  And the elements in the new set cannot be repeated 
    Returns the number of elements in the new collection,   If the element A in tkey and tkey1 If both exist, the merged element A the score add 
$redis->zUnion('ko2', array('k1', 'k2'), array(5, 2)); 
  //  A collection of k1 And collection k2 And set in the k02  . array(5,1) The number of elements in theta corresponds to the subset, and then  5  The corresponding k1 
   k1 Each element score Should be multiplied by the 5  By the same token, 1 The corresponding k2 . k2 Each element score Multiplied by the 1 
    The elements are then sorted incrementally, default to the same elements score(SUM) add 
$redis->zUnion('ko2', array('k1', 'k2'), array(10, 2),'MAX'); 
  //  After each subset is multiplied by a factor, the elements are sorted by increment, of the same elements score Taking the maximum (MAX)
    You can also set it MIN  Taking the minimum 
$redis->zInter('ko1', array('k1', 'k2')); 
  //  A collection of k1 And collection k2 Take the intersection in the k01  And in accordance with the score Value increasing sort 
    If the set elements are the same, then the elements of the new set score Value added 
$redis->zInter('ko1', array('k1', 'k2'), array(5, 1)); 
  // A collection of k1 And collection k2 Take the intersection in the k01  . array(5,1) The number of elements in theta corresponds to the subset, and then  5  The corresponding k1 
   k1 Each element score Should be multiplied by the 5  By the same token, 1 The corresponding k2 . k2 Each element score Multiplied by the 1 
    , and then the element score Sorted by increment, the default is the same element score(SUM) add 
$redis->zInter('ko1', array('k1', 'k2'), array(5, 1),'MAX'); 
  //  After each subset is multiplied by the factor, the element score Sort by increment, same elements score Taking the maximum (MAX)
    You can also set it MIN  Taking the minimum 

Hash data type

redis hash is the mapping table for 1 string type field and value. It is added and deleted by O(1) (average).hash is especially suitable for storing objects.


$redis->hSet('h', 'name', 'TK'); //  in h In the table   add name field  value for TK
$redis->hSetNx('h', 'name', 'TK');
   //  in h In the table   add name field  value for TK  If the field name the value There is returned false  Otherwise returns  true
$redis->hGet('h', 'name'); //  To obtain h In the table name field value
$redis->hLen('h'); //  To obtain h Table length is the number of fields 
$redis->hDel('h','email'); //  delete h In the table email  field 
$redis->hKeys('h'); //  To obtain h All the fields in the table 
$redis->hVals('h'); //  To obtain h All the fields in the table value
$redis->hGetAll('h'); //  To obtain h All the fields in the table and value  return 1 Associative array ( The fields are key values )
$redis->hExists('h', 'email'); // judge email  Whether the field exists with the table h  No return false
$redis->hSet('h', 'age', 28);
$redis->hIncrBy('h', 'age', -2); 
 //  Set up the h In the table age field value add (-2)  if value It's a nonnumerical value   It returns false  Otherwise, return after the operation value
$redis->hIncrByFloat('h', 'age', -0.33); 
  //  Set up the h In the table age field value add (-2.6)  if value It's a nonnumerical value   It returns false  Otherwise, 
    Returns after the operation value( Decimal reservation 15 position )
$redis->hMset('h', array('score' => '80', 'salary' => 2000)); //  table h  Batch set fields and value
$redis->hMGet('h', array('score','salary')); //  table h  Get the field in batch value

conclusion


Related articles: