当前位置:网站首页>[redis] data introduction & General Command & string type
[redis] data introduction & General Command & string type
2022-06-29 01:59:00 【Riding a snail chasing a missile '】
List of articles
One 、Redis Data structure introduction
Redis It's a Key-Value The database of ,key It's usually String type , however value There are many types of :

1.1 Redis Of 5 Data structures
A、 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.

B、 List the type list
Redis List is a simple list of strings , Sort by insertion order , Elements can be repeated . 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 .

C、 Collection types set
Redis Of Set yes string type An unordered and unrepeated set of .

D、 Hash type hash
Redis hash It's a string Type of field and value Mapping table ,hash Ideal for storing objects .

E、zset(sorted set)
Redis Ordered set zset And collection set The same is true. string Collection of type elements , And No repetition Members of .
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 .

Two 、Redis General commands for

The basic general commands are shown in the figure above , Here is a detailed introduction !
2.1 KEYS

Supported glob-style patterns:
h?llomatcheshello,halloandhxlloh*llomatcheshlloandheeeelloh[ae]llomatcheshelloandhallo,but nothilloh[^e]llomatcheshallo,hbllo, … but nothelloh[a-b]llomatcheshalloandhbllo
Use \ to escape special characters if you want to match them verbatim.
Return value :
Array reply: list of keys matching pattern.

View all that match the template key, The bottom layer is similar to Fuzzy query The implementation of the , Easy to cause blockage , It is not recommended to use... On production environment equipment !
2.2 DEL

Integer reply: The number of keys that were removed. Return to deleted key The number of

When deleting, delete , If not, no operation will be performed , The returned value is real and deleted key The number of .
2.3 EXISTS

Integer reply, specifically the number of keys that exist from those specified as arguments. Determines and returns the specified key The number of existence

2.4 EXPIRE & TTL
EXPIRE: to key Set an expiration date , The... Will be deleted automatically after the expiry date key
TTL: View one key The remaining validity period of


When TTL The return value is -2 When , Express this key No longer exists ~

When TTL The return value is -1 When , Express this key Permanent validity ~
3、 ... and 、String type
The string type is Redis The most basic data type in , It can store any kind of string , Including binary data 、 Serialized data 、JSON The object is even a picture .
The general idea of string type data operation is through key operation value,key It's a data ID ,value This is the business data we are interested in . Depending on the format of the string , We can divide it into three categories :
- string: Normal string
- int: Integer types , Can do self increasing 、 Since the reduction of operating
- float: Floating point type , Can do self increasing 、 Since the reduction of operating
The bottom layer enables storage in the form of a byte array , It's just that the coding method is different , The maximum space cannot exceed 512M


The basic general commands are shown in the figure above , Here is a detailed introduction !
3.1 SET & GET & GETSET

Options
The SET command supports a set of options that modify its behavior:
EXseconds – Set the specified expire time, in seconds.PXmilliseconds – Set the specified expire time, in milliseconds.EXATtimestamp-seconds – Set the specified Unix time at which the key will expire, in seconds.PXATtimestamp-milliseconds – Set the specified Unix time at which the key will expire, in milliseconds.NX– Only set the key if it does not already exist.XX– Only set the key if it already exist.KEEPTTL– Retain the time to live associated with the key.GET– Return the old string stored at key, or nil if key did not exist. An error is returned andSETaborted if the value stored at key is not a string.
Note: Since the SET command options can replace SETNX, SETEX, PSETEX, GETSET, it is possible that in future versions of Redis these commands will be deprecated and finally removed.
Return
Simple string reply: OK if SET was executed correctly.
Null reply: (nil) if the SET operation was not performed because the user specified the NX or XX option but the condition was not met.
If the command is issued with the GET option, the above does not apply. It will instead reply as follows, regardless if the SET was actually performed:
Bulk string reply: the old string value stored at key.
Null reply: (nil) if the key did not exist.
Return
Bulk string reply: the value of key, or nil when key does not exist.
In short :SET- Add or modify an existing String Key value pairs of type ;GET- according to key obtain String Type of value.

GETSET
Bulk string reply: the old value stored at key, or nil when key did not exist.
GETSET It can be done with INCR Used together to reset the atomic count . for example : Whenever something happens , A process may be keyed mycounter call INCR, But sometimes we need to get the value of the counter and reset its atoms to zero .

3.2 MSET & MGET

set、get Batch command for

3.3 INCR & INCRBY & INCRBYFLOAT

INCR: Let an integer key Self increasing 1
INCRBY: Let an integer key Auto increment specifies the step size
INCRBYFLOAT: Increment a floating-point number and specify the step size
By contrast, DECR、DECRBY( Or positive growth negative )

3.4 SETNX & SETEX & MSETNX & PSETEX & GETEX

Only when key Add only when it does not exist !

Actually
NXNamelysetA parameter of , CombinedEmpathy
SETEXyessetandexpireThe combination of , The setting key value and validity period are combinedMSETNXNamely SETNX Batch processing ofPSETEXWorking mode and SETEX Exactly the same , The only difference is that the expiration time ismillisecondNot specified in seconds .
GETEX:
When getting the value of the key , And you can choose to set its expiration time .
Options
The GETEX command supports a set of options that modify its behavior:
EXseconds – Set the specified expire time, in seconds.PXmilliseconds – Set the specified expire time, in milliseconds.EXATtimestamp-seconds – Set the specified Unix time at which the key will expire, in seconds.PXATtimestamp-milliseconds – Set the specified Unix time at which the key will expire, in milliseconds.PERSIST– Remove the time to live associated with the key.
Return
Bulk string reply: the value of key, or nil when key does not exist.

- And SETEX The difference lies in the time node for setting the validity period
3.6 SETRANGE & GETRANGE & SUBSTR
SETRANGE
Overwrite a portion of the string stored at the key , Start at the specified offset , Covers the entire value length . If the offset is greater than the current length of the string at the key , The string will be filled with zero bytes to fit the offset . Keys that do not exist are treated as empty strings , So this command will ensure that the string it saves is large enough , So that you can set the value at the offset .
Please note that , The maximum offset you can set is 2^29-1(536870911), because Redis String is limited to 512 MB. If you need to exceed this size , You can use multiple keys .

When key When there is no preset , Normal execution , Definite part 0 fill
GETRANGE
Returns a substring of a string value stored at a key , The value starts and ends with the offset ( Including both ) determine . You can use a negative offset to provide an offset from the end of the string .So -1 Express The last character ,-2 Express Penultimate character , And so on .

- This function handles out of range requests by limiting the result range to the actual length of the string .
- As of Redis version 2.0.0, SUBSTR is regarded as deprecated.It can be replaced by
GETRANGEwhen migrating or writing new code.
3.7 GETDEL
Get the value of key and delete the key. This command is similar to GET, except for the fact that it also deletes the key on success (if and only if the key’s value type is a string).
Get the value of the key and delete the key . This command is similar to GET, But it can also delete keys when it succeeds ( If and only if the value type of the key is a string ).

There is a feeling of tearing down bridges ~
3.8 STRLEN
Returns the length of the string value stored at the key . When the key contains a non string value , Will return an error .

3.9 APPEND
If the key already exists and is a string , This command appends the value to the end of the string . If the key doesn't exist , Then create the key and set it to an empty string , therefore APPEND Will be similar to that in this particular case set.

3.10 LCS
Available since: 7.0.0
LCS The command implements the longest general subsequence algorithm . Please note that , This is different from the longest generic string Algorithm , Because the matching characters in the string do not need to be continuous .
for example ,“foo” and “fao” Between LCS yes “fo”, Because two strings are scanned from left to right , So the longest common character set consists of the first “f” And the second “o” form .
To evaluate the similarity of two strings ,LCS Very useful . Strings can represent many things . for example , If the two strings are DNA Sequence ,LCS Two... Will be provided DNA Similarity measure between sequences . If the string represents some text edited by a user , be LCS It can indicate the difference between the new text and the old text , And so on .
Please note that , This algorithm takes O(N*M) Time run , among N Is the length of the first string ,M Is the length of the second string .

Add
All common commands can be passed through the command line client help @xxx The query :
127.0.0.1:6379> help
redis-cli 6.2.6
To get help about Redis commands type:
"help @<group>" to get a list of commands in <group>
"help <command>" for help on <command>
"help <tab>" to get a list of possible help topics
"quit" to exit
To set redis-cli preferences:
":set hints" enable online hints
":set nohints" disable online hints
Set your preferences in ~/.redisclirc
You can also check the official website :https://redis.io/commands/

边栏推荐
- SQL splits strings into tables
- How to become a senior digital IC Design Engineer (4-2) script: file read / write operation realized by Verilog HDL code
- Kuboardv3 and monitoring kit installation
- Share the code technology points and software usage of socket multi client communication
- Interviewer: with the for loop, why do you need foreach??
- Stm32l4xx serial port log configuration analysis
- How to manage device authorization
- How to become a senior digital IC Design Engineer (4-5) script: file comparison operation implemented by shell script
- Scala 基础 (三):运算符和流程控制
- Ambiguity between 0 and 1
猜你喜欢

如何成为一名高级数字 IC 设计工程师(4-3)脚本篇:C 语言实现的文件读写操作

OculusRiftS与Unity.UI的交互(1)-总览

Server antivirus

C language course design - food warehouse management system

直播预告|SQL也能玩转工业级机器学习?MLOps meetup V3带你一探究竟!

SystemVerilog-结构体(一)
![[understanding of opportunity -33]: seeing is not necessarily true. Most of the time,](/img/60/703a5bc3038d28bcf812415032f240.jpg)
[understanding of opportunity -33]: seeing is not necessarily true. Most of the time, "seeing is false"

How to become a senior digital IC Design Engineer (4-5) script: file comparison operation implemented by shell script

【内网穿透】Frp 自建跳板-两个内网通讯上线-反弹shell

Typescript (5) class, inheritance, polymorphism
随机推荐
栈的增删查改---动态内存
Utiliser kubernets Resource Lock pour compléter son application ha
如何成为一名高级数字 IC 设计工程师(6-4)数字 IC 验证篇:测试点分解
The left toolbar of hbuilder is missing
[solution] longest common subsequence
Near's storage stacking
Fibonacci sequence
Adding, deleting, checking and modifying stack - dynamic memory
[TS] type alias
7-27 bubble sorting (the k-th time)
How to become a senior digital IC Design Engineer (6-5) digital IC Verification: coverage collection
How to become a senior digital IC Design Engineer (3-5) tools: Spyglass Technology
Tiflash compiler oriented automatic vectorization acceleration
【Redis】Key的层级结构
How to manage device authorization
okcc呼叫中心的计费方式哪个最好
Research on VB multi-layer firewall technology - state detection
Redis data migration (III)
How to become a senior digital IC Design Engineer (1-1) Verilog coding Grammar: Introduction
Magic Quadrant of motianlun's 2021 China Database