当前位置:网站首页>Redis common data types - hash and ordered set Zset (sorted set)

Redis common data types - hash and ordered set Zset (sorted set)

2022-07-19 08:27:00 Illusory clarity

Redis Hash (Hash)

1. brief introduction

Redis hash It's a Key value pair set .
Redis hash It's a string Type of field and value Mapping table ,hash Ideal for storing objects . similar Java Inside Map<String,Object>.

user ID For searching key, Stored value User object contains name , Age , Birthday and other information , If you use ordinary key/value Structure to store .

There are mainly the following 2 Storage methods :
 Insert picture description here
Every time you change a property of a user, you need , First deserialize it and then serialize it back . It's expensive .
 Insert picture description here
user ID data redundancy
 Insert picture description here
adopt key( user ID) + field( Property tags ) You can operate the corresponding attribute data , No need to store data repeatedly , It also doesn't cause serialization and concurrent modification control problems .

2. Common commands

hset <key><field><value> to <key> In the collection <field> Key assignment <value> ;

hget <key1><field> from <key1> aggregate <field> Take out value ;

hmset <key1><field1><value1><field2><value2>... Batch settings hash Value ;

hexists<key1><field> Look at the hash table key in , Given domain field Whether there is .

hkeys <key> List the hash All of the collection ;

field hvals <key> List the hash All of the collection value;

hincrby <key><field><increment> Hash table key In the domain field Plus the increment 1 -1 ;

hsetnx <key><field><value> Hash table key In the domain field Is set to value , If and only if domain field non-existent .

3. data structure

Hash There are two kinds of data structures corresponding to types :ziplist( Compressed list ),hashtable( Hashtable ). When field-value When the length is short and the number is small , Use ziplist, Otherwise use hashtable.

Redis Ordered set Zset(sorted set)

1. brief introduction

Redis Ordered set zset With the common set set Very similar , Is a collection of strings without repeating elements . The difference is that each member of the ordered set is associated with a score (score), This score (score) Used to sort the members of a set from the lowest score to the highest score . Members of a collection are unique , But the score can be repeated . Because the elements are ordered , So you can also quickly rate (score) Or order (position) To get a range of elements . Accessing intermediate elements of ordered collections is also very fast , So you can use ordered sets as a smart list without duplicate members .

2. Common commands

zadd <key><score1><value1><score2><value2>… Put one or more member Elements and score Value added to ordered set key among .

zrange <key><start><stop> [WITHSCORES] Return to ordered set key in , The subscript is Between the elements belt WITHSCORES, You can return scores and values together to the result set .

zrangebyscore key minmax [withscores] [limit offset count] Return to ordered set key in , all score The value is between min and max Between ( Including equal to min or max ) Members of . Members of the ordered set press score Value increment ( From small to large ) Order .

zrevrangebyscore key maxmin [withscores] [limit offset count] ditto , Change to big to small .

zincrby <key><increment><value> For the elements score Plus the increment .

zrem <key><value> Delete... Under this collection , The element that specifies the value .

zcount <key><min><max> Count the set , The number of elements in the fraction range zrank Returns the rank of the value in the collection , from 0 Start .

3. data structure

SortedSet(zset) yes Redis A very special data structure provided , On the one hand, it is equivalent to Java Of data structure Map<String, Double>, You can give each element value Give a weight score, The other side It is similar to TreeSet, Internal elements are weighted score Sort , You can get the rank of each element , You can also use score To get a list of elements .

zset The bottom layer uses two data structures
(1)hash,hash The role of is to associate elements value And weight score, Guarantee elements value Uniqueness , You can use the element value Find the appropriate score value .
(2) Skip list , The purpose of jump tables is to give elements value Sort , according to score Scope get element list for .

4. Skip list ( Jump watch )

1、 brief introduction

Orderly collection is common in life , For example, ranking students according to their grades , Rank players according to their scores etc. . For the underlying implementation of ordered sets , You can use arrays 、 Balance tree 、 Chain list, etc . Insertion of array elements 、 Delete ; Although the balanced tree or red black tree is efficient, its structure is complex ; The linked list query needs to traverse all, which is inefficient .Redis Using a jump table . The efficiency of jump table is comparable to that of red and black tree , The implementation is much simpler than the red black tree .

2、 example

Compare ordered linked list and jump list , Find out from the linked list 51

(1) Ordered list
 Insert picture description here
The value to find is 51 The elements of , You need to start with the first element and look for 、 Compare to find . A common need 6 Compare it to .

(2) Skip list
 Insert picture description here
From 2 Layer start ,1 Node ratio 51 Small node , Compare back .

21 Node ratio 51 Small node , Continue to compare backward , The back is NULL 了 , So from 21 Node down to the 1 layer .

In the 1 layer ,41 Node ratio 51 Small node , Keep going backwards ,61 Node ratio 51 Large node , So from 41 Down .

In the 0 layer ,51 The node is the node to find , Node found , Co search 4 Time .

It can be seen that the efficiency of jump list is higher than that of ordered linked list

原网站

版权声明
本文为[Illusory clarity]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/200/202207170723512832.html