Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use redis in PHP

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to use redis in PHP". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. Redis connection and authentication

Connection parameters: ip, port, connection timeout. True is returned if the connection succeeds, otherwise false is returned.

$ret = $redis- > connect ('127.0.0.1, 6379, 30)

Password authentication: true is returned successfully, otherwise false is returned.

$ret = $redis- > auth ('123456'); II. String operation

Set key value: true is returned successfully, otherwise false is returned.

$redis- > set ('cfun',' Welcomeering')

Get key value: the key value of String type is returned successfully, and false is returned if key does not exist or is not a String type

$ret = $redis- > get ('cfun')

Starts with the specified offset of a string stored by a key, replaces it with another specified string, and successfully returns the length of the new string after replacement.

$ret = $redis- > setRange ('cfun', 7,' to Beijingying')

Gets the substring stored in the string in the specified key.

$ret = $redis- > getRange ('cfun', 0,6)

Set the new value and return the old value: if key does not exist, set the value and return false

$ret = $redis- > getSet ('cfun',' hi man')

Set multiple key-value pairs at a time: true is returned successfully.

$ret = $redis- > mset (['name' = >' cfun', 'age' = > 18])

Get more than one key at a time: returns an array of key-value pairs, where the key value that does not exist is false.

$ret = $redis- > mget (['name',' age'])

Sets the value of the specified key and its expiration time in seconds.

Parameters: key name, expiration time, key value. True was returned successfully.

$ret = $redis- > setex ('name', 10,' cfun')

Sets the value and expiration time of the specified key in milliseconds. True was returned successfully.

$ret = $redis- > psetex ('name', 10,' cfun')

The value of key is set only when it does not exist. Key does not exist and a successful setting returns true, otherwise it returns false.

$ret = $redis- > setnx ('name',' boby')

Batch operation of the setnx command. It can be set successfully only if none of the given key exists. As long as one of the key exists, none of the key can be set successfully.

$ret = $redis- > msetnx (['country' = >' China', 'city' = >' HangZhou'])

Gets the length of the string stored by the specified key. Key does not exist and returns 0 and does not return false for the string.

$ret = $redis- > strlen ('name')

Increases the numeric value stored by the specified key by 1. If key does not exist, it will be initialized to 0 and then incremented by 1. If the value stored by key is not an integer, false will be returned. The new value of key was returned successfully.

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

Subtracts the numeric value stored by the specified key by one.

$ret = $redis- > decr ('age')

Subtracts the numeric value stored by the specified key from the specified decrement value.

$ret = $redis- > decrBy ('age', 10)

Appends the value to the end of the original value for the specified key, relative to the set () function if key does not exist.

$ret = $redis- > append ('cfun',' '); III. Hash operation

Assign values to fields in the hash table. Success returns 1 and failure returns 0. If the hash table does not exist, the table will be created first and then the value will be assigned. If the field already exists, the old value will be overwritten.

$ret = $redis- > hSet ('user',' realname', 'cfun')

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

Check to see if a field in the hash table exists. If so, it returns true, otherwise it returns false.

$ret = $redis- > hExists ('user',' realname')

Delete one field from the hash table. Deleting multiple fields is not supported. Returns 1 successfully, otherwise 0.

$ret = $redis- > hDel ('user',' realname')

Set multiple field values for a hash table at the same time. True was returned successfully.

$ret = $redis- > hMset ('user', [' name' = > 'cfun',' age' = > 18])

Get multiple field values for a hash table at the same time. The field value that does not exist is false.

$ret = $redis- > hMget ('user', [' name', 'age'])

Gets all the fields and values of a hash table.

$ret = $redis- > hGetAll ('user')

Gets all the field names of a hash table. An empty array is returned if the hash table does not exist, and false is returned if the key is not a hash table.

$ret = $redis- > hKeys ('user')

Gets all the field values of a hash table.

$ret = $redis- > hVals ('user')

Assign values to fields that do not exist in the hash table. If the hash table does not exist, create it first, and do nothing if the field already exists. True is returned if the setting is successful, otherwise false is returned.

$ret = $redis- > hSetNx ('user',' realname', 'cfun')

Gets the number of fields for a hash table. Returns 0 if the hash table does not exist, and returns false if the key is not a hash table.

$ret = $redis- > hLen ('user')

Adds a specified incremental value to the specified field in the hash table, which is equivalent to a subtraction operation if the increment value is negative. If the hash table does not exist, create it first. If the field does not exist, initialize the value to 0 before performing the operation. If the field value is a string, return false. Set the new value of the returned field successfully.

$ret = $redis- > hIncrBy ('user',' age', 10)

Adds the specified floating-point increment value to the specified field in the hash table.

$ret = $redis- > hIncrBy ('user',' age', 1.5); IV. List operation

Insert a value from the list header.

$ret = $redis- > lPush ('city',' ShangHai')

Insert a value from the tail of the list.

$ret = $redis- > rPush ('city',' ShangHai')

Gets the elements in the specified range of the list. 0 represents the first element of the list,-1 represents the last element, and-2 represents the penultimate element.

$ret = $redis- > lrange ('city', 0jigme *-1)

View all elements of the queue

Inserts an existing list header, and the operation is invalid if the list does not exist.

$ret = $redis- > lPushx ('city',' ShangHai')

Inserts one or more values into the end of an existing list, and the operation is invalid when the list does not exist.

$ret = $redis- > rPushx ('city',' NanJing')

Removes and returns the first element of the list, or returns false if key does not exist or is not a list.

$ret = $redis- > lPop ('city')

Removes and returns the last element of the list, or returns 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 the list has no elements, it blocks the list until the wait times out or until a popup element is found.

Parameter: key, timeout (in seconds)

Return value: [0 = > key,1= > value], timeout returns []

$ret = $redis- > blPop ('city', 10)

Remove and get the last element of the list. If the list has no elements, it blocks the list until the wait times out or until a popup element is found.

Parameter: key, timeout (in seconds)

Return value: [0 = > key,1= > value], timeout returns []

$ret = $redis- > brPop ('city', 10)

Remove the last element from the list, insert it into another list header, and return this element. Returns false if the source list has no elements.

$ret = $redis- > rpoplpush ('city',' city2')

Remove the last element from the list, insert it into another list header, and return this element. If the list has no elements, it blocks the list until the wait times out or until a popup element is found.

Parameters: source list, destination list, timeout (in seconds)

Time-out returns false

$ret = $redis- > brpoplpush ('city',' city2', 10)

Returns the length of the list.

$ret = $redis- > lLen ('city')

Get the elements in the list through the index. Returns false if the index is out of the list range.

$ret = $redis- > lindex ('city', 0)

Sets the value of the element in the list through the index. If the index is out of range, or lset an empty list, false is returned.

$ret = $redis- > lSet ('city', 2,' changsha')

Inserts an element before or after the specified element in the list. If the specified element is not in the list, or the list does not exist, no action is performed.

Parameters: list key,Redis::AFTER or Redis::BEFORE, base element, insert element

Return value: the number of list elements after insertion is returned successfully.-1 is returned if the benchmark element does not exist, 0 is returned if key does not exist, and false is returned if key is not a list.

$ret = $redis- > lInsert ('city', Redis::AFTER,' changsha', 'nanjing')

Removes the element in the list that is equal to the parameter value, based on the value of the third parameter, count.

Count > 0: search from the header to the footer of the table to remove the elements equivalent to value, and the number is count.

Count

< 0 : 从表尾开始向表头搜索,移除与 value 相等的元素,数量为 count 的绝对值。 count = 0 : 移除表中所有与 value 相等的值。 返回实际删除元素个数 $ret = $redis->

Lrem ('city',' ShangHai',-2)

Prune a list so that only the elements in the specified range are retained and all other elements are deleted. True was returned successfully.

$ret = $redis- > ltrim ('city', 1,4); 5. Set operation

Add an element to the collection, while elements that already exist in the collection are ignored. If the collection does not exist, it will be created first. If key is not a collection type, false will be returned. If the element already exists, 0 will be returned. If the insertion is successful, 1 will be returned.

$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, otherwise returns false.

$ret = $redis- > sismember ('myset',' hello')

Returns the number of elements in the collection.

$ret = $redis- > scard ('myset')

Removes and returns a random element in the collection.

$ret = $redis- > sPop ('myset')

Returns one or more random member elements in the collection. The number and condition of the returned elements are 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, each with different elements.

If count is greater than or equal to the cardinality of the collection, the entire collection is returned.

If count is negative, the command returns an array in which elements may be repeated 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 elements that do not exist. 1 is returned for successful deletion, otherwise 0 is returned.

$ret = $redis- > srem ('myset',' hello')

Iterate over the elements in the collection.

Parameter: key, iterator variable, match pattern, number of elements returned at a time (default is 10)

$ret = $redis- > sscan ('myset', $it,' a lifetime, 5)

Moves the specified member from a 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 elements

$ret = $redis- > sMove ('myset',' myset2', 'aaa')

Returns the difference between all given sets, and sets that do not exist are considered empty.

$ret = $redis- > sDiff ('myset',' myset2', 'myset3')

Stores the difference between all given collections in the specified destination collection. Overwrite the destination collection if it already exists. Returns the number of subtraction elements.

Parameter: the first parameter is the target set, which stores the difference set.

$ret = $redis- > sDiffStore ('myset3',' myset', 'myset2')

Returns the intersection of all given collections. Collections that do not exist are considered empty sets.

$ret = $redis- > sInter ('myset',' myset2', 'myset3')

Stores the intersection of all given collections 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 and stores the intersection.

$ret = $redis- > sInterStore ('myset4',' myset', 'myset2',' myset3')

Returns the union of all given collections. Collections that do not exist are considered empty sets.

$ret = $redis- > sUnion ('myset',' myset2', 'myset3')

Stores the union of all given collections in the specified destination collection. Overwrite the destination collection if it already exists. Returns the number of union elements.

Parameters: the first parameter is the target set, which stores the union.

$ret = $redis- > sUnionStore ('myset4',' myset', 'myset2',' myset3'); VI. Zset operation

Adds one or more member elements and their score values to the ordered set. If a member is already a member of an ordered set, update the member's score value and ensure that the member is in the correct position by reinserting the member element. Fractional values can be integer values or double-precision floating-point numbers.

$ret = $redis- > zAdd ('scores', 98,' English', 90, 'physics')

Returns the members in the specified interval in the ordered set. Members are sorted incrementally by score value, and those with the same score value are sorted in lexicographic order.

Parameter: the fourth parameter indicates whether to return the score value of each element, which defaults to false.

$ret = $redis- > zRange ('scores', 0,-1, true)

View all members of Zset and their respective score values

Returns the members in the specified interval in the ordered set. Members are sorted by decreasing scores, and those with the same values are sorted in reverse lexicographic order.

$ret = $redis- > zReverseRange ('scores', 0,-1, true)

Returns the list of members of the specified score range in the ordered set, sorted by the increment of the score value, and the same score value by lexicographic order. A closed interval is used by default.

$ret = $redis- > zRangeByScore ('scores', 90,100, [' withscores'= > true])

Returns a list of members of the specified score range in an ordered set, sorted by decreasing scores, and those with the same values are sorted in reverse order of lexicographic order. Note that when the interval is expressed, the large value comes before the small value, and the closed interval is used by default.

$ret = $redis- > zRevRangeByScore ('scores', 100,90, [' withscores'= > true])

Iterate over the elements in an ordered collection.

Return value: [element name = > fraction 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 in the specified score range in an ordered set.

$ret = $redis- > zCount ('scores', 90100)

Returns the score value of the specified member in an ordered set. Returns false if the member does not exist.

$ret = $redis- > zScore ('scores',' math')

Returns the ranking of the specified members in a sorted set, sorted by increasing scores. Those with the lowest score rank 0.

$ret = $redis- > zRank ('scores',' chemistry')

Returns the ranking of the specified members in an ordered set, sorted by decreasing scores. The person with the highest score is ranked as 0.

$ret = $redis- > zRevRank ('scores',' chemistry')

Removes one or more members from the ordered set, ignoring members that do not exist. Returns the number of elements deleted.

$ret = $redis- > zRem ('scores',' chemistry', 'English')

Removes all members of the specified ranking interval in the ordered set.

$ret = $redis- > zRemRangeByRank ('scores', 0,2)

Removes all members of the specified fraction interval in the ordered set.

$ret = $redis- > zRemRangeByScore ('scores', 80,90)

Increases the specified increment value to the score value of the specified member in the ordered set. Subtract if the number is negative, create it first if the ordered set does not exist, add it first if there is no corresponding member in the ordered set, and then operate at last.

$ret = $redis- > zIncrBy ('scores', 2,' Chinese')

Calculates the intersection of a given ordered set or sets and stores it in a destination ordered set. The score value of a member in the result set is the sum of the score values of that member in all given sets.

$ret = $redis- > zinterstore ('zset3',' zset2', 'zset1')

Calculates the union of a given ordered set or sets and stores it in a destination ordered set. The score value of a member in the result set is the sum of the score values of that member in all given sets.

$ret = $redis- > zunionstore ('zset3',' zset2', 'zset1'); that's all for "how to use redis in PHP". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report