当前位置:网站首页>[introduction series of redis] data types and related commands of redis
[introduction series of redis] data types and related commands of redis
2022-07-23 17:24:00 【Hall owner a Niu】
Personal profile
- Author's brief introduction : Hello everyone , I'm Daniel , New star creator of the whole stack .
- Stand by me : give the thumbs-up + Collection ️+ Leaving a message.
- Series column : Punching and kicking database
- Maxim : To be light , Because there are people who are afraid of the dark !

Catalog
Preface
And then learn Redis Data types and some related instructions , I will continue to practice !
redis Start command and key command
Start command
Redis The command is used in the redis Perform operations on the service .
To be in redis To execute commands on the service, you need to start redis Server side .Redis The server downloaded it before us redis In the installation package of .
Start command :redis-server
After starting the server , Another one cmd Start client .
To be in redis Executing commands on a service requires a redis client .Redis The client downloaded before us redis In the installation package of .
Start command :redis-cli
No pictures are posted here , This part has been implemented during the installation of the previous section , You can go back to see what you want to see !
Key command
redis Key commands must be used in conjunction with data type commands , Here we first separate him , have a look redis What key commands are there , Probably remember , Later, the actual operation of data types will use !
( Command keywords are not case sensitive )
DEL key
- This command is used in key Delete... When it exists key.
EXISTS key
- Check the given key Whether there is
EXPIRE key seconds
- For a given key Set expiration time , In seconds .
PERSIST key
- remove key The expiration time of ,key Will last .
TTL key
- Returns... In seconds key The remaining expiration time of .
RENAME key newkey
- modify key The name of .
TYPE key
- return key The type of value stored .
There are so many commands in common use , The actual operation and data type are put below .
redis data type
Redis Five data types are supported :string( character string ),hash( Hash ),list( list ),set( aggregate ) And zset(sorted set: Ordered set ).
His data operation behavior is still to save , modify , Delete , obtain .
Attached below is a carefully drawn diagram to see the differences between these five data types :
String( character string )
The string type is Redis The most basic data storage type in , He was in Redis Is binary safe , This means that the type can accept data in any format , Such as JPEG Image data or Json Object description information, etc , stay Redis Of string type Value The maximum length of data that can be held is 512M.
SET key value
- Set the specified key Value .
MSET key value [key value ...]
- Set one or more... At the same time key-value Yes .
GET key
- Get specified key Value .
MGET key1 [key2..]
- Get all ( One or more ) Given key Value .

Pictured ,help+ The instruction word can print out the usage of the instruction .
Next, try setting the expiration time for the key :
Pictured , Before not setting, the value is -1, Set ten second expiration , The value after expiration is -2, This value disappears
Why should we practice this key Overdue , because Some scenes key Expiration is important , for example : Verification Code ( The verification code expires after a period of time ).
Okay , There are so many string instructions , Please learn more instructions by yourself !
Hash( Hash )
Redis hash It's a string Type of field( Field ) and value( value ) Mapping table ,hash Ideal for storing objects . Like a shopping cart , You can use it hash Storage .
Look at the picture above, it's a big key , His value is divided into keys - value .
HDEL key field1 [field2]
- Delete one or more hash table fields
HLEN key
- Get the number of fields in the hash table
HGET key field
- Gets the value of the specified field stored in the hash table .
HGETALL key
- Gets the specified in the hash table key All fields and values of
HMGET key field1 [field2]
- Get the value of all the given fields
HKEYS key
- Get all the fields in the hash table
HVALS key
- Get the values in all hash tables
HSET key field value
- Hash table key In the field field The value of the set value .
HMSET key field1 value1 [field2 value2 ]
- There will be more than one field-value ( Domain - value ) Set to hash table key in .

Be careful :HDEL key field1 [field2] Delete hash One of the field, Small key ,DEL key It's this hash Delete , Delete the big key .
List(Redis 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 )
LPUSH key value1 [value2]
- Insert one or more values into the list header ( On the left )
RPUSH key value1 [value2]
- Add one or more values... To the list ( On the right )
LRANGE key start stop
- Get the elements in the specified range of the list ,start from 0 Start , And start and stop The values of all contain
LREM key count value
- Remove list elements
- Put the list before count The value of the second occurrence is value Remove the elements of
- count > 0: Remove from the beginning to the end
- count < 0: Remove from the tail to the head
- count = 0: Remove all
LSET key index value
- Set the value of the list element through the index
LLEN key
- Get list length
LINDEX key index
- Get the elements in the list by index
LINSERT key BEFORE|AFTER pivot value
- Insert an element before or after a list element
LPOP/RPOP key
Move out and get the first element of the list / The last element

Please learn other instructions by yourself !
Set( aggregate )
Redis Of Set yes String Unordered collection of type . Collection members are unique , This means that duplicate data cannot appear in the collection .
The code of the collection object can be intset perhaps hashtable.
Redis The middle set is implemented through a hash table , So add the , Delete , The complexity of searching is O(1).
notes : Collection has no update operation
SADD key member1 [member2]
- Add one or more members to the collection
SCARD key
- Get the number of members of the collection
SMEMBERS key
- Returns all members of the collection
SREM key member1 [member2]
- Remove one or more members of the collection
SMOVE source destination member
- take member Elements from source The assembly moves to destination aggregate
SPOP key
- Remove and return a random element from the collection

Please learn other instructions by yourself !
sorted set(zset)(Redis Ordered set )
Redis An ordered set is the same as a set string Collection of type elements , And duplicate members are not allowed .
The difference is that each element is associated with a double Score of type .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 .
Collections are implemented through hash tables , So add the , Delete , The complexity of searching is O(1).
ZADD key score1 member1 [score2 member2]
- Add one or more members... To an ordered collection , Or update scores of existing members
ZRANGE key start stop [WITHSCORES]
- Return the members in the specified interval of the ordered set through the index interval
ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT]
- Returns the members of an ordered set in a specified interval through scores
ZRANK key member
- Returns the index of a specified member in an ordered collection
ZREM key member [member ...]
- Remove one or more members of an ordered collection
ZREMRANGEBYSCORE key min max
- Remove all members of a given fraction interval from an ordered set

Please learn other instructions by yourself !
Conclusion
If you think the blogger's writing is good , You can pay attention to the current column , Bloggers will finish this series ! You are also welcome to subscribe to other good columns of bloggers .
Series column
Soft grinding css
Hard bubble javascript
flask Framework quick start
边栏推荐
猜你喜欢
[web vulnerability exploration] SQL injection vulnerability

ROS2自学笔记:Rviz可视化工具

程序环境和预处理

Detailed explanation of SQL error reporting and blind annotation

ROS2自学笔记:RQT可视化工具

unity之制作二维码扫描

keil错误和解决办法(1):FCARM - Output Name not specified, please check ‘Options for Target - Utilities‘

CSR、SSR 与 SSG

Pymoo学习 (2):带约束的双目标优化问题

Ros2 self study notes: rqt visualization tool
随机推荐
Search Binary Tree - find nodes, insert nodes, delete nodes
SQL bool盲注和时间盲注详解
Wechat applet class binding, how to bind two variables
Description and usage of Axi interconnect IP core
【redis入门系列】redis搭建主从服务器
Pymoo learning (3): use multi-objective optimization to find the set of optimal solutions
Sorting - introduction, code ideas, usage suggestions, code implementation -1
Kubernetes kubelet 硬核知识 架构
详解一次SQL优化
AXI interconnect IP核的说明及用法
Virtual machine network connection mode
leetcode刷题记录
Function secondary development / plug-in development of JMeter (detailed version)
CSR、SSR 与 SSG
怎么正确设置路由器
食品安全|听起来很健康的植物肉,是什么来头?
Fundamentals of C language -- 2-6 pointers, arrays and sizeof operators
Win11如何添加图片3D效果?Win11添加图片3D效果的方法
59.雷电安全常识
keil错误和解决办法(1):FCARM - Output Name not specified, please check ‘Options for Target - Utilities‘