当前位置:网站首页>Example analysis of five data types in redis
Example analysis of five data types in redis
2022-06-09 18:23:00 【Yisu cloud】
Redis Analysis of five data types of
This article mainly explains “Redis Analysis of five data types of ”, Interested friends might as well come and have a look . The method introduced in this paper is simple and fast , Practical . Now let Xiaobian take you to learn “Redis Analysis of five data types of ” Well !

1.Redis Of 5 Type of data
redis It's a kind of advanced key-value Storage system , among value Five data types are supported :
Redis Supported key data types |
string String type |
hash Table type |
list List the type |
set Collection types |
zset Ordered set type |
About key The definition of , Note the following :
Don't suggest key The name is too long , Usually no more than 1024, If it is too long, the query speed will be affected .
It's not recommended to be too short , Too short reduces readability .
Usually in the company , There's a unified naming convention .
2. String type string
2.1 summary
The string type is Redis The most basic data storage type in , It's in Redis Save in binary , There is no encoding and decoding process . No matter what string is stored 、 Integers 、 Floating point types are written as strings . stay Redis Of string type Value The maximum length of data that can be held is 512M. This is the most commonly used data type in the future .

2.2 Common commands
command | Behavior |
set key value | towards redis Add... To the database 1 Keys and values of string type , return OK Indicates successful addition . The same name will replace |
get key | Extract a value of the specified key from the database , If there is a return value , If there is no return nil |
del key | Delete the specified key and value , If the deletion is successful , Return the number of deletions . Otherwise return to 0 |
setnx key value | At the designated key When there is no , by key Set the specified value . |
2.3 Command demonstration
demand :
Add a key to company, The value is itcast
Set another key to company, The value is heima
obtain company The elements of
Delete company Elements
Delete again company See if the return value is the same
obtain company Look at the return value
Set the key to job, The value is programmer
Set again job The value of is code-farmer, Inquire about job Value
2.4 Execution effect

3. Hash type hash
3.1 summary
Redis Medium Hash Types can be viewed as having String And String Value Map Containers , every last Hash Can be stored 40 Billion key value pairs .

So this type is very suitable for storing information about objects . If a user has a name , password , Information such as age , You can have username、password and age Its storage structure is as follows :

3.2 Common commands
command | Behavior |
hset key Field value | Adds a pair of... To the specified key hash Field name and value of type |
hget key Field | Get the value of the specified field of the specified key |
hmset key Field value Field value | mulitple, Set multiple field names and values to a key at once |
hmget key Field Field | Get the values of multiple fields from the specified key at once |
hdel key Field Field | Delete one or more fields in a key |
hgetall key | Get all the field values of a key |
3.3 Command demonstration
demand :
establish hash The key of type is user, And add a field as username, The value is newboy
towards user The field added in is password, The value is 12345
towards user The field added in is age, The value is 18
Get... Separately user Medium username、password and age The field values of the

towards user Add multiple fields and values at the same time ,birthday 2018-01-01 sex male

Get multiple fields at the same time :age and sex

obtain user All fields and values in

Delete user Birthday and password fields in

4. List the type list
4.1 summary
stay Redis in ,List The type is a list of strings in insertion order . It is the same as the common linked list in data structure , We can be on its left (left) And right (right) Add new elements . When inserting , If the key doesn't exist ,Redis A new linked list will be created for the key , If this key already exists , It's to list Additive elements . On the contrary , If all elements in the list are removed , Then the key will also be removed from the database .List The maximum number of elements that can be contained in is 40 One hundred million .

4.2 Common commands
command | Behavior |
lpush key Elements Elements | left push Add a list element to the specified key on the left side of the list , If the key doesn't exist ,Redis A new linked list will be created for the key , If this key already exists , It's to list Additive elements . |
rpush key Elements Elements | right push Add a list element to the specified key on the right side of the list |
lpop key | left pop Pop up an element from the left side of the specified key , The elements in the list are deleted . |
rpop key | right pop Pop up an element from the right of the specified key , The elements in the list are deleted . |
lrange key Start end | Extract the element list of the specified range from the list of the specified key , Count from the left 0 Start , Count from the right -1 Start . If you want to take the whole list , First the 0, The end is -1 |
llen key | Get the length of the specified list |
4.3 Command demonstration
Execution effect

demand :
towards mylist In the list of keys , Add... From the left a b c Three elements
Add... From the right one two three Three elements
Query all elements

Add a repeating element from the right three

Delete the rightmost element three

Delete the leftmost element c
Get the number of elements in the list

5. Collection types set
5.1 summary
stay Redis in , We can Set Type as a character set without sorting , and List Same type , We can also add data values of this type 、 Delete or determine whether an element exists, etc .
Set The maximum number of elements that can be included is 40 Billion , and List The difference in type is ,Set Duplicate elements are not allowed in the collection .

5.2 Common commands
command | Behavior |
sadd key Elements Elements | towards set Add... To the collection 1 Elements or elements |
smembers key | Query all the elements in the specified collection |
sismember key Elements | Determines whether the specified element is in a collection , If there is a return 1, Otherwise return to 0 |
srem key Elements Elements | remove Delete one or more specified elements |
sunion key 1 key 2 | Returns the union of a given set . Nonexistent collection key Be regarded as an empty set . |
5.3 Command demonstration
demand :
towards myset Add... To the collection A B C 1 2 3 Six elements
Again to myset Add B Elements , See if it can be added successfully
Show all members , It is found that the order of the elements is different from that of the added elements , The elements are disordered
Delete the C This element , And then look at the results
Judge A Whether in myset Collection
Judge D Whether in myset Collection
Create key as set1 Set : The element is a b c
Create key as set2 Set : The element is a b d
obtain set1 and set2 Union , And display

6. Ordered set zset
6.1 summary
Redis An ordered set, like a set, is disordered and cannot be repeated .
The difference is that each element is associated with a score .redis It's the scores that sort the members of a collection from small to large . Members of an ordered set are unique , But fractions (score) But it can be repeated , Each collection can store 40 More than 100 million members .

6.2 Common commands
command | Behavior |
zadd key fraction value fraction value | Add one or more members... To an ordered collection |
zrange key Start index End index | Return the members in the specified interval in the ordered set through the index interval |
zrem key value value | Remove one or more members of an ordered collection |
zrank key value | Returns the index of a specified member in an ordered collection |
zcard key | Get the number of members of the ordered set |
zscore key value | Get the score of the designated member |
6.3 Command demonstration
Add key country, The score is 10, The value is Japan
Add key country, The score is 5, The value is USA
Add key country, The score is 1, The value is China, The score is 120, The value is Korea
Inquire about country All the elements in
Inquire about Japan The index number of ( from 0 Start )
The deletion value is USA The elements of
Inquire about country How many elements are there in
6.4 effect

Here we are , I'm sure you're right “Redis Analysis of five data types of ” Have a deeper understanding of , You might as well put it into practice ! This is the Yisu cloud website , For more relevant contents, you can enter the relevant channels for inquiry , Pay attention to our , Continue to learn !
边栏推荐
- C# 29. textbox始终显示最后一行
- 【操作教程】如何正确使用海康demo工具配置通道上线?
- Golang基础(2)
- Golang基础(1)
- [work with notes] multiple coexistence of ADB, sound card, network card and serial port of Tina system
- 10 common high-frequency business scenarios that trigger IO bottlenecks
- Analysis of C language [advanced] paid knowledge [II]
- gcc编译demo+Makefile使用
- ElasticSerach
- Bienvenue à la plateforme d'écriture InfoQ!
猜你喜欢

【工作随笔记】Tina 系统的 ADB、声卡、网卡、串口多路共存

C# 29. textbox始终显示最后一行

redis源码学习-02_内存分配

CAM350检查gerber与钻孔文件

Development and practice of the martyr's family search system

图解|高性能服务器设计之缓存系统一致性

功能:多文件上传,统一提交

Redis source code learning-01_ Debugging redis source code in clion

Interpretation of new shares | ranked second in the event content marketing industry, and wanted to push SaaS products on the cloud to create a competitive barrier

Moco -Momentum Contrast for Unsupervised Visual Representation Learning
随机推荐
js中性能优化之函数防抖
How to learn the process of KD tree construction and search with cases?
php使用unlink删除文件提示无权限
redis源码学习-05_字典、哈希表
功能:多文件上传,统一提交
AD 删除尺寸标注
Process control -- > > process termination
Redis source code learning-02_ memory allocation
Golang Foundation (2)
进程控制--->进程终止
如何利用无线通讯技术优化钢铁厂消防用水管网?
logrotate
Ali's 10-year Technician: seven ways of thinking of the leader
[play with Huawei cloud] Huawei cloud zero code development image compression tool
解析:稳定币不是“稳定的币” 其本质是一种产品
Bienvenue à la plateforme d'écriture InfoQ!
Go 最细节篇|pprof 统计的内存总是偏小?
[work with notes] multiple coexistence of ADB, sound card, network card and serial port of Tina system
深入浅出 RPC 框架
MySQL删除方法delete、truncate、drop的区别是什么