30 common php code examples for redis operations. Redis has many operations. I used to see a full blog, but I cannot find it now. Search for things for half a day. let's take a look at the example of php processing redis. I think there are a lot of common redis operations. I used to see a full blog, but I cannot find it now. Search for things for half a day. Below are some examples of php processing redis, which I think are commonly used. The following example is based on the php-redis extension.
1, connect
Description: the instance is connected to a Redis instance.
Parameter: host: string, port: int
Return value: BOOL success return: TRUE; failure return: FALSE
Example:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Result = $ redis-> connect ('2017. 0.0.1 ', 127 );
Var_dump ($ result); // result: bool (true)
?>
2, set
Description: sets the key and value values.
Parameter: Key Value
Return value: BOOL success return: TRUE; failure return: FALSE
Example:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Result = $ redis-> set ('test', "11111111111 ");
Var_dump ($ result); // result: bool (true)
?>
3, get
Description: Gets the value of a specified key.
Parameter: key
Return value: string or BOOL. if the key does not exist, FALSE is returned. Otherwise, the value corresponding to the specified key is returned.
Example:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Result = $ redis-> get ('test ');
Var_dump ($ result); // The result is string (11) "11111111111"
?>
4. delete
Description: deletes a specified key.
Parameter: a key, or an uncertain number of parameters, each key array: key1 key2 key3... KeyN
Return value: number of deleted items
Example:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> set ('test', "1111111111111 ");
Echo $ redis-> get ('test'); // Result: 1111111111111
$ Redis-> delete ('test ');
Var_dump ($ redis-> get ('test'); // Result: bool (false)
?>
5, setnx
Description: if the key does not exist in the database, set the key value parameter.
Parameter: key value
Return value: BOOL success return: TRUE; failure return: FALSE
Example:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> set ('test', "1111111111111 ");
$ Redis-> setnx ('test', "22222222 ");
Echo $ redis-> get ('test'); // Result: 1111111111111
$ Redis-> delete ('test ');
$ Redis-> setnx ('test', "22222222 ");
Echo $ redis-> get ('test'); // Result: 22222222
?>
6. exists
Description: checks whether the specified key exists.
Parameter key
Return value: Bool success return: TRUE; failure return: FALSE
Example:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> set ('test', "1111111111111 ");
Var_dump ($ redis-> exists ('test'); // Result: bool (true)
?>
7, incr
Description: storage key-value for incremental data.
Parameter: key value: value to be added to the key
Return value: INT the new value
Instance:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> set ('test', "123 ");
Var_dump ($ redis-> incr ("test"); // Result: int (124)
Var_dump ($ redis-> incr ("test"); // Result: int (125)
?>
8, decr
Description: The storage key value of the decimal number.
Parameter: key value: value to be added to the key
Return value: INT the new value
Instance:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> set ('test', "123 ");
Var_dump ($ redis-> decr ("test"); // Result: int (122)
Var_dump ($ redis-> decr ("test"); // Result: int (121)
?>
9, getMultiple
Description: Gets the values of all specified keys. If one or more keys do not exist, the value of the key in the array is false.
Parameter: list array containing key values
Return value: returns an array containing values of all keys.
Instance:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> set ('test1', "1 ");
$ Redis-> set ('test2', "2 ");
$ Result = $ redis-> getMultiple (array ('test1', 'test2 '));
Print_r ($ result); // result: Array ([0] => 1 [1] => 2)
?>
10, lpush
Description: Adds a string value to the list header. If this key does not exist, the list is created. If the key exists and is not a list, FALSE is returned.
Parameter: key, value
Returned Value: The array length is returned successfully. if the returned value is false
Instance:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
Var_dump ($ redis-> lpush ("test", "111"); // Result: int (1)
Var_dump ($ redis-> lpush ("test", "222"); // Result: int (2)
?>
11. rpush
Description: Adds a string value at the end of the list. If this key does not exist, the list is created. If the key exists and is not a list, FALSE is returned.
Parameter: key, value
Returned Value: The array length is returned successfully. if the returned value is false
Example:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
Var_dump ($ redis-> lpush ("test", "111"); // Result: int (1)
Var_dump ($ redis-> lpush ("test", "222"); // Result: int (2)
Var_dump ($ redis-> rpush ("test", "333"); // Result: int (3)
Var_dump ($ redis-> rpush ("test", "444"); // Result: int (4)
?>
12, lpop
Description: returns and removes the first element of the list.
Parameter: key
Return value: the value of the first element is returned. if the return value fails, false is returned.
Example:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> lpush ("test", "111 ");
$ Redis-> lpush ("test", "222 ");
$ Redis-> rpush ("test", "333 ");
$ Redis-> rpush ("test", "444 ");
Var_dump ($ redis-> lpop ("test"); // The result is: string (3) "222"
?>
13, lsize, llen
Description: the length of the returned list. If the list does not exist or is empty, this command returns 0. If the key is not a list, the command returns FALSE.
Parameter: Key
Returned Value: The array length is returned successfully. if the returned value is false
Example:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> lpush ("test", "111 ");
$ Redis-> lpush ("test", "222 ");
$ Redis-> rpush ("test", "333 ");
$ Redis-> rpush ("test", "444 ");
Var_dump ($ redis-> lsize ("test"); // Result: int (4)
?>
14, lget
Description: return the elements specified by the specified key stored in the list. 0: The first element, 1: The second... -1: The last element,-2's penultimate... If the wrong index or key does not point to the list, FALSE is returned.
Parameter: key index
Returned value: the value of the specified element is returned successfully. if the returned value is false
Example:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> lpush ("test", "111 ");
$ Redis-> lpush ("test", "222 ");
$ Redis-> rpush ("test", "333 ");
$ Redis-> rpush ("test", "444 ");
Var_dump ($ redis-> lget ("test", 3); // The result is: string (3) "444"
?>
15, lset
Description: Adds a value to the index specified in the list. if the index does not exist, false is returned.
Parameter: key index value
Return value: true if the call is successful, false if the call fails.
Example:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> lpush ("test", "111 ");
$ Redis-> lpush ("test", "222 ");
Var_dump ($ redis-> lget ("test", 1); // The result is: string (3) "111"
Var_dump ($ redis-> lset ("test", 1, "333"); // Result: bool (true)
Var_dump ($ redis-> lget ("test", 1); // The result is: string (3) "333"
?>
16, lgetrange
Description:
Returns the specified element lGetRange (key, start, end) from start to end in the specified key list of the region ). 0: The first element, 1: The second element... -1: The last element,-2's penultimate...
Parameter: key start end
Return value: the search value is returned successfully. if the query fails, false is returned.
Example:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> lpush ("test", "111 ");
$ Redis-> lpush ("test", "222 ");
Print_r ($ redis-> lgetrange ("test", 0,-1); // result: Array ([0] => 222 [1] => 111)
?>
17, lremove
Description: removes count matched values from the header of the list. If count is zero, all matched elements are deleted. If count is a negative number, the content is deleted from the end.
Parameter: key count value
Return value: the number of deleted items returned. if the deletion fails, the return value is false.
Example:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> lpush ('test', 'A ');
$ Redis-> lpush ('test', 'B ');
$ Redis-> lpush ('test', 'C ');
$ Redis-> rpush ('test', 'A ');
Print_r ($ redis-> lgetrange ('test', 0,-1); // result: array ([0] => c [1] => B [2] => a [3] =>)
Var_dump ($ redis-> lremove ('test', 'A', 2); // Result: int (2)
Print_r ($ redis-> lgetrange ('test', 0,-1); // result: Array ([0] => c [1] => B)
?>
18, sadd
Description: Adds a value to a Key. If the value is already in this Key, FALSE is returned.
Parameter: key value
Return value: true if the call is successful, false if the call fails.
Example:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
Var_dump ($ redis-> sadd ('test', '2013'); // Result: bool (true)
Var_dump ($ redis-> sadd ('test', '2013'); // Result: bool (true)
Print_r ($ redis-> sort ('test'); // result: Array ([0] => 111 [1] => 333)
?>
19, sremove
Description: deletes the specified value in the Key.
Parameter: key member
Return value: true or false
Example:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> sadd ('test', '123 ');
$ Redis-> sadd ('test', '123 ');
$ Redis-> sremove ('test', '123 ');
Print_r ($ redis-> sort ('test'); // result: Array ([0] => 333)
?>
20, smove
Description: move the value in Key1 to Key2.
Parameter: srcKey dstKey member
Return value: true or false
Example
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> delete ('test1 ');
$ Redis-> sadd ('test', '123 ');
$ Redis-> sadd ('test', '123 ');
$ Redis-> sadd ('test1', '123 ');
$ Redis-> sadd ('test1', '123 ');
$ Redis-> smove ('test', "test1", '123 ');
Print_r ($ redis-> sort ('test1'); // result: Array ([0] => 111 [1] => 222 [2] => 444)
?>
21, scontains
Description: checks whether a specified value exists in the set.
Parameter: key value
Return value: true or false
Example:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> sadd ('test', '123 ');
$ Redis-> sadd ('test', '123 ');
$ Redis-> sadd ('test', '123 ');
Var_dump ($ redis-> scontains ('test', '2013'); // Result: bool (true)
?>
22, ssize
Description: number of stored values in the collection.
Parameter: key
Returned value: the number of returned arrays. if the return value is successful, the return value is 0.
Example:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> sadd ('test', '123 ');
$ Redis-> sadd ('test', '123 ');
Echo $ redis-> ssize ('test'); // Result: 2
?>
23, spop
Description: random removal and return a value in the key.
Parameter: key
Return value: the deleted value is returned. if the deletion fails, false is returned.
Example:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> sadd ("test", "111 ");
$ Redis-> sadd ("test", "222 ");
$ Redis-> sadd ("test", "333 ");
Var_dump ($ redis-> spop ("test"); // The result is: string (3) "333"
?>
24, sinter
Description: returns the intersection of all specified keys. If only one key is specified, this command generates the members of this set. If a key does not exist, FALSE is returned.
Parameter: key1, key2, keyN
Returned value: Array intersection is returned successfully. if the return value is false
Example:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> sadd ("test", "111 ");
$ Redis-> sadd ("test", "222 ");
$ Redis-> sadd ("test", "333 ");
$ Redis-> sadd ("test1", "111 ");
$ Redis-> sadd ("test1", "444 ");
Var_dump ($ redis-> sinter ("test", "test1"); // result: array (1) {[0] => string (3) "111 "}
?>
25, sinterstore
Description: run the sInter command and save the result to the new variable.
Parameters:
Key: dstkey, the key to store the diff.
Keys: key1, key2... KeyN. key1.. keyN are intersected as in sInter.
Return value: success, number of intersections, failure, false
Example:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> sadd ("test", "111 ");
$ Redis-> sadd ("test", "222 ");
$ Redis-> sadd ("test", "333 ");
$ Redis-> sadd ("test1", "111 ");
$ Redis-> sadd ("test1", "444 ");
Var_dump ($ redis-> sinterstore ('new', "test", "test1"); // Result: int (1)
Var_dump ($ redis-> smembers ('New'); // result: array (1) {[0] => string (3) "111 "}
?>
26, sunion
Description:
Returns the union of all specified keys.
Parameters:
Keys: key1, key2 ,... , KeyN
Returned value: the merged set is returned successfully. if the merging fails, false is returned.
Example:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> sadd ("test", "111 ");
$ Redis-> sadd ("test", "222 ");
$ Redis-> sadd ("test", "333 ");
$ Redis-> sadd ("test1", "111 ");
$ Redis-> sadd ("test1", "444 ");
Print_r ($ redis-> sunion ("test", "test1"); // result: array ([0] => 111 [1] => 222 [2] => 333 [3] => 444)
?>
27, sunionstore
Description: execute the sunion command and save the result to the new variable.
Parameters:
Key: dstkey, the key to store the diff.
Keys: key1, key2... KeyN. key1.. keyN are intersected as in sInter.
Return value: success, number of intersections, failure, false
Example:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> sadd ("test", "111 ");
$ Redis-> sadd ("test", "222 ");
$ Redis-> sadd ("test", "333 ");
$ Redis-> sadd ("test1", "111 ");
$ Redis-> sadd ("test1", "444 ");
Var_dump ($ redis-> sinterstore ('new', "test", "test1"); // Result: int (4)
Print_r ($ redis-> smembers ('New'); // result: array ([0] => 111 [1] => 222 [2] => 333 [3] => 444)
?>
28, sdiff
Description: return the result that exists in the first set and does not exist in all other sets.
Parameter: Keys: key1, key2 ,... , KeyN: Any number of keys corresponding to sets in redis.
Returned Value: The array is returned successfully, and the returned value is false.
Example:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> sadd ("test", "111 ");
$ Redis-> sadd ("test", "222 ");
$ Redis-> sadd ("test", "333 ");
$ Redis-> sadd ("test1", "111 ");
$ Redis-> sadd ("test1", "444 ");
Print_r ($ redis-> sdiff ("test", "test1"); // result: Array ([0] => 222 [1] => 333)
?>
29, sdiffstore
Description: execute the sdiff command and save the result to the new variable.
Parameters:
Key: dstkey, the key to store the diff.
Keys: key1, key2 ,... , KeyN: Any number of keys corresponding to sets in redis
Return value: a number is returned for success, and false is returned for failure.
Example:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> sadd ("test", "111 ");
$ Redis-> sadd ("test", "222 ");
$ Redis-> sadd ("test", "333 ");
$ Redis-> sadd ("test1", "111 ");
$ Redis-> sadd ("test1", "444 ");
Var_dump ($ redis-> sdiffstore ('new', "test", "test1"); // Result: int (2)
Print_r ($ redis-> smembers ('New'); // result: Array ([0] => 222 [1] => 333)
?>
30, smembers, sgetmembers
Description:
Returns the content of the set.
Parameter: Key: key
Returned value: An array of elements, the contents of the set.
Example:
The code is as follows:
<? Php
$ Redis = new redis ();
$ Redis-> connect ('2017. 0.0.1 ', 127 );
$ Redis-> delete ('test ');
$ Redis-> sadd ("test", "111 ");
$ Redis-> sadd ("test", "222 ");
Print_r ($ redis-> smembers ('test'); // result: Array ([0] => 111 [1] => 222)
?>
Php-redis has many different names, but functions with the same functions, such as lrem and lremove.
Bytes. Search for things for half a day. Below are some examples of php processing redis, which I think are commonly used...