PHP Operations Redis Common operation method Code summary

Source: Internet
Author: User
This article mainly introduces the PHP operation Redis Common Method code example, you can manipulate the string type, list type and set type of data, the need for friends can refer to the following

The following is an example of how PHP handles Redis's Phpredis extension.

1,connect

Description: The instance is connected to a redis.
Parameter: Host:string,port:int
Return value: BOOL successful return: TRUE; failure return: FALSE

2,set

Description: Sets the value of key and value
Parameter: Key Value
Return value: BOOL successful return: TRUE; failure return: FALSE

< php $redis = new Redis (); $redis->connect (' 127.0.0.1 ', 6379); $result = $redis->set (' Test ', "11111111111"); Var_dump ($result);    

3,get

Description: Gets the value of the specified key
Parameter: Key
Return value: String or bool returns FALSE if the key does not exist. Otherwise, returns the value for the specified key.

< php $redis = new Redis (); $redis->connect (' 127.0.0.1 ', 6379); $result = $redis->get (' Test '); Var_dump ($result);   

4,delete

Description: Deletes the specified key
Parameters: A key, or an indeterminate number of arguments, for each key array: Key1 Key2 Key3 ... KeyN
Return value: Number of items deleted

< php $redis = new Redis (); $redis->connect (' 127.0.0.1 ', 6379); $redis->set (' Test ', "1111111111111"); echo $redis->get (' test ');   Results: 1111111111111 $redis->delete (' Test '); Var_dump ($redis->get (' Test '));  

5,setnx

Description: If the key is not present in the database, set the key value parameter
Parameter: Key value
Return value: BOOL successful return: TRUE; failure return: FALSE

< php $redis = new Redis (); $redis->connect (' 127.0.0.1 ', 6379); $redis->set (' Test ', "1111111111111"); $redis->setnx (' Test ', "22222222"); echo $redis->get (' test ');  Results: 1111111111111 $redis->delete (' Test '); $redis->setnx (' Test ', "22222222"); echo $redis->get (' Test ');  

6,exists

Description: Verifies that the specified key is present
Parameter key
Return value: Bool successful return: TRUE; failure return: FALSE

< php $redis = new Redis (); $redis->connect (' 127.0.0.1 ', 6379); $redis->set (' Test ', "1111111111111"); Var_dump ($redis->exists (' Test '));  

7,incr

Description: The numeric increment stores the key value key.
Parameters: Key value: Values to be added to the key
Return value: INT the new value

< php $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"));  

8,decr

Description: The numeric decrement stores the key value.
Parameters: Key value: Values to be added to the key
Return value: INT the new value

< php $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: Gets the value of all specified keys. If one or more keys do not exist, the value of the key in the array is false
Parameter: The list array that contains the key values
Return value: Returns an array containing the values of all keys

< php $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: Adds a string value to the head of the list. If the key does not exist, the list is created. Returns False if the key exists and is not a list.
Parameter: Key,value
Return value: Successfully returned array length, failed false

< php $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"));   Results: Int (2)?>

11,rpush

Description: Adds a string value to the tail of the list. If the key does not exist, the list is created. Returns False if the key exists and is not a list.
Parameter: Key,value
Return value: Successfully returned array length, failed false

< php $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"));   Results: Int (4)?>

12,lpop

Description: Returns and removes the first element of a list
Parameter: Key
Return value: Successfully returns the value of the first element, failure returns false

< php $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: Returns the length of the list. If the list does not exist or is empty, the command returns 0. If the key is not a list, the command returns FALSE.
Parameter: Key
Return value: Successfully returned array length, failed false

< php $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"));  Results: Int (4)?>

14,lget

Description: Returns the specified key stored in the specified element in the list. 0 first element, 1 two ...-1 last element,-2 the penultimate ... The wrong index or key does not point to the list and returns false.
Parameter: Key index
Return value: Successfully returns the value of the specified element, failed false

< php $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: Assigns a new value to the index specified by the list and returns False if the index does not exist.
Parameter: Key index value
Return value: Successful return true, failed false

< php $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

Describe:
Returns the specified element, Lgetrange (key, start, end) in the specified list of keys in the range, starting at the end of the store. 0 first element, 1 second element ...-1 last element,-2 second from the bottom ...
Parameter: Key start end
Return value: Successfully returned the lookup value, failed false

< php $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));  

17,lremove

Description: Removes count-matched values from the list starting from the head. If Count is zero, all matching elements are deleted. If count is negative, the content is deleted from the tail.
Parameter: Key count value
Return value: Successful return of the number of deletions, failure false

< php $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));   Results: 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 this value is already in this key, it returns false.
Parameter: Key value
Return value: Successful return true, failed false

< php $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

< php $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 value in Key1 to Key2
Parameters: Srckey Dstkey Member
Return value: TRUE or False

< php $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

< php $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: Number of successfully returned arrays, failure 0

< php $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 ');   Results: 2?>

23,spop

Description: Randomly removes and returns a value from key
Parameter: Key
Return value: Successful return of deleted value, failed false

< php $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 you specify only one key, the command generates a member of the collection. Returns False if a key does not exist.
Parameters: Key1, Key2, KeyN
Return value: Successful return array intersection, failed false

< php $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: Executes the sinter command and stores the result in the newly created variable.
Parameters:
Key:dstkey, the key to store the diff into.
Keys:key1, Key2 ... keyN. Key1. KeyN is intersected as in SInter.
Return value: Successful return, number of intersections, failure false

< php $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 '));  

26,sunion

Describe:
Returns a set of all specified keys
Parameters:
Keys:key1, Key2, ..., KeyN
Return value: Successful return of merged set, failed false

< php $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: Executes the sunion command and stores the result in the newly created variable.
Parameters:
Key:dstkey, the key to store the diff into.
Keys:key1, Key2 ... keyN. Key1. KeyN is intersected as in SInter.
Return value: Successful return, number of intersections, failure false

< php $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 a result that exists in the first collection and does not exist in all other collections
Parameters: Keys:key1, Key2, ..., keyn:any number of Keys corresponding to sets in Redis.
Return value: Successfully returned array, failed false

< php $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: Executes the Sdiff command and stores the result 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: Successful return number, failed false

< php $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

Describe:
Returns the contents of a collection
Parameter: Key:key
Return value: An array of elements, the contents of the set.

< php $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)?>

31,setex,ttl

Describe:
Setex Setting the Key,value expiration time

TTL returns the expiration time remaining for a key

$redis->setex ("Test", "value"); Set key to Test,value as value expires at 20 seconds for Echo $redis->get ("test"), Echo $redis->ttl ("test"); Returns the remaining expiration time (returned-2 expired, no expiration Time returned-1)

Php-redis, there are many different names, but functions of the same function, such as: Lrem and Lremove, here is not an example.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.