Redis PHP Example One

Source: Internet
Author: User
Tags php example
Redis PHP Instance One

Redis has a lot of operations, previously saw a more full blog, but now can not find. Find something to search for half a day, the following to organize the example of the PHP processing Redis, the personal feel commonly used some examples. The following examples are based on the Php-redis 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

?

?

 
  Connect (' 127.0.0.1 ', 6379);  Var_dump ($result); Result: BOOL (true)  ?>  

?

?

2,set

?

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

?

?

    
 
  Connect (' 127.0.0.1 ', 6379);      $result = $redis->set (' Test ', "11111111111");      Var_dump ($result);    Result: BOOL (true)      ?>  

?

?

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.

?

?

    
 
  Connect (' 127.0.0.1 ', 6379);      $result = $redis->get (' test ');      Var_dump ($result);   Result: String (one) "11111111111"      ?>  

?

?

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

?

?

    
 
  Connect (' 127.0.0.1 ', 6379);      $redis->set (' Test ', ' 1111111111111 ');      echo $redis->get (' test ');   Results: 1111111111111      $redis->delete (' test ');      Var_dump ($redis->get (' Test '));  Result: bool (false)      ?>  

?

?

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

?

?

 
  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 ');  Results: 22222222  

?

?

6,exists

?

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

?

?

    
 
  Connect (' 127.0.0.1 ', 6379);      $redis->set (' Test ', ' 1111111111111 ');      Var_dump ($redis->exists (' Test '));  Result: BOOL (true)      ?>  

?

?

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

?

 
  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 (?>)  

?

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

?

 
  Connect (' 127.0.0.1 ', 6379);  $redis->set (' Test ', ' 123 ');  Var_dump ($redis->decr ("test"));  Result: Int (122)  var_dump ($redis->decr ("test"));  Results: 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

?

 
  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

?

 
  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

?

 
  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

?

 
  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"  ?>

?

Will 12,rpop be removed after a successful return or eventually deleted anyway?

?

Description: Returns and removes the last element of the list
Parameter: Key
Return value: The value of the last element was successfully returned, the failure returned false

?

 
  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->rpop ("test"));  Result: String (3) "444"  

?

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

?

 
  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

 
  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

?

?

 
  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

?

?

    
 
  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: 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

?

?

 
  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: 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

?

?

 
  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

?

?

 
  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

?

?

 
  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

?

?

 
  Connect (' 127.0.0.1 ', 6379);  $redis->delete (' test ');  $redis->sadd (' Test ', ' 111 ');  $redis->sadd (' Test ', ' the ' "");  $redis->sadd (' Test ', ' 113 ');  Var_dump ($redis->scontains (' Test ', ' 111 ')); Result: BOOL (true)  ?>

?

?

?

 
  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)  

? 22,ssize

?

Description: Returns the number of values stored in the collection
Parameter: Key
Return value: Number of successfully returned arrays, failure 0

?

?

 
  Connect (' 127.0.0.1 ', 6379);  $redis->delete (' test ');  $redis->sadd (' Test ', ' 111 ');  $redis->sadd (' Test ', ' the ' "");  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

?

?

 
  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

?

?

 
  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

?

 
  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

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

?

?

 
  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

?

 
  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 (Difference set)

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

?

?

     
 
  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

?

    
 
  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.
Example:

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

?

Original: http://blog.51yip.com/cache/1439.html

?

  • 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.