当前位置:网站首页>Five data structures of redis
Five data structures of redis
2022-07-06 18:12:00 【eight hundred and forty-eight million six hundred and ninety-ei】
1. Redis Five data structures for
Redis You can store mappings between keys and five different types of values .
key
Can only be of typecharacter string
,Value supports five data types
: character string 、 list 、 aggregate 、 Hash table 、 Ordered set .
Programs are used to process data ,redis It's used to store data , The data processed by the program shall be stored in redis in , Data with different characteristics should be stored in redis Different types of data structures in .
character string : single key: single value
list list : Orderly and repeatable single key: Multi order value( The order here refers to the order of input and output )
set aggregate : Disordered and unrepeatable single key: Multiple disorder value
pojo hash single key : object ( attribute : value )
zset single key: Multi order value( Order here refers to sorting )
- String type String
The string type is Redis The most basic data structure in , It can store any kind of data , Including binary data , Serialized data ,JSON The object is even a picture . Maximum 512M.
- List the type list
Redis List is a simple list of strings , Sort by insertion order . You can add an element to the head of the list ( On the left ) Or tail ( On the right ), The bottom layer is a linked list structure .
- Collection types set
Redis Of Set yes string An unordered unrepeated set of types .
- Hash type hash
Redis hash It's a string Type of field and value Mapping table ,hash Ideal for storing objects .
- Ordered set type zset (sorted set)
Redis Ordered set zset And collection set The same is true. string Collection of type elements , And duplicate members are not allowed .
The difference is zset Each element of the is associated with a score ( Scores can be repeated ),redis Sort the members of the set from small to large by score .
Is to implement the sortedset Interface TreeSet aggregate , Use comparator to realize sorting
2. redis Operation command in
redis Stored in the Key-Value
2.1 redis In the relevant key Operation command of :
2.1.1 View all in the database key : keys pattern
effect : Find all matching patterns pattern Of key. pattern You can use wildcards :*
: Express 0 Or more
character , for example : keys* Query all key.?
: Express Single
character , for example : wo?d, matching word , wood[]
: Express choice [] A character in , for example wo[or]d, matching word, wood, Mismatch wold、woord
keys *
: View all in the database instance keykeys k*
: View all in the data with k At the beginning keykeys h*o
: View all in the data with h Begin with o At the end of the key
keys h[abc]llo
: View all in the data with h Begin with llo ending , also h You can only take abc Of one character in the key
2.1.2 Judge key Does it exist in the database :exists key
exists key
If there is , return 1; If it doesn't exist , Then return to 0
exists key [key key .....]
The return value is the quantity that can exist
2.1.3 Move the specified key To the specified database instance : move key index
move k 1
Rarely used , In general , A database instance corresponds to a project , This transfer key Rarely used in another database instance
2.1.4 View specified key The remaining lifetime of :ttl
time to livettl k1
see k1 The remaining lifetime of
If k1 There is no return -2.
If k1 Return without setting the lifetime -1, As long as there is no downtime, data always exists
2.1.5 Set up key Maximum lifetime of :expire key seconds
expire k2 20
to k2 Set the lifetime 20s,,, By time the data disappears
2.1.6 View specified key Data type of type key
type k1
Returns the current data type
2.1.7 rename key: rename key newkey
rename k1 k2
hold k1 Change to k2,,, The value does not change, but it is renamed key
2.1.8 Delete key : del key
del key
Delete the specified key The data of , The return value is the actual deletion key The number of
You can also delete multiple data del k1 k2 k3 ....
Delete multiple data
2.2 redis In the relevant String Operation command of data type
2.2.1 take string Data setting of type redis in :set key value
set zsname zhangsan
set zsage 20
set totalRows 100
2.2.2 from redis In order to get string Data of type :get key
get zsname
get zsage
get totalRows
2.2.3 Append string :append key value
set phone 1388888
add to key-valueappend phone 6666
stay key=phone Add 6666, Returns the length of the appended string
If key non-existent , Create key-value
2.2.4 Get the length of string data :strlen key
strlen phone
Gets the length of the string
2.2.5 Add the string value 1 operation :incr key
incr zsage
Add Zhang San's age value 1 , Back to plus 1 Operation of After the value of
If key non-existent , First set up a key, The value is initialized to 0, Then proceed incr operation
requirement key Indicated by value It must be a number , otherwise , Report errors
incr zsname Report errors
2.2.6 Subtract string values 1 operation :decr key
- Return minus 1 Data after operation
- If key non-existent , First set up a key, The value is initialized to 0, Then proceed decr operation .
- requirement key Indicated by value It must be a number , otherwise , Report errors
2.2.7 Adds a string value offset operation :incrby key offset
incrby key offset
- Back to plus offset Data after operation
- If key non-existent , First set up a key, The value is initialized to 0, Then proceed incrby operation .
- requirement key Indicated by value It must be a number , otherwise , Report errors
incrby zsage 10
Add... To Zhang San's data 10 operation ,
2.2.8 Subtract string values offset operation :decrby key offset
- Back to plus offset Data after operation
- If key non-existent , First set up a key, The value is initialized to 0, Then proceed decrby operation .
- requirement key Indicated by value It must be a number , otherwise , Report errors
decrby zsage 10
Subtract the data of Zhang San 10 operation
2.2.9 Close interval intercept string :getrange key startIndex endIndex
The subscript is intercepted from 0 Start , Closed interval interception
The subscript from left to right is 0,1,2,3…
The subscript from right to left is -1,-2,-3 …
zhangsangetrange zsname 2 3
The return is an
gettrange zsname 2 -3
Return results angs
2.2.10 use value Override subscript startIndex Starting string :setrange key startIndex value
zhangshansetrange zsname 5 xiaoming
The modified data is zhangxiaoming
边栏推荐
- declval(指导函数返回值范例)
- C语言通过指针交换两个数
- Why should Li Shufu personally take charge of building mobile phones?
- ASEMI整流桥DB207的导通时间与参数选择
- 重磅!蚂蚁开源可信隐私计算框架“隐语”,主流技术灵活组装、开发者友好分层设计...
- Jerry's watch deletes the existing dial file [chapter]
- [introduction to MySQL] the first sentence · first time in the "database" Mainland
- std::true_type和std::false_type
- The difference between parallelism and concurrency
- Jerry's watch reads the file through the file name [chapter]
猜你喜欢
F200——搭载基于模型设计的国产开源飞控系统无人机
QT中Model-View-Delegate委托代理机制用法介绍
FMT开源自驾仪 | FMT中间件:一种高实时的分布式日志模块Mlog
30 分钟看懂 PCA 主成分分析
[introduction to MySQL] the first sentence · first time in the "database" Mainland
Kivy tutorial: support Chinese in Kivy to build cross platform applications (tutorial includes source code)
Interesting - questions about undefined
FMT open source self driving instrument | FMT middleware: a high real-time distributed log module Mlog
【.NET CORE】 请求长度过长报错解决方案
SQL statement optimization, order by desc speed optimization
随机推荐
微信为什么使用 SQLite 保存聊天记录?
78 岁华科教授逐梦 40 载,国产数据库达梦冲刺 IPO
[Android] kotlin code writing standardization document
Principle and usage of extern
Kivy tutorial: support Chinese in Kivy to build cross platform applications (tutorial includes source code)
Pytest learning ----- detailed explanation of the request for interface automation test
一体化实时 HTAP 数据库 StoneDB,如何替换 MySQL 并实现近百倍性能提升
Comparative examples of C language pointers *p++, * (p++), * ++p, * (++p), (*p) + +, +(*p)
Manifest of SAP ui5 framework json
【Android】Kotlin代码编写规范化文档
传统家装有落差,VR全景家装让你体验新房落成效果
Appium automated test scroll and drag_ and_ Drop slides according to element position
ASEMI整流桥DB207的导通时间与参数选择
ADB common commands
Jerry is the custom background specified by the currently used dial enable [chapter]
MySQL 8 sub database and table backup database shell script
std::true_ Type and std:: false_ type
Why should Li Shufu personally take charge of building mobile phones?
Interview shock 62: what are the precautions for group by?
[introduction to MySQL] the first sentence · first time in the "database" Mainland