当前位置:网站首页>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
边栏推荐
- 2022-06-16 work record --js- judge the number of digits in string type digits + judge the number of digits in numeric type digits + limit the text length (display n words at most, exceeding...)
- 【Mongodb】READ_ME_TO_RECOVER_YOUR_DATA,数据库被恶意删除
- NIO 零拷贝
- 电力系统| IEEE论文投稿流程
- High level application of SQL statements in MySQL database (II)
- The usage difference between isempty and isblank is so different that so many people can't answer it
- Nuscenes -- remedies for missing image files or 0-size images encountered during dataset configuration
- 详细了解Redis的八种数据类型及应用场景分析
- ThreadLocal内存泄漏问题
- Online filing process
猜你喜欢

NiO, bio, AIO

How to compare two or more distributions: a summary of methods from visualization to statistical testing
Relationnet++: a representation of fusion of multiple detection targets based on transformer | neurips 2020
![[QT] QT event handling](/img/48/14a5491307fee1c99434d6cb308337.jpg)
[QT] QT event handling

Can AI chat robots replace manual customer service?

机器学习编译入门课程学习笔记第一讲 机器学习编译概述

Seven principles of software design

Environment configuration | vs2017 configuring openmesh source code and environment

Uncover the secret of station B. is it true that programmers wear women's clothes and knock code more efficiently?

Genesis public chain and a group of encryption investors in the United States gathered in consensus 2022
随机推荐
interrupt、interrupted 、isInterrupted 区别
Problem solving - nested lists
Cache control of HTTP
Introduction, installation and use of postman tool
结合源码剖析Oauth2分布式认证与授权的实现流程
Embedded development: tips and tricks -- clean jump from boot loader to application code
ThreadLocal内存泄漏问题
Wechat side: what is consistent hash? In what scenario? What problems have been solved?
机器学习编译入门课程学习笔记第一讲 机器学习编译概述
Leetcode algorithm The first common node of two linked lists
NiO, bio, AIO
In the multi network card environment, the service IP registered by Nacos is incorrect, resulting in inaccessible services
NiO zero copy
Tetris
Firewall working principle and detailed conversation table
See how sparksql supports enterprise data warehouse
使用Aggregated APIServer扩展你的kubernetes API
MySQL + JSON = King fried!!
Use of selector for NiO multiplexing
Data communication foundation - Ethernet port mirroring and link aggregation