Home >Backend Development >PHP Tutorial >30 code examples of common methods for operating redis in PHP_PHP Tutorial

30 code examples of common methods for operating redis in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:26:42811browse

There are many redis operations. I used to see a relatively comprehensive blog, but I can’t find it now. After searching for a long time, I will summarize some examples of PHP processing redis. I personally think some examples are commonly used. The following examples are all based on the php-redis extension.

1, connect

Description: The instance is connected to a Redis.
Parameters: host: string, port: int
Return value: BOOL Successful return: TRUE; Failure return: FALSE

Example:

Copy code The code is as follows:

$redis = new redis();
$result = $redis->connect('127.0.0.1', 6379);
var_dump($result); //Result: bool(true)
?>

2, set
Description: Set the values ​​of key and value
Parameter: Key Value
Return value: BOOL Successful return: TRUE; Failure return: FALSE
Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$result = $redis->set('test',"11111111111");
var_dump($result); //Result: bool(true)
?>

3, get

Description: Get the value for the specified key
Parameter: key
Return value: string or BOOL If the key does not exist, returns FALSE. Otherwise, return the value corresponding to the specified key.
Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$result = $redis->get('test');
var_dump($result); //Result: string(11) "11111111111"
?>

4, delete


Description: Delete the specified key
Parameters: a key, or an unspecified number of parameters, an array for each key: key1 key2 key3 ... keyN
Return value: Number of deleted items
Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$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 Successful return: TRUE; Failure return: FALSE

Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$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: Verify that the specified key exists
Parameter key
Return value: Bool Successful return: TRUE; Failure return: FALSE
Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('test',"1111111111111");
var_dump($redis->exists('test')); //Result: bool(true)
?>

7, incr

Description: Numerically incrementing stored key value key.
Parameters: key value: The value that will be added to the key
Return value: INT the new value
Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('test',"123");
var_dump($redis->incr("test")); //Result: int(124)
var_dump($redis->incr("test")); //Result: int(125)
?>

8, decr

Description: Store key value in numerical decrement.
Parameters: key value: The value that will be added to the key
Return value: INT the new value
Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->set('test',"123");
var_dump($redis->decr("test")); //Result: int(122)
var_dump($redis->decr("test")); //Result: int(121)
?>

9, getMultiple

Description: Get the values ​​of all specified keys. If one or more keys do not exist, the value of that key in this array is false
Parameters: array of lists
containing key values Return value: Returns an array containing the values ​​of all keys
Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$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: Add a string value from the head of the list. Create the list if the key does not exist. If the key exists and is not a list, return FALSE.
Parameters: key, value
Return value: Returns the array length on success, false
on failure Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
var_dump($redis->lpush("test","111")); //Result: int(1)
var_dump($redis->lpush("test","222")); //Result: int(2)
?>

11, rpush

Description: Add a string value from the end of the list. Create the list if the key does not exist. If the key exists and is not a list, return FALSE.
Parameters: key, value
Return value: Returns the array length on success, false
on failure Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$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: Return and remove the first element of the list
Parameter: key
Return value: Returns the value of the first element on success, false
on failure Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis->lpush("test","111");
$redis->lpush("test","222");
$redis->rpush("test","333");
$redis->rpush("test","444");
var_dump($redis->lpop("test")); //Result: string(3) "222"
?>

13, lsize,llen

Description: The length of the returned list. If the list does not exist or is empty, the command returns 0. If the key is not a list, this command returns FALSE.
Parameter: Key
Return value: Returns the array length on success, false
on failure Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$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: Returns the specified element stored in the list with the specified key. 0 first element, 1 second... -1 last element, -2 second last... Returns FALSE if the wrong index or key does not point to the list.
Parameter: key index
Return value: Returns the value of the specified element successfully, false
on failure Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$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)); //Result: string(3) "444"
?>

15, lset

Description: Assign a new value to the index specified in the list. If the index does not exist, return false.
Parameter: key index value
Return value: true on success, false on failure
Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis->lpush("test","111");
$redis->lpush("test","222");
var_dump($redis->lget("test",1)); //Result: string(3) "111"
var_dump($redis->lset("test",1,"333")); //Result: bool(true)
var_dump($redis->lget("test",1)); //Result: string(3) "333"
?>

16, lgetrange

Description:
Returns the specified element stored from start to end in the list of specified keys in the range, lGetRange(key, start, end). 0 for the first element, 1 for the second element... -1 for the last element, -2 for the penultimate element...
Parameters: key start end
Return value: Returns the found value if successful, false
if failed Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$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: Remove count matching values ​​from the list starting from the head. If count is zero, all matching elements are removed. If count is negative, the content is deleted from the end.
Parameters: key count value
Return value: Returns the number of deleted items if successful, false if failed
Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$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] => a )
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: Add a value to a Key. If this value is already in this Key, return FALSE.
Parameter: key value
Return value: true on success, false on failure
Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
var_dump($redis->sadd('test','111')); //Result: bool(true)
var_dump($redis->sadd('test','333')); //Result: bool(true)
print_r($redis->sort('test')); //Result: Array ( [0] => 111 [1] => 333 )
?>

19, sremove

Description: Delete the value specified in Key
Parameter: key member
Return value: true or false
Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis->sadd('test','111');
$redis->sadd('test','333');
$redis->sremove('test','111');
print_r($redis->sort('test')); //Result: Array ([0] => 333)
?>

20,smove

Description: Move the value in Key1 to Key2
Parameters: srcKey dstKey member
Return value: true or false
Example

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis->delete('test1');
$redis->sadd('test','111');
$redis->sadd('test','333');
$redis->sadd('test1','222');
$redis->sadd('test1','444');
$redis->smove('test',"test1",'111');
print_r($redis->sort('test1')); //Result: Array ( [0] => 111 [1] => 222 [2] => 444 )
?>

21, scontains

Description: Checks whether the specified value exists in the collection.
Parameter: key value
Return value: true or false
Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis->sadd('test','111');
$redis->sadd('test','112');
$redis->sadd('test','113');
var_dump($redis->scontains('test', '111')); //Result: bool(true)
?>

22,ssize

Description: Returns the number of values ​​stored in the collection
Parameter: key
Return value: The number of arrays is returned on success, 0 on failure
Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis->sadd('test','111');
$redis->sadd('test','112');
echo $redis->ssize('test'); //Result: 2
?>

23, spop

Description: Randomly remove and return a value in key
Parameter: key
Return value: Returns the deleted value if successful, false
if failed Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis->sadd("test","111");
$redis->sadd("test","222");
$redis->sadd("test","333");
var_dump($redis->spop("test")); //Result: string(3) "333"
?>

24,sinter

Description: Returns the intersection of all specified keys. If only a key is specified, this command generates members of the set. If a key does not exist, returns FALSE.
Parameters: key1, key2, keyN
Return value: array intersection is returned successfully, false
on failure Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$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: Execute the sInter command and store the result in the newly created variable.
Parameters:
Key: dstkey, the key to store the diff into.
Keys: key1, key2… keyN. key1..keyN are intersected as in sInter.
Return value: successful return, the number of intersections, false on failure
Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$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 a union of all specified keys
Parameters:
Keys: key1, key2, … , keyN
Return value: The merged set is returned successfully, false
on failure Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$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 store the result in the newly created variable.
Parameters:
Key: dstkey, the key to store the diff into.
Keys: key1, key2… keyN. key1..keyN are intersected as in sInter.
Return value: successful return, the number of intersections, false on failure
Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$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: Returns results that exist in the first set and do not exist in all other sets
Parameters: Keys: key1, key2, …, keyN: Any number of keys corresponding to sets in redis.
Return value: array returned on success, false
on failure Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$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 store the results in the newly created variable.
Parameters:
Key: dstkey, the key to store the diff into.
Keys: key1, key2, …, keyN: Any number of keys corresponding to sets in redis
Return value: Returns a number on success, false on failure
Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$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 contents of the collection
Parameters: Key: key
Return value: An array of elements, the contents of the set.
Example:

Copy code The code is as follows:

$redis = new redis();
$redis->connect('127.0.0.1', 6379);
$redis->delete('test');
$redis->sadd("test","111");
$redis->sadd("test","222");
print_r($redis->smembers('test')); //Result:Array ( [0] => 111 [1] => 222 )
?>

In php-redis, there are many functions with different names but the same function, such as: lrem and lremove, which are not listed here.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824609.htmlTechArticleThere are many redis operations. I used to see a relatively comprehensive blog, but I can’t find it now. After searching for a long time, here are some examples of PHP processing redis. I personally think some are commonly used...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn