Redis examples to explain

Brief introduction

  Redis is a key-value of nosql products, and we know Memcached is somewhat similar, but he stored value type is relatively more abundant, including string (string), list (list), set (collection), zset (sorted set ordered collection) and hash. Like with memcached, in order to ensure high efficiency, the data is cached in memory. Redis difference are numerous types, also called nosql structured database, the following explanations for each type of data and the use of role instances with PHP.

connection

// Create a connection redis 
$ redis = new new the Redis ();
 // instantiate, are generally connected to the address 127.0.0.1, the default port 6379 
$ redis -> Connect ( '127.0.0.1', '6379' );
 / / authentication password, if the password is not set redis negligible this 
$ redis -> the auth ( '123456');

type of data

  string string

Redis $ -> SET ( 'Test', 'Hello Redis'); // store or change the string type

  list list

The method of operation list l representatives

  storage

Redis $ -> LPUSH ( 'List', 'Xiaohua'); // adding from left 

$ Redis -> RPUSH ( 'List', 'Bob'); // added from the right 

$ Redis -> LSet ( 'List' 2, 'red Sa Long day'); // set or change the list of values specified position, returns a success, failure error message is returned

  delete

Redis $ -> lPop ( 'List'); // delete the first left 

$ Redis -> RPOP ( 'List'); // delete the right of the first 

/ * 
 * remove elements based on the value, the second parameter is to value for deletion (this is the value of the element to be deleted) 
 * delete third parameter indicates where to start, several deleted 
 * parameter> 0 from the head to the tail of the table starts to delete, remove the first of several end 
 * parameters <0 from footer to header start delete, delete a few. 
 * The parameter value = 0 Xiaoming remove all elements 
* / 
$ Redis -> lRem ( 'List', 'Bob', 1);

  Obtain

/ * 
 * The first parameter is a list of names 
 * The second parameter is the index start position 
 * The third parameter is the position of the end of the index (bits comprising end elements) 
 * representative of the reciprocal negative, -1 indicates the inverse of a first a 
 * is larger than the routine returns to the start if empty, is greater than the actual length of the end of the last element directly back 
 * * / 
// returns an array. Stored in the specified range of elements in the list of key 
$ Redis -> lrange ( 'List', 0, -1 ); 

// get the value list of the designated section, supra 
$ Redis -> lGetRange ( 'List', 0,2 ); 

// intercept and retain the values in the list of the designated section, delete the remaining values. Successful return 1, else return an error message. A negative number reciprocal 
$ Redis -> LTRIM ( 'List', 0,2 ); 

// get the list of length 
$ Redis -> lSize Number of ( 'List' ); 

// get the list of the specified position value 
$ Redis -> LGET ( ' List ',. 1 );

->lIndex('list',1);

     hash dictionary

hash of a string field type and value of the mapping table, particularly suited for storing objects. Each hash key may be stored power of 32 -1 to 2 (40 million) corresponds to the hash table redis storage key => value of the key, table of contents corresponds to the hash value h represents the operation method

  storage

/ * 
 Value * key corresponding to the hash table is incremented by one (integer)   
 * The first parameter dictionary name 
 * The second parameter is the name key, 
 * the third parameter is the amplitude increment. If the key does not exist in the table, it is automatically added to the key, and set the value of increase from 
 *
 









* / 
$ Redis -> hIncrBy ( 'hashtest', '. 1',. 1 ); 

// to the hash table key specified increment value 
$ Redis -> hIncrByFloat ( 'hashtest', '2', for 1.5);

  Obtain

// Get a specified value in hashtest 
$ Redis -> hGet ( 'hashtest', 'a' ); 

// get all keys (keys) in the hash table returns an array 
$ Redis -> hKeys ( 'hashtest ' ); 

// get all the hash values in the table, in random order, return an array 
$ Redis -> hVals (' hashtest ' ); 

// get all of the keys and values in the hash table, in a random order, return an array 
$ Redis -> hGetAll ( 'hashtest' ); 

// Get the number of the hash table key 
$ Redis -> hLen ( 'hashtest' ); 

// get bulk value corresponding to the plurality of key; the second parameter is an array, returning an array 
Redis $ -> hMGet ( 'hashtest', [l, 2,3 ]); 

// determines whether there is the hash table key, a return is present, return empty absence 
$ Redis -> hExists ( 'hashtest', '2020');

  delete

// the hash table is a key, successful returns true, or if the key does not exist table returns true 
$ Redis -> HDEL ( 'hashtest', '. 1');

  set collection

Redis string type of set is an unordered collection. It is the only member of the collection, which means that the collection of duplicate data can not appear. A set of the largest member of the 32 th -12 (4,294,967,295, each set can store 40 million members). The method of operation set s representatives

  storage

/ * 
 * Was added to set a set value, adding the number of successful return, else return 0. 
 * The first parameter is the name of a collection set 
 * The second parameter is set to the new value is inserted, namely: to insert a new set of value 
 * / 

// returns. 1 
$ Redis -> Sadd ( 'settest1', 'AA' );
 // returns. 1 
$ Redis -> Sadd ( 'settest1', 'BB', 'CC' );
 // returns 0 
$ redis -> Sadd ( 'settest1', 'AA' );
 $ redis -> Sadd ( 'settest2', 'CC' );
 // add more values above 4.0 redis extension is supported 
$ redis -> sAddArray ( 'settest ', [' cc ',' dd ',' ee ']);

  Obtain

// Get all sets all elements 
$ Redis -> sMembers ( 'settest1' ); 

// determines whether the element is a member of set 1 is returned, instead of returning empty 
$ Redis -> sIsMember ( 'settest1', 'BB' ); 

// Check the number of elements in a collection 
$ Redis -> SCARD ( 'settest1' ); 

// returns the intersection of two sets 
$ Redis -> Sinter ( 'settest1', 'settest2' ); 

// the intersection settest1 and settest2 into the collection settest3 
$ Redis -> sInterStore ( 'settest3', 'settest1', 'settest2' ); 

// returns the union of two sets 
$ Redis -> SUNION ( 'settest1', 'settest2' );

// The union settest1 and placed in settest2 set settest4 
$ Redis -> sUnionStore ( 'settest4', 'settest1', 'settest2'); 

// returns the set difference of two sets of 
$ Redis -> sdiff ( 'settest1', 'settest2' ); 

// the set difference settest2 settest1 and into the collection settset5 
$ Redis -> sDiffStore ( 'settest5', 'settest1', 'settest2') ;

  delete

// a value set to delete 
$ Redis -> SREM ( 'settest1', 'AA' );
 // delete multiple values 
$ Redis -> SREM ( 'settest1', 'AA', 'BB' );
 / / removal of a random element in the collection and returns the element 
$ Redis -> SPOP ( 'settest1');

  sorted set ordered set

 Redis ordered set and the same collection is also a set of string type elements, and does not allow duplicate members. The difference is that each element will be associated with a double score. It redis to members of small to large sort ordered set by a score of members of the collection is unique, but the score (score) it can be repeated set the maximum number of members to 32 th power of 1 - (4,294,967,295 , each set can store more than 40 million members). z is an ordered collection operation method.

  storage

/ * 
 * Ztest added to a value in the ordered set, the score value may be an integer or a double precision floating point value 
 * Zadd when executed, if not, create a new ordered set; ordered but not if there is another when collection type, an error is returned 
 * / 
$ Redis -> Zadd ( 'ZTEST',. 1, '2019' );
 $ Redis -> Zadd ( 'ZTEST', 2, '2019' ); 

/ * 
 * if an element is present when updating the score of this element, and reinsert the element to ensure that the elements in the correct position. 
 * Not new but added $ redis-> zadd ( 'ztest' , a score of 1, the value 1, score 2, the value 2); 
 * to insert an ordered collection of a plurality of values 
 * / 
$ Redis -> Zadd ( 'ZTEST' 2, '2020', 3, '2021', 4, '2022' ); 

// specified value increased 2033 2 
$ Redis -> Zadd ( 'SET', 2, '2022');

  Obtain

/ * 
 * Get the specified range of the ordered set, it returns an array, from small to large fraction 
 * The first parameter: Name ordered set 
 * The second parameter: start position 
 * third parameter: end position (including the position), on behalf of the penultimate several negative 
 * The fourth parameter: optional, Boolean value, with or without scores, default false 
 * / 
// Sort by scores, but not mixed number 
$ Redis -> Z Range The ( 'ZTEST' , 0,1 ); 

// by sorting scores, scores and carries 
$ Redis -> Z range the ( 'ZTEST', 0,1, to true ); 

// Get the specified range of the ordered set. Returns an array. Score descending 
$ Redis -> zRevRange ( 'ZTEST', 1,2 ); 

// Get the specified element scores 
$ Redis -> zScore ( 'ZTEST', '2019' ); 

// Get the number of elements stored 
Redis $ -> zCard ( 'ZTEST' );


-> zCount ( 'ZTEST', 2,5 ); 

// Returns fraction between 2-3 elements, not mixed fraction, displayed with Z Range The 
$ Redis -> zRangeByScore ( 'ZTEST', 2,3 ); 

// Back fraction between elements 2-3 and mixed number display method is the same Z range the 
$ Redis -> zRangeByScore ( 'ZTEST', 2,3, [ 'withscore' => to true ]);

  delete

// delete the specified members 
$ Redis -> zRem ( 'ZTEST', '2019' );
 // remove the fraction between the elements 2-3, the number of deleted return 
$ Redis -> zRemRangeByScore ( 'ZTEST', 2 , 3);

(Expansion) to other commonly used methods

  Find related key

/ *  
 * Condition detected by the corresponding key (key), support string concatenation (the return value is an array, even if did not identify the data also returns an empty array) 
 * * on behalf of any length, any character? A length of any character 
 * * / 
// find the key is equal to A 
$ Redis -> Keys ( 'A' ); 

// find the key, beginning with a, an arbitrary value of the back 
$ Redis -> Keys ( 'A * ' ); 

// find the intermediate link comprises a key b 
$ Redis -> keys (' * b * ' ); 

// find the length of 3, and the first character of the key c a = a $; 
$ Redis -> keys ( 'C ??' ); 

// after use keys may be used for loop plus get () to get the keys corresponding to the associated value 
$ Redis -> keys ( $ a '*'.);

  Set an expiration time

/ * 
 * Check the remaining time a key is valid, and returns the number of seconds. 
 * When no expiration time, -1 
 * When this key value is no, returns -2 
 * / 
$ Redis -> TTL ( 'SET');

  Check expiration date

/ * 
 * The number of seconds expires 
 * to a certain timestamp (in seconds) expires when 
 * / 
$ Redis -> The expire ( 'SET', 120);

Guess you like

Origin www.cnblogs.com/DoonyLiu/p/12457289.html