当前位置:网站首页>Detailed explanation and use of redis data types: key and string types
Detailed explanation and use of redis data types: key and string types
2022-06-12 10:06:00 【Liu Java】
Keep creating , Accelerate growth ! This is my participation 「 Nuggets day new plan · 6 Yuegengwen challenge 」 Of the 11 God , Click to see the event details
In detail Redis Of key and String The underlying principle of data types , And the most basic way to use it .
1 Redis Data type of
Redis It's not an ordinary key-value Storage server , It's actually a data structure server , Supports different types of values , stay Redis The median is not limited to simple strings , It can also contain more complex data structures .
Here are Redis All data structures supported :
- Binary system (Binary-safe) Safe String( character string ).
- List: Sorted by insertion order String A collection of elements , Basically a linked list .
- Set: Unique 、 Unordered String A collection of elements .
- Sorted set: Be similar to Set, But every String Elements are associated with a floating-point value , It's called a score . Elements are always sorted by their scores , Therefore, Sets Different , It can retrieve a series of elements ( for example , Get the top ten scores or the bottom ten scores ).
- Hash: It consists of fields associated with values map, Fields and values are strings , This is related to Ruby or Python Of Hash Very similar .
- Bit array( Or call it bit map):Bitarray It's not a data structure in itself , In fact, it is String, It's just Redis Support the use of special commands can be like (bitarray) Handle string values the same as bit groups , You can set and clear individual bit position , Calculate all settings to 1 Of bit position , Find the first set or unset bit Bits, etc . This makes Redis Of String Can be used to store 、 obtain 、 Statistics and other operations , Very space saving .
- HyperLogLog: This is a probabilistic data structure , Used to quickly estimate the cardinality of a set .
- Stream:Redis 5.0 Version of the new data structure , For implementing persistent message queues .
2 Redis key
Redis key yes String type , It's also binary safe , We can use any binary sequence as a key , From the image “foo” Such a string goes to JPEG The content of the document .
About Redis Of key There are the following rules and recommendations :
- Too long key Not a good choice , Not only because of memory consumption , And look in the data key It also requires many complex key Compare .
- Too short a key value is usually not a good choice , If you want to use ”u:1000:pwd” Instead of ”user:1000:password”, There is nothing wrong with this in itself , But the latter is easier to read , And the space consumption thus increased is relative to key object and value object Small in itself . Of course , No one is stopping you from saving a little space with shorter keys .
- It's best to stick to a naming pattern . for example :”
object-type:id:field” It's a good idea , like this :“user:1000:password”. For multi word field names, you can add a dot , Like this :“comment:1234:reply.to” sample :. - The biggest allowed key The size is 512 MB.
keys The command is used to return the... Of the service specified mode key list , The command is blocking , During the blocking period Redis Service not available , have access to scan command , This command extracts the specified pattern without blocking key list , But there is a certain probability of repetition , You can perform de duplication on the client .
2.1 Binary security and SDS
stay Redis The study of , Will see a lot of binary safe Description of , such as key、String wait , So what is binary security ? It's easy to understand : Only care about binary strings , Don't care about the specific format , Will only strictly follow the binary data access , Do not attempt to parse data in a special format , That is, how binary data is written , What happens when reading .
C The default string of the language is '\0' At the end of the , That is to say, the string you save contains '\0',c The language will recognize the previous data , The latter will be ignored , So it's not safe . such as str=“redis cluster” This string , stay C The result of direct reading in is “redis”, and strlen(str) The result of calculating the length of the string is also 4, This is not equal to the original data , So binary is unsafe .
Redis Although it is adopted C Language to develop , however Redis The string data structure stored internally is implemented by itself , It doesn't follow C Language string data structure .
redis use SDS(simple dynamic string) To implement simple dynamic strings (sds.h/sds.c), At the same time, it guarantees redis The stored data is binary secure , The structure is as follows :
struct sdshdr {
int len;
int free;
char buf[];
};
Copy code In fact, the bottom layer of the string is a char Array ( and Java Of ArrayList similar ), At the same time, it also preserves len and free Two attributes .
len Indicates the length of the actual contents of the string ,free Represents all space allocated to the string - The actual content length of the string , That is to say free Indicates the free space length of the string , So you take the value of this string by len Property to determine the length of the actual content , Then take the value of .
String concatenation is appended to free In space . therefore redis To calculate the length and update the contents of the string C Language is much faster , Because to find the length, you only need to return the len Property value ,C The language wants to traverse the entire string to know the length . String concatenation “ commonly ” There is no need to reallocate space , The concatenated string is placed directly in free Just in memory .
Because of the definition of string length len, Therefore, when processing strings, zero value bytes are not used (\0) Mark the end of the string , Can get the complete input data , That is, binary is guaranteed .
3 Redis String
String Is the most basic Redis Value type , It's also Memcached Unique value type in .String Is the most basic Redis Value type , It's also Memcached Unique value type in . because Redis key Is string , So when we also use string type as value , We map one string to another .String The maximum length of the value is 512 MB.
Redis Of String It's binary safe , It means Redis String It can contain any type of data , for example JPEG Image or serialized Ruby object , In order to improve the running speed of the website , have access to String Type cache some static files , Such as picture file 、CSS Documents, etc. .
We can use redis-cli Order to Redis Perform command line operations . The most basic operation is SET and GET command :
127.0.0.1:6379> set xx yyy
OK
127.0.0.1:6379> get xx
"yyy"
Copy code Use SET On command , If the same has already existed key The cache of , Will perform value Replacement :
127.0.0.1:6379> set xx yyy
OK
127.0.0.1:6379> get xx
"yyy"
Copy code have access to SETNX To avoid the above situation ,SETNX yes “SET if Not eXists( If it doesn't exist , be SET)” Abbreviation . Only in key In the absence of , Will key key Is set to value . if key Already exist , be SETNX Order no action , The original value will not be overwritten .
127.0.0.1:6379> GET a
(nil)
127.0.0.1:6379> SETNX a aa
(integer) 1
127.0.0.1:6379> SETNX a bb
(integer) 0
127.0.0.1:6379> GET a
"aa"
Copy code Another interesting command is that the operation is GETSET command , It is specified key Set the new value and return the original value . What's the use of this ? for example : Your system uses whenever new users access it INCR Command to operate a Redis key. If you want to collect this information every hour . You can start every hour GETSET This key And assign it 0 And read the original value .
127.0.0.1:6379> get num
"100"
127.0.0.1:6379> GETSET num 0
"100"
127.0.0.1:6379> get num
"0"
Copy code 3.1 Numerical atomic operation
Even if Redis The basic value of is a string type , You can also perform some special operations , For example, atomic increment ( The premise is that the string of integer type ).
127.0.0.1:6379> set num 100
OK
127.0.0.1:6379> incr num
(integer) 101
127.0.0.1:6379> incr num
(integer) 102
127.0.0.1:6379> get num
"102"
Copy code INCR The command parses the string value to 10 It's binary 64 Bit signed integer data , Add one , Finally, set the obtained value to the new value . There are other similar commands , Such as INCRBY—— Increase the specified value 、DECR—— Self reduction 1 and DECRBY—— Minus the specified value . In the internal , It's actually the same command , Perform... In a slightly different way .
127.0.0.1:6379> INCRBY num 20
(integer) 122
127.0.0.1:6379> decr num
(integer) 121
127.0.0.1:6379> decr num
(integer) 120
127.0.0.1:6379> DECRBY num 20
(integer) 100
Copy code If key A string that is not an integer numeric type , Using the increase or decrease operation will return an exception :
127.0.0.1:6379> set flo 11.1
OK
127.0.0.1:6379> INCR flo
(error) ERR value is not an integer or out of range
Copy code If specified key non-existent , So before the increase or decrease operation , Will first set its value to 0:
127.0.0.1:6379> DECR numm
(integer) -1
Copy code Even if multiple clients are connected to the same key Issue the command of increase / decrease operation , It will never lead to competitive situations . For example, the following can never happen :“ client 1 And the client 2 Simultaneous reading 10, They both added to it 11, Then set the new value to 11”. The final value must be 12.
because INCR、INCRBY、DECR、DECRBY Such numerical operations are atomic , So it can be used to do many useful things , The most common use case is the counter .
The idea is : Every time there are related operations , As to the Redis Server sends a incr command . For example, such a scene : We have a web application , We want to record how many times each user visits this website every day .web The application only needs to be spliced by the user id And a string representing the time of the day key, Every time a user visits this page, it is necessary to key Execute it. incr command .
This scenario can be extended in many ways :
- By combining INCR and EXPIRE command , It can realize a counter that only records the access times of users in the specified interval .
- The client can use the GETSET Command gets the value of the current counter and resets it to 0.
- Through similar DECR perhaps INCRBY Equal atomic increment / Decreasing order , Some values can be increased or decreased according to the user's operation , Like online games , The user's game score needs to be controlled in real time , Scores may increase or decrease .
In addition, for the e-commerce field , There is also an application called secsha business .
3.2 The batch operation
Redis Support for setting or retrieving multiple... In a single command key The ability to value , This is also useful for reducing latency .
Redis Provides MSET and MGET command :
127.0.0.1:6379> MSET a aa b bb c cc d dd
OK
127.0.0.1:6379> MGET a b d
1) "aa"
2) "bb"
3) "dd"
Copy code When using MGET when ,Redis Returns an array of values .
3.3 key General operation
Some commands are not specific to a particular value type , Can be associated with any type of key Use it together .
for example , Use EXISTS Command judgment key Whether the corresponding value exists , Will return 1 or 0. You can also pass in multiple key, Will return the existing key The sum of :
127.0.0.1:6379> keys *
1) "b"
2) "c"
3) "d"
4) "a"
5) "num"
127.0.0.1:6379> EXISTS a
(integer) 1
127.0.0.1:6379> EXISTS a b
(integer) 2
127.0.0.1:6379> EXISTS a b e
(integer) 2
127.0.0.1:6379> EXISTS e
(integer) 0
Copy code Use DEL The command can delete key Corresponding value , Will return 1 or 0, The identification value is successfully deleted ( The value is ) Or not deleted (key The corresponding value does not exist ). You can also pass in multiple key, Will return successfully deleted key The sum of :
127.0.0.1:6379> DEL a c
(integer) 2
127.0.0.1:6379> DEL a c
(integer) 0
127.0.0.1:6379> DEL b
(integer) 1
127.0.0.1:6379> DEL a
(integer) 0
Copy code TYPE The command can return key The storage type of the corresponding value :
127.0.0.1:6379> KEYS *
1) "d"
2) "num"
127.0.0.1:6379> TYPE num
string
127.0.0.1:6379> TYPE d
string
127.0.0.1:6379> DEL num d
(integer) 2
127.0.0.1:6379> TYPE num
none
127.0.0.1:6379> TYPE d
none
Copy code Can be applied to any value type key Set timeout , When this time comes key-value Will be deleted . Precision can be in milliseconds or seconds , But the resolution of the expiration time is always 1 millisecond .
Usually use EXPIRE To set the timeout , The default unit is seconds , You can also call this command again to change the timeout :
127.0.0.1:6379> set a aa
OK
127.0.0.1:6379> set b bb
OK
127.0.0.1:6379> EXPIRE a 8
(integer) 1
127.0.0.1:6379> GET a
(nil)
127.0.0.1:6379> GET b
"bb"
Copy code The expired information is copied and saved on disk , actually Redis Will save key Expiration time point of , When Redis When the server stops , Time has actually passed , This expires on restart key place it on clipboard .
Make in key Use before expiration PERSIST The command can remove key Timeout for :
127.0.0.1:6379> SET a aa
OK
127.0.0.1:6379> EXPIRE a 8
(integer) 1
127.0.0.1:6379> PERSIST a
(integer) 1
127.0.0.1:6379> GET a
"aa"
Copy code We can also set key-value Set the timeout when , Use TTL see key The rest of the time :
127.0.0.1:6379> SET a aa ex 8
OK
127.0.0.1:6379> TTL a
(integer) 4
127.0.0.1:6379> TTL a
(integer) 2
127.0.0.1:6379> GET a
(nil)
Copy code The following is a summary of timeout related commands :
EXPIREtake key The lifetime of is set to seconds .PEXPIRE: take key The lifetime of is set to milliseconds .EXPIREAT: take key The expiration time for is set to timestamp The timestamp of the number of seconds represented .PEXPIREAT: take key The expiration time for is set to timestamp Timestamp of the number of milliseconds represented .PTTL: Check... In milliseconds key The rest of the time .
3.5 Colored eggs
Redis Of LOLWUT [VERSION version] The command will return a string containing the generated computer art image , And with Redis Version text . The demo version of this article is Redis 6.2.
LOLWUT VERSION 5:
LOLWUT:
Official documents :www.redis.com.cn/commands/lo…
Related articles :
If you need to communicate , Or the article is wrong , Please leave a message directly . In addition, I hope you will like it 、 Collection 、 Focus on , I will keep updating all kinds of Java Learning blog !
边栏推荐
- Shen Min, CIO of science and technology innovator Digital China Group: the best practice model is failing, and open source accelerates Distributed Innovation
- Li Yang, a scientific and technological innovator and CIO of the world's top 500 group: the success of digital transformation depends on people. Decision makers should always focus on "firewood"
- Value investment
- 【926. 将字符串翻转到单调递增】
- CentOS 7 installing MySQL 8
- Abstract classes and interfaces
- Jetpack架构组件学习(3)——Activity Results API使用
- High quality and good books help guide apes and recommend "good summer books" with the four major publishers
- np.meshgrid()函数 以及 三维空间中的坐标位置生成 以及 numpy.repeat()函数介绍
- Overview of software definition storage (one article is enough)
猜你喜欢

原始套接字使用

基于SSM实现水果商城批发平台

《保护我们的数字遗产:DNA数据存储》白皮书发布

传输层协议 ——— TCP协议

Li Yang, a scientific and technological innovator and CIO of the world's top 500 group: the success of digital transformation depends on people. Decision makers should always focus on "firewood"

SAP Hana error message sys_ XSA authentication failed SQLSTATE - 28000

Papaya Mobile has a comprehensive layout of cross-border e-commerce SaaS papaya orange. What are the opportunities for this new track?

MYSQL的最左匹配原則的原理講解

Create simple windowing programs using Visual Studio 2017

True north reading notes
随机推荐
MySQL VI Database lock
MySQL optimized slow log query
[cloud native] establishment of Eureka service registration
《第五项修炼》读书笔记
奇葩错误 -- 轮廓检测检测到边框、膨胀腐蚀开闭运算效果颠倒
2026年中国软件定义存储市场容量将接近45.1亿美元
机器学习之数据处理与可视化【鸢尾花数据分类|特征属性比较】
Storage R & D Engineer Recruitment
2022 pole technology communication - the dispute over anmou technology is settled, and the cornerstone of the local semiconductor industry is more stable
SAP HANA 错误消息 SYS_XSA authentication failed SQLSTATE - 28000
np.meshgrid()函数 以及 三维空间中的坐标位置生成 以及 numpy.repeat()函数介绍
[DDS] dds-rpc implementation based on opendds
one
MySQL III Configuration file & log file
Crazy temporary products: super low price, big scuffle and new hope
Abstract classes and interfaces
JVM (VIII) Thread safety and lock optimization
Introduction to applet
DNA数字信息存储的研究进展
IoT简介