php-redis-based Redis Operations

Source: Internet
Author: User
Tags diff
<span id="Label3"></p><p><p></p></p><p><p><strong>1,connect</strong></p></p><p><p>Description: The instance is connected to a Redis.<br>Parameter: Host:string,port:int<br>Return value: BOOL Successful return: TRUE; failure return: FALSE</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$result = $redis->connect (' 127.0.0.1 ', 6379);<br>Var_dump ($result); result: BOOL (true)<br>?></p></p><p><p><strong>2,set<br></strong>Description: sets the value of key and value<br>Parameter: Key Value<br>Return value: BOOL Successful return: TRUE; failure return: FALSE</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$result = $redis->set (' Test ', "11111111111″");<br>Var_dump ($result); result: BOOL (true)<br>?></p></p><p><p><strong>3,get</strong></p></p><p><p>Description: gets the value of the specified key<br>Parameter: Key<br>return value: string or bool returns FALSE if the key does not exist. otherwise, returns the value for the specified key.</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$result = $redis->get (' Test ');<br>Var_dump ($result); Result: string (11) "11111111111"<br>?></p></p><p><p><strong>4,delete</strong></p></p><p><p><strong><br></strong>Description: deletes the specified key<br>Parameters: a key, or an indeterminate number of arguments, for each key array: key1 key2 Key3 ... KeyN<br>Return value: number of items deleted</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->set (' Test ', "1111111111111″");<br>echo $redis->get (' Test '); Results: 1111111111111<br>$redis->delete (' Test ');<br>Var_dump ($redis->get (' Test ')); result: BOOL (false)<br>?></p></p><p><p><strong>5,setnx</strong></p></p><p><p>Description: if the key is not present in the database, set the key value parameter<br>Parameter: Key value<br>Return value: BOOL Successful return: TRUE; failure return: FALSE</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->set (' Test ', "1111111111111″");<br>$redis->setnx (' Test ', "22222222″");<br>echo $redis->get (' Test '); Results: 1111111111111<br>$redis->delete (' Test ');<br>$redis->setnx (' Test ', "22222222″");<br>echo $redis->get (' Test '); Results: 22222222<br>?></p></p><p><p><strong>6,exists</strong></p></p><p><p>Description: verifies that the specified key is present<br>Parameter key<br>Return value: Bool Successful return: TRUE; failure return: FALSE</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->set (' Test ', "1111111111111″");<br>Var_dump ($redis->exists (' Test ')); result: BOOL (true)<br>?></p></p><p><p><strong>7,incr</strong></p></p><p><p>Description: the numeric increment stores the key value Key.<br>Parameters: key value: values to be added to the key<br>Return value: INT The new value</p></p><p><p><strong>Instance:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->set (' Test ', "123″");<br>Var_dump ($REDIS->INCR ("test")); results: int (124)<br>Var_dump ($REDIS->INCR ("test")); results: int (125)<br>?></p></p><p><p></p></p><p><p><strong>8,decr</strong></p></p><p><p>Description: the numeric decrement stores the key Value.<br>Parameters: key value: values to be added to the key<br>Return value: INT The new value</p></p><p><p><strong>Instance:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->set (' Test ', "123″");<br>Var_dump ($REDIS->DECR ("test")); results: int (122)<br>Var_dump ($REDIS->DECR ("test")); results: int (121)<br>?></p></p><p><p><strong>9,getmultiple</strong></p></p><p><p>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<br>Parameter: the list array that contains the key values<br>Return value: returns an array containing the values of all keys</p></p><p><p><strong>Instance:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->set (' test1 ', "1″");<br>$redis->set (' test2 ', "2″");<br>$result = $redis->getmultiple (array (' test1′, ' test2 '));<br>Print_r ($result); Result: Array ([0] = 1 [1] = 2)<br>?></p></p><p><p></p></p><p><p><strong>10,lpush</strong></p></p><p><p>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.<br>Parameter: Key,value<br>Return value: successfully returned array length, failed false</p></p><p><p><strong>Instance:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->delete (' Test ');<br>Var_dump ($redis->lpush ("test", "111″"); results: int (1)<br>Var_dump ($redis->lpush ("test", "222″"); results: int (2)<br>?></p></p><p><p></p></p><p><p><strong>11,rpush</strong></p></p><p><p>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.<br>Parameter: Key,value<br>Return value: successfully returned array length, failed false</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->delete (' Test ');<br>Var_dump ($redis->lpush ("test", "111″"); results: int (1)<br>Var_dump ($redis->lpush ("test", "222″"); results: int (2)<br>Var_dump ($redis->rpush ("test", "333″"); results: int (3)<br>Var_dump ($redis->rpush ("test", "444″"); results: int (4)<br>?></p></p><p><p></p></p><p><p><strong>12,lpop</strong></p></p><p><p>Description: returns and removes the first element of a list<br>Parameter: Key<br>Return value: successfully returns the value of the first element, failure returns false</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->delete (' Test ');<br>$redis->lpush ("test", "111″");<br>$redis->lpush ("test", "222″");<br>$redis->rpush ("test", "333″");<br>$redis->rpush ("test", "444″");<br>Var_dump ($redis->lpop ("test")); Result: string (3) "222"<br>?></p></p><p><p></p></p><p><p><strong>13,lsize,llen</strong></p></p><p><p>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.<br>Parameter: Key<br>Return value: successfully returned array length, failed false</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->delete (' Test ');<br>$redis->lpush ("test", "111″");<br>$redis->lpush ("test", "222″");<br>$redis->rpush ("test", "333″");<br>$redis->rpush ("test", "444″");<br>Var_dump ($redis->lsize ("test")); results: int (4)<br>?></p></p><p><p></p></p><p><p><strong>14,lget</strong></p></p><p><p>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.<br>Parameter: Key Index<br>Return value: successfully returns the value of the specified element, failed false</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->delete (' Test ');<br>$redis->lpush ("test", "111″");<br>$redis->lpush ("test", "222″");<br>$redis->rpush ("test", "333″");<br>$redis->rpush ("test", "444″");<br>Var_dump ($redis->lget ("test", 3)); Result: string (3) "444"<br>?></p></p><p><p></p></p><p><p><strong>15,lset</strong></p></p><p><p>Description: assigns a new value to the index specified by the list and returns False if the index does not exist.<br>Parameter: Key index value<br>Return value: successful return true, failed false</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->delete (' Test ');<br>$redis->lpush ("test", "111″");<br>$redis->lpush ("test", "222″");<br>Var_dump ($redis->lget ("test", 1)); Result: string (3) "111"<br>Var_dump ($redis->lset ("test", 1, "333″)"); result: BOOL (true)<br>Var_dump ($redis->lget ("test", 1)); Result: string (3) "333"<br>?></p></p><p><p></p></p><p><p><strong>16,lgetrange</strong></p></p><p><p>Describe:<br>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 ...<br>Parameter: Key Start end<br>Return value: successfully returned the lookup value, failed false</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->delete (' Test ');<br>$redis->lpush ("test", "111″");<br>$redis->lpush ("test", "222″");<br>Print_r ($redis->lgetrange ("test", 0,-1)); Result: Array ([0] = 222 [1] = 111)<br>?></p></p><p><p></p></p><p><p><strong>17,lremove</strong></p></p><p><p>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.<br>Parameter: Key Count value<br>Return value: successful return of the number of deletions, failure false</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->delete (' Test ');<br>$redis->lpush (' Test ', ' a ');<br>$redis->lpush (' Test ', ' b ');<br>$redis->lpush (' Test ', ' C ');<br>$redis->rpush (' Test ', ' a ');<br>Print_r ($redis->lgetrange (' Test ', 0,-1)); Result: Array ([0] = c [1] = b [2] = = a [3] = A)<br>Var_dump ($redis->lremove (' Test ', ' a ', 2)); results: int (2)<br>Print_r ($redis->lgetrange (' Test ', 0,-1)); Result: Array ([0] = c [1] = B)<br>?></p></p><p><p></p></p><p><p><strong>18,sadd</strong></p></p><p><p>Description: adds a value to a key. If this value is already in this key, it returns FALSE.<br>Parameter: Key value<br>Return value: successful return true, failed false</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->delete (' Test ');<br>Var_dump ($redis->sadd (' Test ', ' 111′ '); result: BOOL (true)<br>Var_dump ($redis->sadd (' Test ', ' 333′ '); result: BOOL (true)<br>Print_r ($redis->sort (' Test ')); Result: Array ([0] = 111 [1] = 333)<br>?></p></p><p><p></p></p><p><p><strong>19,sremove</strong></p></p><p><p>Description: Delete the value specified in key<br>Parameter: Key Member<br>Return value: true or False</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->delete (' Test ');<br>$redis->sadd (' Test ', ' 111′ ');<br>$redis->sadd (' Test ', ' 333′ ');<br>$redis->sremove (' Test ', ' 111′ ');<br>Print_r ($redis->sort (' Test ')); Result: Array ([0] = 333)<br>?></p></p><p><p></p></p><p><p><strong>20,smove</strong></p></p><p><p>Description: move value in Key1 to Key2<br>Parameters: Srckey Dstkey Member<br>Return value: true or False</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->delete (' Test ');<br>$redis->delete (' test1 ');<br>$redis->sadd (' Test ', ' 111′ ');<br>$redis->sadd (' Test ', ' 333′ ');<br>$redis->sadd (' test1′, ' 222 ');<br>$redis->sadd (' test1′, ' 444 ');<br>$redis->smove (' Test ', ' test1″, ' 111′);<br>Print_r ($redis->sort (' test1 ')); Result: Array ([0] = 111 [1] = 222 [2] = 444)<br>?></p></p><p><p></p></p><p><p><strong>21,scontains</strong></p></p><p><p>Description: checks whether the specified value exists in the Collection.<br>Parameter: Key value<br>Return value: true or False</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->delete (' Test ');<br>$redis->sadd (' Test ', ' 111′ ');<br>$redis->sadd (' Test ', ' 112′ ');<br>$redis->sadd (' Test ', ' 113′ ');<br>Var_dump ($redis->scontains (' Test ', ' 111 ')); result: BOOL (true)<br>?></p></p><p><p></p></p><p><p><strong>22,ssize</strong></p></p><p><p>Description: returns the number of values stored in the collection<br>Parameter: Key<br>Return value: number of successfully returned arrays, failure 0</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->delete (' Test ');<br>$redis->sadd (' Test ', ' 111′ ');<br>$redis->sadd (' Test ', ' 112′ ');<br>echo $redis->ssize (' Test '); Results: 2<br>?></p></p><p><p></p></p><p><p><strong>23,spop</strong></p></p><p><p>Description: randomly removes and returns a value from key<br>Parameter: Key<br>Return value: successful return of deleted value, failed false</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->delete (' Test ');<br>$redis->sadd ("test", "111″");<br>$redis->sadd ("test", "222″");<br>$redis->sadd ("test", "333″");<br>Var_dump ($redis->spop ("test")); Result: string (3) "333"<br>?></p></p><p><p></p></p><p><p><strong>24,sinter</strong></p></p><p><p>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.<br>Parameters: key1, key2, KeyN<br>Return value: successful return array intersection, failed false</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->delete (' Test ');<br>$redis->sadd ("test", "111″");<br>$redis->sadd ("test", "222″");<br>$redis->sadd ("test", "333″");<br>$redis->sadd ("test1″," 111 ");<br>$redis->sadd ("test1″," 444 ");<br>Var_dump ($redis->sinter ("test", "test1″"); Result: array (1) {[0]=> string (3) "111"}<br>?></p></p><p><p></p></p><p><p><strong>25,sinterstore</strong></p></p><p><p>Description: executes the sinter command and stores the result in the newly created Variable.<br>Parameters:<br>key:dstkey, the key to store the diff into.<br>keys:key1, Key2 ... keyN. key1. KeyN is intersected as in SInter.<br>Return value: successful return, number of intersections, failure false</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->delete (' Test ');<br>$redis->sadd ("test", "111″");<br>$redis->sadd ("test", "222″");<br>$redis->sadd ("test", "333″");<br>$redis->sadd ("test1″," 111 ");<br>$redis->sadd ("test1″," 444″);<br>Var_dump ($redis->sinterstore (' new ', "test", "test1″)"); results: int (1)<br>Var_dump ($redis->smembers (' New ')); Result: array (1) {[0]=> string (3) "111"}<br>?></p></p><p><p></p></p><p><p><strong>26,sunion</strong></p></p><p><p>Describe:<br>Returns a set of all specified keys<br>Parameters:<br>keys:key1, key2, ..., KeyN<br>Return value: successful return of merged set, failed false</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->delete (' Test ');<br>$redis->sadd ("test", "111″");<br>$redis->sadd ("test", "222″");<br>$redis->sadd ("test", "333″");<br>$redis->sadd ("test1″," 111 ");<br>$redis->sadd ("test1″," 444 ");<br>Print_r ($redis->sunion ("test", "test1″"); Result: Array ([0] = 111 [1] = 222 [2] = = 333 [3] = 444)<br>?></p></p><p><p></p></p><p><p><strong>27,sunionstore</strong></p></p><p><p>Description: executes the sunion command and stores the result in the newly created Variable.<br>Parameters:<br>key:dstkey, the key to store the diff into.<br>keys:key1, Key2 ... keyN. key1. KeyN is intersected as in SInter.<br>Return value: successful return, number of intersections, failure false</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->delete (' Test ');<br>$redis->sadd ("test", "111″");<br>$redis->sadd ("test", "222″");<br>$redis->sadd ("test", "333″");<br>$redis->sadd ("test1″," 111 ");<br>$redis->sadd ("test1″," 444″);<br>Var_dump ($redis->sinterstore (' new ', "test", "test1″)"); results: int (4)<br>Print_r ($redis->smembers (' New ')); Result: Array ([0] = 111 [1] = 222 [2] = = 333 [3] = 444)<br>?></p></p><p><p></p></p><p><p><strong>28,sdiff</strong></p></p><p><p>Description: Returns a result that exists in the first collection and does not exist in all other collections<br>Parameters: keys:key1, key2, ..., keyn:any number of Keys corresponding to sets in Redis.<br>Return value: successfully returned array, failed false</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->delete (' Test ');<br>$redis->sadd ("test", "111″");<br>$redis->sadd ("test", "222″");<br>$redis->sadd ("test", "333″");<br>$redis->sadd ("test1″," 111 ");<br>$redis->sadd ("test1″," 444 ");<br>Print_r ($redis->sdiff ("test", "test1″"); Result: Array ([0] = 222 [1] = 333)<br>?></p></p><p><p></p></p><p><p><strong>29,sdiffstore</strong></p></p><p><p>Description: executes the Sdiff command and stores the result in the newly created Variable.<br>Parameters:<br>key:dstkey, the key to store the diff into.<br>keys:key1, key2, ..., keyn:any number of keys corresponding to sets in Redis<br>Return value: successful return number, failed false</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->delete (' Test ');<br>$redis->sadd ("test", "111″");<br>$redis->sadd ("test", "222″");<br>$redis->sadd ("test", "333″");<br>$redis->sadd ("test1″," 111 ");<br>$redis->sadd ("test1″," 444″);<br>Var_dump ($redis->sdiffstore (' new ', "test", "test1″)"); results: int (2)<br>Print_r ($redis->smembers (' New ')); Result: Array ([0] = 222 [1] = 333)<br>?></p></p><p><p></p></p><p><p><strong>30,smembers, sgetmembers</strong></p></p><p><p>Describe:<br>Returns the contents of a collection<br>Parameter: Key:key<br>Return value: An array of elements, the contents of the Set.</p></p><p><p><strong>Example:</strong><br><?php<br>$redis = new Redis ();<br>$redis->connect (' 127.0.0.1 ', 6379);<br>$redis->delete (' Test ');<br>$redis->sadd ("test", "111″");<br>$redis->sadd ("test", "222″");<br>Print_r ($redis->smembers (' Test ')); Result: Array ([0] = 111 [1] = 222)<br>?></p></p><p><p></p></p><p><p>for <strong>Reprint please specify:</strong> reproduced from 26 points of the blog</p></p><p><p><strong>This article link address:</strong> php-redis-based redis Operations</p></p><p><p>php-redis-based Redis Operations</p></p></span>
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.