当前位置:网站首页>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 :
Every time you change a property of a user, you need , First deserialize it and then serialize it back . It's expensive . 
user ID data redundancy 
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
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 
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 
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
边栏推荐
- 60、wsgiref手写web框架+jinja2模块初识
- 总结的太好了!终于有人把SQL的各种连接Join都讲明白了
- Visual studio 2022 (vs 2022) cannot read memory
- New redis6 features
- Seaport 以及 ERC-4907 能否成为释放NFT流动性的新途径?| Tokenview
- Csp-2020-6- role authorization
- Can seaport and erc-4907 become new ways to release NFT liquidity| Tokenview
- 5.2 数据库安全
- Stm32f103c8t6 hardware IIC control 4-pin 0.96 inch OLED display
- Deep learning 7 deep feedforward network
猜你喜欢
随机推荐
JS学习笔记09-12:原型对象以及Foreach+tostring及回收站
总结的太好了!终于有人把SQL的各种连接Join都讲明白了
Deep learning 7 deep feedforward network
Visual studio 2022 (vs 2022) cannot read memory
mySQL 2502 2503错误
5g at that time, where will driverless driving go in the future?
5.2 数据库安全
visual studio 2022(VS 2022)无法读取内存的问题
Precautions for MySQL statements
How to convert STR in read list to float
Not so large number of combinations
3D激光SLAM:ALOAM---帧间里程计代码解读
Redis distributed lock
经典通用的Pbootcms花卉网站模板源码,自适应手机端,带后台管理
Ribbon负载均衡服务调用
Redis6新数据类型——HyperLogLog
Super dry! Thoroughly understand golang memory management and garbage collection
Deep learning 7 deep feedforward network 2
网传USDT和USDC要做空?带你一探究竟 | Tokenview
5.1 vulnérabilités et précautions en matière de sécurité

![Paddleserving服务化部署 tensorrt报错, shape of trt subgraph is [-1,-1,768],](/img/15/5dde91261a44fcfeda4d8436bb8559.png)







