当前位置:网站首页>Learn more about redis' eight data types and application scenario analysis
Learn more about redis' eight data types and application scenario analysis
2022-06-24 22:45:00 【Java enthusiast】
Introduce
Redis There are several types of data ?( Note that the data type is not a data structure )
There are eight kinds altogether ,String、Hash、Set、List、Zset、Hyperloglog、Geo、Streams.
1、 Why put data in memory ?
- Memory is faster ,10W QPS
- Reduce computing time , Reduce database pressure
2、 If you use the data structure of memory as a cache , Why not HashMap perhaps Memcached?
- Richer data types
- Support for multiple programming languages
- Rich in functions : Persistence mechanism 、 Memory retirement strategy 、 Business 、 Publish subscribe 、pipeline、LUA
- Support clusters 、 Distributed
3、Memcached and redis What's the difference ?
Memcached Can only store KV、 There is no persistence mechanism 、 Master slave replication is not supported 、 It's multi-threaded .
Reids The default is 16 Databases (0-15), Can be found in the configuration file redis.conf Revision in China .
databases 16Redis Key The maximum length limit is 512M, Value limits differ , Some are limited by length , Some are limited by number .
String
Introduce
The most basic and commonly used data type is String.get and set The order is String Operation command of ,Redis The string is called Binary safe string (Binary-safe strings).
String There are three types of storage ,INT( Integers )、float( Single-precision floating-point )、string( character string ).
Operation command
Here are some of its operating commands
# Store value ( If to the same key set Multiple times will directly overwrite the old value )
set jack 2673
# Value
get jack
# Look at all the keys
keys *
# Get the total number of keys ( Large amount of production environment data , Use with caution )
dbsize
# Check if the key exists
exists jack
# Delete key
del jack tonny
# Rename key
rename jack tonny
# View type
type jack
# Get the specified range of characters
getrange jack 0 1
# Get value length
strlen jack
# String append content
append jack good
# Set multiple values ( The batch operation , Atomicity )
mset jack 2673 tonny 2674
# Get multiple values
mget jack tonny
# Set the value , If key There is , No success
setnx jack shuaige
# Based on this, distributed locking is realized
set key value [expiration EX seconds|PX milliseconds][NX|XX]
# ( Integers ) Value increment ( If the value does not exist, you will get 1)
incr jack
incrby jack 100
# ( Integers ) The value is decreasing
decr jack
decrby jack 100
# Floating point increments
set mf 2.6
incrbyfloat mf 7.3Application scenarios
1、 cache
String type , This is the most commonly used , You can cache some hot data , For example, home news , It can significantly improve the access speed of hot data , At the same time reduce DB pressure .
2、 Distributed data sharing
String type , because Redis It's a distributed, stand-alone service , It can be shared among multiple applications .
for example : Distributed Session
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>3、 Distributed lock
Reference resources 《 This paper introduces several common writing methods of distributed lock 》
4、 overall situation ID
INT type ,INCRBY, Using atomicity
incrby userid 1000( The scenario of dividing tables and databases , Take one piece at a time )
5、 Counter
INT type ,INCR Method
for example : The amount of reading , Microblog likes , Allow a certain delay , Write... First Redis Then synchronize to the database regularly
6、 Current limiting
INT type ,INCR Method
With the visitor's IP And other information as key, Add one count at a time , If it exceeds the number of times, it will return false.
Hash
Introduce
Now there's one teacher surface

Suppose we still pass String Type storage , When storing, you should put Teacher Serialize entity classes , And then as a value Store only ; At the time of revision , Also need to put value Take it out first and deserialize it , For example, change the age to 21 year , And then serialize , Save it again , It's very complicated , Increased overhead .
We need to get it separately 、 Modify a value , Then we can go through key In a hierarchical way , The following table :

But this way key It's going to be a lot ,key It's a long time , Occupancy space , Is there a better way , That's when we use our Hash type , The following two tables show :


This also facilitates centralized management , The granularity of the partition is different , You can follow the actual scene ,key The expiration time of , Flexibility considers which storage method to choose .
Hash Used to store multiple unordered key value pairs , Maximum storage 2^32-1(40 Million or so ).
advantage :
- Aggregate all related values into one Key in , Save memory space
- Use only one Key, Reduce Key Conflict
- When you need to get values in batches , Just use one command , Reduce memory /IO/CPU Consumption of
shortcoming :
- Field Expiration time cannot be set separately
- We need to consider the distribution of data volume (field A lot of times , Cannot distribute to multiple nodes )
Operation command
# Set up 、 Batch setting value
hset h1 f 6
hset h1 e 5
hmset h1 a 1 b 2 c 3 d 4
# Value
hget h1 a
# Batch values
hmget h1 a b c d
# Get all field
hkeys h1
# Get all field Value
hvals h1
# Return to the hash table , All fields and values
hgetall h1
# Delete field
hdel h1 a
# Get the number of fields in the hash table
hlen h1Application scenarios
String What can be done ,Hash You can do it. . Add another scene , The shopping cart :

key: user id;field: goods id;value: The number ;
+1:hincr;
-1:hdecr;
Delete :hincrby key field -1;
Future generations :hgetall;
Number of goods :hlen;
List
Introduce
Store ordered strings ( From left to right ), Elements can be repeated , Maximum storage 2^32-1(40 Million or so ).
Let's show how to enter the queue by drawing a picture , Outgoing queue

Operation command
# Push left
lpush queue a
lpush queue b c
# Push right
rpush queue d e
# The left removes and returns the first element of the list
lpop queue
# The right removes and returns the first element of the list
rpop queue
# Get the elements in the list by index
lindex queue 0
# Returns the elements in the specified interval in the list
lrange queue 0 -1Application scenarios
1、 list
For example, the user's message list 、 List of announcements on the website 、 Activity list 、 List of blog posts 、 Comment list, etc , adopt LRANGE Take out a page , Show in order .

2、 queue / Stack
List It can also be used as a queue in a distributed environment / Stack usage .
queue : fifo ,rpush and blpop
Stack : First in, then out ,rpush and brpop
Here are two blocked pop-up operations :blpop/brpop, Timeout can be set ( Company : second ).
blpop:blpop key1 timeout, Move out and get the first element of the list , If there are no elements in the list, it will block the list until the wait timeout or pop-up elements are found .
brpop:brpop key1 timeout, Move out and get the last element of the list , If there are no elements in the list, it will block the list until the wait timeout or pop-up elements are found .
Set
Introduce
Set Storage String Unordered collection of type , Maximum storage 2^32-1(40 Million or so ).
As shown in the figure below :

Operation command
# Add one or more elements
sadd myset a b c d e f g
# Get all the elements
smembers myset
# Number of statistical elements
scard myset
# Get an element at random
srandmember myset
# Pop an element at random
spop myset
# Remove one or more elements
srem myset d e f
# See if the element exists
sismember myset a
# Get the difference set
sdiff set1 set2
# Get intersection
sinter set1 set2
# Get Union
sunion set1 set2Application scenarios
1、 Luck draw
Random access to elements :spop myset
2、 give the thumbs-up 、 Sign in 、 Clock in

Let's take Weibo as an example , Suppose this microblog ID yes t1001, user ID yes u6001,
use dianzan:t1001 To maintain the t1001 All the like users of this microblog .
I like this microblog :sadd dianzan:t1001 u6001
Cancel likes :srem dianzan:t1001 u6001
Do you like it :sismember dianzan:t1001 u6001
All users who like :smembers dianzan:t1001
Number of likes :scard dianzan:t1001
Much simpler than relational databases .
3、 Product label
use tags:i8001 To maintain all labels on the product .

sadd tags:i8001 The picture is clear and delicate
sadd tags:i8001 True color clear screen
sadd tags:i8001 It's extremely smooth
4、 Product selection

Huawei P40 launched , Support national brands , Add to each label .
sadd brand:huawei p40
sadd os:android p40
sadd screensize:6.0-6.24 p40
Screening when buying , The brand is Huawei , The operating system is Android , The screen size is 6.0-6.24 Between , intersect :
sinter brand:huawei os:android screensize:6.0-6.24
ZSet
Introduce
sorted set Store ordered elements . Every element has a score, according to score Sort from small to large .score Phase at the same time , according to key Of ASCII Sort code .

Operation command
# Additive elements
zadd myzset 10 java 20 php 30 ruby 40 cpp 50 python
# Get all the elements
zrange myset 0 -1 withscores
zrevrange myzset 0 -1 withscores
# Get the element according to the score interval
zrangebyscore myzset 20 30
# Remove elements ( It can also be based on score rank Delete )
zrem myzset php cpp
# Number of statistical elements
zcard myzset
# The score increases
zincrby myzset 5 python
# According to the score min and max Number of Statistics
zcount myzset 20 60
# obtain python ranking
zrank myzset python
# Get element scores
zscore myzset python
Application scenarios
Ranking List

It's today 2021 year 5 month 23 Number , To build a key by hotSearch:20210523 Of zset.
The news of Uncle sheep herding id yes n1234, Every click :zincrby hotSearch:20210523 1 n1234
Get the top 10 hot search rankings :zrevrange hotSearch:20210523 0 10 withscores
Yuanlao is an unparalleled scholar , All the way walk good , The children of the Chinese nation will never forget you !
BitMaps
Introduce
BitMaps Is to define the position operation on the string type , One byte by 8 Binary bits make up . As shown in the figure below :

m Of ASCII Code is 109, The corresponding binary data is 0110 1101
Operation command
# Set string key by k1,value by mic
set k1 mic
# take k1 Seventh place in , The result is 0
getbit k1 6
# take k1 The eighth place is 0, At this time ASCII Code is 108, The corresponding letter is l
setbit k1 7 0
# So the value is lic
get k1
# In statistical binary 1 The number of , Is the total 12 individual
bitcount k1
# Get the first one 1 perhaps 0 The location of
bitpos k1 1
bitpos k1 1Application scenarios
- BITOP AND destkey key [key ...], To one or more key Seek logic and , And save the result to destkey.
- BITOP OR destkey key [key ...], To one or more key Seek logic or , And save the result to destkey.
- BITOP XOR destkey key [key ...], To one or more key Seek logical XOR , And save the result to destkey.
- BITOP NOT destkey key, For given key Logic is not , And save the result to destkey.
Online users for seven consecutive days
setbit firstday 0 1 // Set the first day uid yes 0 User login
setbit firstday 1 0 // Set the first day uid yes 1 The user of is not logged in
setbit firstday 2 1 // Set the first day uid yes 2 User login
...
setbit secondday 0 0 // Set the next day uid yes 0 The user of is not logged in
setbit secondday 1 1 // Set the next day uid yes 1 User login
setbit secondday 2 1 // Set the next day uid yes 2 User login
... // And so on So in the calculation, online users for seven consecutive days are :
BITOP AND 7_both_online_users firstday secondday thirdday fourthday fifthday sixthday seventhday
You can also apply access statistics 、 Online user statistics and so on .
Hyperloglog
Hyperloglog Provides an imprecise cardinality statistical method , Used to count the number of non repeating elements in a collection , For example, Statistics website UV, Or applied daily life 、 Monthly living , There is a certain error .
stay Redis Implemented in Hyperloglog, It only needs 12k Memory can count 2^64 Data .
public static void main(String[] args) {
Jedis jedis = new Jedis("39.103.144.86", 6379);
float size = 100000;
for (int i = 0; i < size; i++) {
jedis.pfadd("hll", "hll-" + i);
}
long total = jedis.pfcount("hll");
System.out.println(String.format(" Number of Statistics : %s", total));
System.out.println(String.format(" Accuracy rate : %s", (total / size)));
System.out.println(String.format(" Error rate : %s", 1 - (total / size)));
jedis.close();
} The source code in :
com/xhj/jedis/HyperLogLogTest.java
Geo
Now there is a need , Get the radius 1 Stores within km , Then we should save the longitude and latitude of the store , If there is a database , A field stores longitude , A field is stored in a dimension , It is complicated to calculate the distance . Now let's go through Redis Of Geo Storage is very convenient .
# Save latitude and longitude
geoadd location 121.445 31.213 shanghai
# Take latitude and longitude
geopos location shanghai
Address location information can be added 、 Get address location information 、 Calculate the distance between two positions 、 Get the geographic location collection within the specified range, and so on . The source code in :
com/xhj/jedis/GeoTest.java
public static void main(String[] args) {
Jedis jedis = new Jedis("39.103.144.86", 6379);
Map<String, GeoCoordinate> geoMap = new HashMap<>();
GeoCoordinate coordinate = new GeoCoordinate(121.445, 31.213);
geoMap.put("shanghai", coordinate);
jedis.geoadd("positions", geoMap);
System.out.println(jedis.geopos("positions", "shanghai"));
jedis.close();
}
Streams
5.0 The type of data launched . Persistent message queue supporting multicast , It is used to implement the publish and subscribe function , Learn from it Kafka The design of the .
Summary of application scenarios
- cache — Speed up access to hot data
- Shared data — The problem of data storage and sharing
- overall situation ID— Distributed global ID The generation scheme of ( Sub database and sub table )
- Distributed lock — Atomic operation guarantee of data sharing between processes
- Online user statistics and counting
- queue 、 Stack — Processes across queues / Stack
- Message queue — Asynchronous decoupling message mechanism
- Service registration and discovery —RPC Service coordination center of communication mechanism (Dubbo Support Redis)
- The shopping cart
- Sina user message timeline
- Draw logic ( gift 、 forward )
- give the thumbs-up 、 Sign in 、 Clock in
- Product label
- user ( goods ) Focus on ( recommend ) Model
- E-commerce product screening
- Ranking List
Reproduced in :
https://juejin.cn/post/6965870956103335949#heading-0
边栏推荐
- Use of selector for NiO multiplexing
- YGG recent game partners list
- [ingénierie logicielle] points clés à la fin de la période
- Pinduoduo updates the merchant live broadcast service agreement and strictly punishes the illegal merchants
- Learn more about the practical application of sentinel
- Data communication foundation - Ethernet port mirroring and link aggregation
- 重磅!法大大上榜“专精特新”企业
- Unable to use the bean introduced into the jar package
- O (n) complexity hand tear sorting interview questions | an article will help you understand counting sorting
- Code farmers should also understand the IPv4 subnet division of point networks
猜你喜欢

Selection and comparison of message oriented middleware MQ
How to solve the problem that the computer suddenly can't connect to WiFi

Seven principles of software design

Wechat side: what is consistent hash? In what scenario? What problems have been solved?

In the era of full programming, should I give up this road?

Learning bit segment (1)

NIO多路复用之Selector的使用

High level application of SQL statements in MySQL database (II)

Analyze the implementation process of oauth2 distributed authentication and authorization based on the source code

NIO 零拷贝
随机推荐
Can AI chat robots replace manual customer service?
1. fully explain the basic principles of IPSec
Database transaction Transanction
Docker installs redis-5.0.12. Detailed steps
Dynamic memory management (1)
Idea global search replace shortcut key
NiO, bio, AIO
Technology inventory: Technology Evolution and Future Trend Outlook of cloud native Middleware
String exercise summary 2
2022-06-16 工作记录--JS-判断字符串型数字有几位 + 判断数值型数字有几位 + 限制文本长度(最多展示n个字,超出...)
Creating files, recursively creating directories
Wechat side: what is consistent hash? In what scenario? What problems have been solved?
Data center basic network platform
Learn more about the practical application of sentinel
软件设计的七大原则
[ingénierie logicielle] points clés à la fin de la période
Docker installs MySQL 8.0. Detailed steps
开发规范~参数校验异常、异常返回提示切面
面试害怕被问MySQL相关问题 ?这份三万字精华总结 + 面试100 问,吊打面试官完全够了
DX 的 HLSL 和 GL 的 GLSL的 矩阵构建的行列区别