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

Example introduction of hash type and zset type data in php operation redis

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

The main content of this article is to explain "the example introduction of hash type and zset type data in php operation redis", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "the example introduction of hash type and zset type data in php operation redis".

1,hset

Description: set the value of field field in the hash table key to value. If key does not exist, a new hash table is created and HSET is performed. If the domain field already exists in the hash table, the old value will be overwritten.

Parameter: key field value

Return value: if field is a new field in the hash table and the value is set successfully, 1 is returned. Returns 0 if the field field already exists in the hash table and the old value has been overwritten by the new value.

2,hsetnx

Description: set the value of the field field in the hash table key to value if and only if the domain field does not exist. If the domain field already exists, the operation is invalid. If key does not exist, a new hash table is created and the HSETNX command is executed.

Parameter: key field value

Return value: 1 is returned if the setting is successful. Returns 0 if the given domain already exists and no operation is performed.

3,hget

Description: returns the value of the given field field in the hash table key.

Parameter: key field

Return value: the value of the given field. Returns nil when the given domain does not exist or the given key does not exist.

4,hmset

Description: multiple field-value pairs are set to the hash table key at the same time. This command overwrites fields that already exist in the hash table. If key does not exist, an empty hash table is created and the HMSET operation is performed.

Parameter: key field value [field value...]

Return value: if the command executes successfully, OK is returned. An error is returned when key is not a hash type.

5,hmget

Description: returns the values of one or more given fields in the hash table key. If the given field does not exist in the hash table, a Nil value is returned. Because a non-existent key is treated as an empty hash table, a HMGET operation on a non-existent key returns a table with only Nil values.

Parameter: key field [field...]

Return value: a table containing the associated values of multiple given fields in the same order as the request order for the parameters of the given field.

6,hgetall

Description: returns all fields and values in the hash table key. In the return value, each domain name (field name) is followed by the domain value (value), so the length of the return value is twice the size of the hash table.

Parameter: key

Return value: returns the values of the fields and fields of the hash table as a list. If key does not exist, an empty list is returned.

7,hdel

Description: delete one or more specified fields in the hash table key, and fields that do not exist will be ignored.

Parameter: key field [field...]

Return value: the number of fields that were successfully removed, excluding ignored fields.

8,hlen

Description: returns the number of fields in the hash table key.

Parameter: key

Return value: the number of fields in the hash table. Returns 0 when key does not exist.

9,hexists

Description: check whether the given domain field exists in the hash table key.

Parameter: key field

Return value: if the hash table contains a given field, return 1. Returns 0 if the hash table does not contain the given field, or key does not exist.

10,hincrby

Description: add incremental increment to the value of the field field in the hash table key. The increment can also be negative, which is equivalent to a subtraction of a given field.

Parameter: key field increment

Return value: the value of the field field in the hash table key after the HINCRBY command is executed.

11,hkeys

Description: returns all fields in the hash table key.

Parameter: key

Return value: a table containing all the fields in the hash table. Returns an empty table when key does not exist.

12,hvals

Description: returns all values in the hash table key.

Parameter: key

Return value: a table that contains all the values in the hash table. Returns an empty table when key does not exist.

Code examples of the above 12 methods:

The copy code is as follows:

13,zadd

Description:

Add one or more elements, and if the element already exists, update its soap value

Although the ordered collection is orderly, it is also a collection and cannot repeat elements. Adding repeating elements will only

Update the score value of the original element

Parameters:

Key

Score: double

Value: string

Return value: 1 or 0

14,zrange

Description: gets the sorted elements within a specific range, with 0 for the first element, 1 for the second, and so on. -1 for the last one,-2 for the penultimate one.

Parameters:

Key

Start: long

End: long

Withscores: bool = false

Return value: array

15,zdelete, zrem

Description: removes the specified member from the ordered collection.

Parameters:

Key

Member

Return value: 1 or 0

16,zrevrange

Description: returns all elements of the specified interval in the ordered set corresponding to key. These elements are arranged in the order of score from highest to lowest. For elements with the same score, they will be arranged in descending dictionary order. This command is similar to ZRANGE, except that the order of elements in this command is different from that of the former.

Parameters:

Key

Start: long

End: long

Withscores: bool = false

Return value: array

17,zrangebyscore, zrevrangebyscore

Description: returns all elements in the ordered collection corresponding to key whose score is between min and max (elements with a hash score equal to min or max). The elements are arranged in the order of score from lowest to highest. If the elements have the same score, they are arranged in dictionary order.

The optional option LIMIT can be used to get a range of matching elements. If the offset value is large, the ordered collection needs to traverse before getting the element to be returned, thus increasing the time complexity of O (N). The optional option WITHSCORES, which has been available since Redis version 2.0, allows you to return the element's score as well as the element.

Parameters:

Key

Start: string

End: string

Options: array

Return value: array

18,zcount

Description: returns the number of elements in the ordered collection corresponding to key mediating between min and max.

Parameters:

Key

Start: string

End: string

Return value: array length

19,zremrangebyscore, zreleterangebyscore

Description: removes all elements in the ordered collection corresponding to key where scroe is between min and max (including endpoints). Starting with version 2.1.6, interval endpoints min and max can be excluded, which is the same as the syntax of ZRANGEBYSCORE.

Parameters:

Key

Start: double or "+ inf" or "- inf" string

End: double or "+ inf" or "- inf" string

Return value: number of deleted elements

20,zremrangebyrank, zdeleterangebyrank

Description: removes all elements in the ordered collection corresponding to key that have rank values between start and stop. Both start and stop start at 0, and both can be negative. When the index value is negative, it indicates that the offset value starts from the element with the highest score value in the ordered set. For example:-1 represents the element with the highest score,-2 represents the element with the second highest score, and so on.

Parameters:

Key

Start: LONG

End: LONG

Return value: number of deleted elements

21,zsize, zcard

Description: returns the number of elements stored in the ordered collection corresponding to key.

Parameter: key

Return value: number of elements

22,zscore

Description: returns the score value of member in the ordered collection corresponding to key. If member does not exist in the ordered collection, null will be returned.

Parameter: key member

23,zrank, zrevrank

Description: returns the index value of the member element in the ordered collection corresponding to key, arranged by score from lowest to highest. The rank value (or index) starts at 0, which means that the element with the lowest score value has a rank value of 0. Using ZREVRANK, you can get the rank (or index) of elements arranged from high to low.

Parameter: key member

Return value: number

24,zincrby

Add the scroe of the member element in the ordered set corresponding to key to increment. If the specified member does not exist, the element is added and its initial value of score is increment. If key does not exist, a new ordered list containing the unique element member will be created. If the value corresponding to key is not an ordered list, an error will occur. The value of the specified score should be a string that can be converted to a numeric value and receive double-precision floating-point numbers. At the same time, you can also provide a negative value, which will reduce the value of score.

Parameter: key value member

Return value: character data

25,zunion

Description: numkeys ordered sets corresponding to keys are calculated and the results are stored in destination

Parameter: keyOutput arrayZSetKeys arrayWeights aggregateFunction

Return value: Union array

26,zinter

Description: numkeys ordered sets corresponding to keys calculate the intersection and store the results in destination

Parameter: keyOutput arrayZSetKeys arrayWeights aggregateFunction

Return value: intersection array

Code example for 13-26:

The copy code is as follows:

$redis = new redis ()

$redis- > connect ('192.168.1.108 dollars, 6379)

$redis- > delete ('test')

$redis- > zadd ('test', 1,' val1')

$redis- > zadd ('test', 0,' val2')

$redis- > zadd ('test', 3,' val3')

Print_r ($redis- > zrange ('test', 0,-1)); / / result: Array ([0] = > val2 [1] = > val1 [2] = > val3)

$redis- > zdelete ('test',' val2')

Print_r ($redis- > zrange ('test', 0,-1)); / / result: Array ([0] = > val1 [1] = > val3)

$redis- > zadd ('test',4,' val0')

Print_r ($redis- > zrevrange ('test', 0,-1)); / / result: Array ([0] = > val0 [1] = > val3 [2] = > val1)

Print_r ($redis- > zrevrange ('test', 0,-1 Magi true)); / / result: Array ([val0] = > 4 [val3] = > 3 [val1] = > 1)

Echo "

"

$redis- > zadd ('key', 0,' val0')

$redis- > zadd ('key', 2,' val2')

$redis- > zadd ('key', 10,' val10')

Print_r ($redis- > zrangebyscore ('key', 0,3, array (' limit' = > array (1,1), 'withscores' = > TRUE)); / / result: Array ([val2] = > 2)

Print_r ($redis- > zrangebyscore ('key', 0,3, array (' limit' = > array (1,1); / / result: Array ([0] = > val2)

Echo $redis- > zcount ('key', 0,3); / / result: 2

$redis- > zremrangebyscore ('key', 0,3)

Print_r ($redis- > zrange ('key', 0,-1)); / / result: Array ([0] = > val10)

Echo $redis- > zsize ('key'); / / result: 1

$redis- > zadd ('key', 2.5,' aaaa')

Echo $redis- > zscore ('key',' aaaa'); / / result: 2.5

Echo $redis- > zrank ('key',' aaaa'); / / result: 0

Echo $redis- > zrevrank ('key',' aaaa'); / / result: 1

$redis- > delete ('key')

Echo $redis- > zincrby ('key', 2,' aaaa'); / / result: 2

Echo $redis- > zincrby ('key', 1,' aaaa'); / / result: 3

$redis- > delete ('key')

$redis- > delete ('test')

$redis- > zadd ('key', 0,' val0')

$redis- > zadd ('key', 1,' val1')

$redis- > zadd ('key', 4,' val2')

$redis- > zadd ('test', 2,' val2')

$redis- > zadd ('test', 3,' val3')

$redis- > zunion ('k01mm, array (' key', 'test'))

Print_r ($redis- > zrange ('k01jewelry no. 0,-1)); / / result: Array ([0] = > val0 [1] = > val1 [2] = > val3 [3] = > val2)

$redis- > zunion ('k03, array (' key', 'test'), array (5,1))

Print_r ($redis- > zrange ('k03jewelry 0,-1)); / / result: Array ([0] = > val0 [1] = > val3 [2] = > val1 [3] = > val2)

$redis- > zinter ('k02percent, array (' key', 'test'))

Print_r ($redis- > zrange ('k02jewelry 0,-1)); / / result: Array ([0] = > val2)

? >

At this point, I believe you have a deeper understanding of "the example introduction of hash type and zset type data in php operation redis". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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

Development

Wechat

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

12
Report