当前位置:网站首页>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
边栏推荐
- 如何将读取列表中的str转化为float
- Redis6 新数据类型——Geospatial
- 凭借左程云(左神)的这份 “程序员代码面试指南”我入职了字节
- US pressure surges, tiktok changes global safety director
- Set settings in vscode json
- mySQL 2502 2503错误
- Why does the Fed cut interest rates benefit the digital money market in the long run? 2020-03-05
- Stm32f103c8t6 hardware IIC control 4-pin 0.96 inch OLED display
- New redis6 features
- Demo collection injection
猜你喜欢

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

Talk about distributed locks

Complete square number
![[kernel] character device that drives development and learning](/img/99/2eaed37078c3245be29d82382cfd59.png)
[kernel] character device that drives development and learning

Viewing the technology stack of distributed system from the crash report of station B

最新一代互联网:WEB 3.0

Redis cache avalanche, penetration, breakdown

【Kernel】驱动开发学习之字符设备

Redis介绍
随机推荐
ansible自动化运维详解(四)ansible中playbook的编写使用、执行命令及实例演示
QT related problems encountered when writing code
Can seaport and erc-4907 become new ways to release NFT liquidity| Tokenview
Super dry! Thoroughly understand golang memory management and garbage collection
总结的太好了!终于有人把SQL的各种连接Join都讲明白了
Redis master-slave replication
How did "leek" give money to "sickle"? 2020-03-07
openpyxl跨工作簿复制sheet页
Redis introduction
【Kernel】驱动开发学习之字符设备
Visual studio production environment configuration scheme: slowcheetah
Complete square number
Redis常用数据类型——Redis列表(List)和Redis 集合(Set)
JS学习笔记04-05——构造函数的修改以及使用工厂方法创建
Redis cluster
Redis message subscription
如何在 Jenkins Pipeline 中使用curl 并处理响应结果
Filesourcestrategy, datasourcestrategy and datasourcev2strategy in spark
深度学习之 7 深度前馈网络
5g at that time, where will driverless driving go in the future?