A detailed description of the PHP operation Redis Step

Source: Internet
Author: User
This time for everyone to bring the PHP operation Redis steps in detail, PHP operation of the Redis note what, the following is the actual case, together to see.

One, Redis connection and authentication

Connection parameters: IP, port, connection timeout time, the connection succeeds return true, otherwise return False$ret = $redis->connect (' 127.0.0.1 ', 6379, 30);//Password Authentication: Success returns TRUE, otherwise returns false $ret = $redis->auth (' 123456 ');

Second, string manipulation

Set key value: Returns True, otherwise returns False$redis->set (' MyStr ', ' welcome! '); /Get Key value: Successful return string type key value, if key does not exist or is not string type return False$ret = $redis->get (' mystr ');//start with a specified offset of a string stored by a key, replace with another specified string , the length of the new string after the replacement is returned successfully. $ret = $redis->setrange (' mystr ', 7, ' to beijing! '); /Gets a substring of the string stored in the specified key. $ret = $redis->getrange (' mystr ', 0, 6);//Set new value, return old value: Set value if key does not exist, return False$ret = $redis->getset (' mystr ', ' Hi Man '); /Set multiple key-value pairs at once: Returns true successfully. $ret = $redis->mset ([' name ' = ' jet ', ' age ' + 18]);//Get the value of multiple keys at once: Returns a key value array, where the key value that does not exist is false. $ret = $redis->mget ([' Name ', ' age ']);//Set the value of the specified key and its expiration, in seconds. Parameters: Key Name, expiration time, key value. Successful return True. $ret = $redis->setex (' name ', ' Ten ', ' Jetwu ');//Set the value and expiration time of the specified key in milliseconds. Successful return True. $ret = $redis->psetex (' name ', ' Ten ', ' Jetwu '), and the value of//key is not present until it is set. Key does not exist and the setting succeeds returns True, otherwise false is returned. $ret = $redis->setnx (' name ', ' Boby ');//setnx command bulk operation. The setting succeeds only if a given key does not exist, and all keys cannot be set successfully as long as one of the keys is present. $ret = $redis->msetnx ([' country ' = ' China ', ' city ' = ' Shenzhen ']);//Gets the length of the string specified by the key stored, key does not exist return 0, False is not returned for the string. $ret = $rediS->strlen (' name ');//adds 1 to the numeric value stored by the specified key. If key does not exist, it is initialized to 0 and then incremented by 1, which returns False if the key is not stored as an integer value. The key new value was successfully returned. $ret = $redis->incr (' age ');//Adds the specified increment value to the numeric value stored by the specified key. $ret = $redis->incrby (' age ', 10);//Adds the specified floating-point increment to the numeric value stored by the specified key. $ret = $redis->incrbyfloat (' age ', 1.5);//Subtract one of the numeric values stored by the specified key. $ret = $redis->decr (' age ');//The numeric value stored by the specified key minus the specified decrement value. $ret = $redis->decrby (' age ', 10);//Append the value to the end of the original value for the specified key, or relative to the set () function if the key does not exist. $ret = $redis->append (' mystr ', ' haha ');

three, hash operation

Assigns a value to a field in a hash table. Successful return 1, failure returns 0. If the hash table does not exist, it will first create the table and then assign the value, if the field already exists will overwrite the old value. $ret = $redis->hset (' user ', ' realname ', ' JETWU ');//Gets the value of the specified field in the hash table. Returns false if the hash table does not exist. $ret = $redis->hget (' user ', ' realname ');//To see if a field of the hash table exists, return true, or false otherwise. $ret = $redis->hexists (' user ', ' realname ');//Deleting a field from a hash table does not support deleting multiple fields. Successfully returns 1, otherwise 0 is returned. $ret = $redis->hdel (' user ', ' realname ');//Set multiple field values for a hash table at the same time. Successful return True. $ret = $redis->hmset (' user ', [' name ' = ' jet ', ' age ' = 18]);//Get multiple field values for a hash table. The field value that does not exist is false. $ret = $redis->hmget (' user ', [' name ', ' age ']);//Gets the fields and values of a hash table. $ret = $redis->hgetall (' user ');//Get all the field names of a hash table. Returns an empty array when the hash table does not exist, and returns False if key is not a hash table. $ret = $redis->hkeys (' user ');//Get all field values for a hash table. $ret = $redis->hvals (' user ');//Assign a value to a field that does not exist in the hash table. If the hash table does not exist, it is created first, and if the field already exists, no action is taken. The setting returns true successfully, otherwise false is returned. $ret = $redis->hsetnx (' user ', ' realname ', ' JETWU ');//Gets the number of fields for a hash table. Returns 0 if the hash table does not exist, or false if key is not a hash table. $ret = $redis->hlen (' user ');//For the specified field in the hash table plus the specified increment value, if the increment value is negative is equivalent to the subtraction operation. If the hash table does not exist, it is created first, if the field does not exist the first initialization value is 0 and then the operation, if the field value is a wordThe string returns FALSE. Set the new value of the field to return successfully. $ret = $redis->hincrby (' User ', ' age ', 10);//For the specified field in the hash table plus the specified floating-point increment value. $ret = $redis->hincrby (' User ', ' age ', 1.5);

four, list operation

Inserts a value from the list header. $ret = $redis->lpush (' City ', ' Guangzhou ');//Insert a value from the tail of the list. $ret = $redis->rpush (' City ', ' Guangzhou ');//Gets the element in the specified interval of the list. 0 represents the first element of the list, 1 represents the last element, and 2 represents the second-to-penultimate element. $ret = $redis->lrange (' City ', 0,-1);//view queue all elements//insert one into the header of an existing list, the operation is invalid when the list does not exist. $ret = $redis->lpushx (' City ', ' Hangzhou ');//Insert one or more values into the tail of the list that already exists, and the operation is invalid if the list does not exist. $ret = $redis->rpushx (' City ', ' Hangzhou ');//Remove and return the first element of the list, or false if key does not exist or is not a list. $ret = $redis->lpop (' city ');//Remove and return the last element of the list, or false if key does not exist or is not a list. $ret = $redis->rpop (' city ');//Remove and get the first element of the list. If there are no elements in the list, the list is blocked until the wait time out or the popup element is found. Parameters: Key, timeout time (in seconds)//return value: [0=>key,1=>value], timeout return [] $ret = $redis->blpop (' City ', 10);//Remove and get the last element of the list. If there are no elements in the list, the list is blocked until the wait time out or the popup element is found. Parameters: Key, timeout time (in seconds)//return value: [0=>key,1=>value], timeout return [] $ret = $redis->brpop (' City ', 10);//Remove the last element in the list, Insert it into another list header and return this element. Returns False if no element is in the source list. $ret = $redis->rpoplpush (' City ', ' city2 ');//Remove the last element in the list, insert it into another list header, and return this element. If there are no elements in the list, the list is blocked until the wait time out or the popup element is found. Parameters: Source list, target list, time-out (in seconds)//timeout return false$ret = $redis->brpopLpush (' City ', ' City2 ', 10);//Returns the length of the list. $ret = $redis->llen (' city ');//Gets the element in the list by index. Returns False if the index is outside the list range. $ret = $redis->lindex (' city ', 0);//sets the value of an element in the list by index. Returns False if the index is out of range or if an LSet operation is performed on an empty list. $ret = $redis->lset (' City ', 2, ' Changsha ');//Insert element before or after the specified element in the list. If the specified element is not in the list, or if the list does not exist, no action is taken. Parameters: List Key,redis::after or Redis::before, datum element, insert element//Return value: Insert successfully returns the number of list elements after insertion, if the datum element does not exist return-1, if key does not exist return 0, if key is not the list returns false. $ret = $redis->linsert (' City ', Redis::after, ' Changsha ', ' Nanjing ');//The element in the list that is equal to the parameter value is removed based on the value of the third parameter count. Count > 0: Searches from the header to the end of the table, removing elements equal to value, count. Count < 0: Searches the header starting at the end of the table, removing the element equal to value, and the number is the absolute value of count. Count = 0: Removes all values that are equal to value in the table. Returns the actual number of deleted elements $ret = $redis->lrem (' City ', ' Guangzhou ',-2);//Trim a list, keep only the elements of the specified interval, and delete the other elements. Successful return True. $ret = $redis->ltrim (' City ', 1, 4);

five, set operation

Adds an element to the collection, and the element that already exists in the collection is ignored. If the collection does not exist, it is created first, if the key is not a collection type then returns FALSE, if the element already exists returns 0, the insert successfully returns 1. $ret = $redis->sadd (' MySet ', ' hello ');//Returns all members of the collection. $ret = $redis->smembers (' MySet ');//Determines whether the specified element is a member of the specified collection, returns True, or false otherwise. $ret = $redis->sismember (' MySet ', ' hello ');//Returns the number of elements in the collection. $ret = $redis->scard (' MySet ');//Remove and return a random element in the collection. $ret = $redis->spop (' MySet ');//returns one or more random member elements in the collection, the number of elements returned and the situation is determined by the second parameter count of the function://If Count is positive and less than the set cardinality, The command returns an array of Count elements, and the elements in the array are different. If count is greater than or equal to the collection cardinality, then the entire collection is returned. If count is a negative number, the command returns an array in which the elements in the array may recur multiple times, and the length of the array is the absolute value of count. $ret = $redis->srandmember (' MySet ', 2);//Removes an element specified in the collection, ignoring the element that does not exist. Delete successfully returns 1, otherwise 0 is returned. $ret = $redis->srem (' MySet ', ' hello ');//iterate over the elements in the collection. Parameters: Key, iterator variable, matching pattern, number of elements returned each time (default is 10) $ret = $redis->sscan (' MySet ', $it, ' A * ', 5);//move the specified member from one source collection to a destination collection. Returns False if the source collection does not exist or does not contain the specified element. Parameters: Source collection, target collection, moving element $ret = $redis->smove (' MySet ', ' Myset2 ', ' aaa ');//Returns the difference between all given sets, the nonexistent collection being considered an empty set. $ret = $redis->sdiff (' MySet ', ' Myset2 ', ' myset3 ');//stores the difference between all given sets in the specified destination collection. Overwrite the destination collection if it already exists. Returns the number of difference set elements. Parameters: FirstThe parameter is the target collection, which stores the difference set. $ret = $redis->sdiffstore (' Myset3 ', ' myset ', ' Myset2 ');//returns the intersection of all given sets, which is considered an empty set. $ret = $redis->sinter (' MySet ', ' Myset2 ', ' myset3 ');//stores the intersection of all given sets in the specified destination collection. Overwrite the destination collection if it already exists. Returns the number of intersection elements. Parameters: The first parameter is the target collection, which stores the intersection. $ret = $redis->sinterstore (' Myset4 ', ' myset ', ' Myset2 ', ' myset3 ');//Returns the set of all the given sets, and the nonexistent collection is considered an empty set. $ret = $redis->sunion (' MySet ', ' Myset2 ', ' myset3 ');//stores the set of all given collections in the specified destination collection. Overwrite the destination collection if it already exists. Returns the number of SET elements. Parameters: The first parameter is the target collection, which stores the set. $ret = $redis->sunionstore (' Myset4 ', ' myset ', ' Myset2 ', ' myset3 ');

VI, zset operation

Adds one or more member elements and their sub-values to an ordered set. If a member is already a member of an ordered set, the member's fractional value is updated and the member element is re-inserted to ensure that the member is in the correct position. The fractional value can be an integer value or a double-precision floating-point number. $ret = $redis->zadd (' scores ', 98, ' 中文版 ', ' n ', ' physics ');//Returns the members of the specified interval in an ordered set. The members are sorted incrementally by fractional value, and the same values are sorted by dictionary order. Parameter: The fourth parameter indicates whether to return the fractional value of each element, false by default. $ret = $redis->zrange (' scores ', 0,-1, true);//View all members of the Zset and their respective fractional values//return members within the specified interval in an ordered set. The members are sorted in descending order of fractional values, and the same values are sorted in reverse order by dictionary order. $ret = $redis->zreverserange (' scores ', 0,-1, true);//Returns a list of members of a specified fractional interval in an ordered set, sorted by fractional value, and sorted by dictionary order with the same number of points. Closed intervals are used by default. $ret = $redis->zrangebyscore (' scores ', [' withscores ' =>true]);//Returns a list of members of the specified fractional interval in an ordered set, descending by fractional value, The same score values are sorted in reverse order of the dictionary order. Note that the interval represents the time when the large value is in front, the decimal value is behind, and the closed interval is used by default. $ret = $redis->zrevrangebyscore (' scores ', [' withscores ' =>true]);//iterate over elements in an ordered set. return value: [element name = + Value,,..] $ret = $redis->zscan (' scores ', $it, ', 10);//Returns the number of elements in the specified ordered set. $ret = $redis->zcard (' scores ');//Returns the number of members of the specified fractional interval in an ordered set. $ret = $redis->zcount (' scores ', 90, 100);//returns the fractional value of the specified member in an ordered set. Returns False if the member does not exist. $ret = $redis->zscore (' scores ', ' math ');//Returns the rank of the specified member in an ordered set, sorted by the fractional value increment. The minimum score value is 0. $ret = $redis->zrank (' scores ', ' chemistry ');//Returns the rank of the specified member in an ordered set, descending by fractional value. The highest score value is 0. $ret = $redis->zrevrank (' scores ', ' chemistry ');//Remove one or more members in an ordered set, ignoring non-existent members. Returns the number of deleted elements. $ret = $redis->zrem (' scores ', ' chemistry ', ' 中文版 ');//Remove all members of the specified rank range in an ordered set. $ret = $redis->zremrangebyrank (' scores ', 0, 2);//Remove all members of the ordered set that specify a range of values. $ret = $redis->zremrangebyscore (' scores ', 80, 90);//increases the specified increment value for the fractional value of the specified member in an ordered set. If a negative number is to do subtraction, if the ordered set does not exist, it is created first, if there is no corresponding member of the ordered set is added first, and then the operation. $ret = $redis->zincrby (' scores ', 2, ' Chinese ');//computes the intersection of a given one or more ordered sets and stores it in an ordered set of purposes. The fractional value of a member in the result set is the sum of the values of that member under all given sets. $ret = $redis->zinterstore (' Zset3 ', ' Zset2 ', ' zset1 ');//Calculates the set of a given one or more ordered sets and stores it in an ordered set of purposes. The fractional value of a member in the result set is the sum of the values of that member under all given sets. $ret = $redis->zunionstore (' Zset3 ', ' Zset2 ', ' zset1 ');

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP prototype design pattern use case study

PHP-FPM Service startup script steps

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.