PHP operation redis common method example [key and value operation]

Time:2021-1-13

This paper describes the common methods of PHP operating redis with examples. The details are as follows:

About key operation:

1. Get all keys, excluding values;


$redis ->keys("*");

2. Get the value of one or more keys, [unlimited data type];

$redis - > mget ([$key1, $key2]); // the parameter is an array;

3. Set the life cycle of the specified key;

$redis > expire ($key, 30); // set the life cycle to 30 seconds;

4. Gets the remaining life cycle of the specified key;


$redis->tTl($key);

About the operation of value:

1. String type

$key = 'user';
$redis->delete($key); 
$redis ->set($key,'11');
$redis - > setnx ($key, 3); // the setting takes effect only when $key does not exist; if it does, the value remains unchanged;
$redis - > decr ($key); // when the value of $key is a number, you can use this method to subtract 1
$redis - > incr ($key); // when the value of $key is a number, it can be increased by 1 by this method
$redis - > delete ($key); // delete the specified key value
$redis - > getmultiple ([$key, 'LX']; // get the value corresponding to multiple keys return value: return the array containing the values of all keys
$redis - > get ($key); // get the value of the specified key
$redis - > exists ($key); // judge whether the key exists

2. List linked list

$key = 'user';
$redis - > delete ($key); // delete $key data every time you come in for testing
$redis - > lpush ($key, 1); // add elements from the top of the list
$redis - > rpush ($key, 2); // add elements from the end of the list
$redis ->rPush($key,4);
$redis ->lPush($key,4); 
$redis - > lpop ($key); delete an element from the head of the linked list and return the value of this element. Because this feature is unique, list is very suitable for high concurrency seckill
$redis - > lremove ($key, 2,3); // delete the value of 2 in $key, and delete at most three values that are all 2;
$redis - > lset ($key, 2,30); // set the element with subscript 2 to 30; return value: the value found is returned successfully, but false is failed
$redis - > lget ($key, 0); // get the value of the specified subscript. Return value: the value of the search is returned successfully, and false is failed
$redis - > ltrim ($key, 0,1); // intercepts and retains the first two values. Return value: the found value is returned successfully, but false fails. It is generally used with lgetrange
$redis - > lgetrange ($key, 0, - 1); // get all the values of $key in all list lists [value can be repeated] return value: the value found is returned successfully, and false is failed
$redis - > llen ($key); // the number of statistical elements is the same as $redis - > lSize ($key). Return value: the search value is returned successfully, and false is returned if it fails

Personal summary: the last lpush indicates who the first element is; the last rpush represents who the last element is;

3. Set set [different from list, set is not allowed to have the same value]

$key1 = 'user1';
$key2 = 'user2';
$redis->delete($key1);
$redis->delete($key2);
$redis - > Sadd ($key1, '2'); // add a value for a key. Return value: true for success and false for failure
$redis ->sAdd($key1,'1');
$redis ->sAdd($key2,'2');
$redis ->sAdd($key2,'3');
$redis - > sremove ($key, '1'); // delete the specified value. Return value: true for success and false for failure
$redis - > scontains ($key1, '3'); // judge whether there is a specified value in $key1. Return value: true for success and false for failure
$redis - > ssize ($key1); // returns the number of stored values in the collection. Return value: the number of arrays is returned successfully, and 0 fails
$redis - > spop ($key1); // randomly remove and return a value in the key. Return value: the deleted value is returned successfully, and false is returned if it fails
$redis - > Sunion ($key2, $key1); // returns the union of all specified keys. Return value: the merged set is returned successfully, but false fails
$redis - > sinterstore ('user3 ', $key2', $key1); // execute sinter command and store the result in the new set. Return value: return successfully, number of intersections, fail, false
$redis - > sinter ($key1, $key2); // returns the intersection of two specified sets. Return value: array intersection is returned successfully, and false is failed
$redis - > sunonstore ('user3 ', $key2', $key1); // execute the sun ion command and store the result in the new variable. Return value: success returned, number of intersections, failure false 
$redis - > smembers ($key1); // get all the values in the collection
$redis - > sismember ($key1,2); // if the member element 2 is a set $key1, the member of the set returns 1; if the member element is not a member of the set, or the key does not exist, it returns 0
$redis - > sdiffstore ('user3 ', $key1, $key2); // execute the sdiff command and store the result in the new variable. Return value: the number is returned successfully, and false is failed
$redis - > sdiff ($key1, $key2); // returns the result that exists in the first set and does not exist in the second set. Return value: array returned successfully, false failed. Return value: number of intersection returned successfully, false failed 
$redis - > sort ($key1); // when all the values in the set are numeric, the sorted result is returned. As long as one value is not numeric, no result is returned
$redis - > scard ($key1); // returns the number of members of the set container

4. Hash object [it can be understood that each key represents a piece of data, just like the row in the database]

$key1 = 'user';
$key2 = 'user1';
$redis ->delete($key1);
$redis - > hset ($key1, 'name','lx '); // each key in hash corresponds to one or more key value pairs, just like fields and values in MySQL; and $key is similar to a piece of data
$redis - > hset ($key1, 'age','confidential ');
$redis - > hset ($key1, 'sex','male ');
$redis - > hmget ($key2, ['name ','age']; // get the values of multiple keys in the hash table
$redis - > hmset ($key2, ['name '= >'lx','age '= > 27]); // set multiple key values to the hash table
$redis - > hvals ($key1); // the order of getting all the values in the hash table is random
$redis - > hkeys ($key1); // the order of all keys in the hash table is random
$redis - > HDEL ($key1, 'user1'); // delete the specified key in a table. If the hash table does not exist or the corresponding key does not exist, false is returned
$redis - > HLEN ($key1); // the number of keys in the table is the same as the number of rows in the database
$redis ->hGet($key1,'user2');
$redis - > hsetnx ($key1, 'user2','lx '); // when a key does not exist in the hash table, set a value for the key
$redis - > hexists ($key1, 'user1'); // check whether the hash key exists. Return value: true if it exists, false if it does not exist
$redis - > hgetall ($key2); // get all the keys and values in a hash table
$redis - > hincrbyfloat ($key1, 'user3', 3.5); // add a floating-point value to a key in the hash table, provided that the existing value in the key must be a numeric type
$redis - > hincrby ($key1, 'user3', 3); // add an integer value to a key in the hash table, provided that the existing value in the key must be a numeric type

For more information about PHP, readers who are interested in it can see the following topics: PHP + redis database programming skills summary, PHP object-oriented programming introductory course, PHP basic grammar introductory course, PHP array operation skills encyclopedia, PHP string usage summary, PHP + MySQL database operation introductory course and PHP common numbers Summary of database operation skills

I hope this article is helpful for PHP programming.