当前位置:网站首页>Redis strings command
Redis strings command
2022-06-26 01:01:00 【StoNENeee】
Redis And Strings command
In the previous article, we were introducing Redis The data type of , The basic commands of various data types are introduced , In this article , We will introduce these data type commands in detail
Strings
We know
Strings
Data types can store various strings , Then let's introduce itStrings
Related commands
APPEND
edition : >= 2.0.0
grammar :APPEND key value
Return results : The length of the string after the append operation
semantics :
- If key Already exist , also key The value of is a string , The value is appended to the end of the string ; If the value is not a string , It will give an error prompt :
WRONGTYPE Operation against a key holding the wrong kind of value
- If key non-existent , I'm going to create one key, And set its value to an empty string , Then append the value to the end of the string , The command execution result in this case is equivalent to
set
command
> append k3 v3
because k3 non-existent , So the result is , establish k3, And set its value to an empty string , Then append the value to the end of the string .
> append k3 aaa
because k3 Already exist , So you splice the values directly to the end of the string .
DECR
edition : >= 1.1.0
grammar :DECR key
Return results : reduce 1 After the value of
semantics :
- If key There is , And the value is a string of integer type , Then subtract its value 1. If the value is not a string of integer type , May be an error :
ERR value is not an integer or out of range
. - If key non-existent , Set its value to before execution 0, Then subtract 1.
The command is atomic
> decr k3
ERR value is not an integer or out of range
> decr k4
-1
DECRBY
edition : >= 1.1.0
grammar :DECRBY key decrement
Return results : reduce decrement After the value of
semantics : Be similar to DECR
command
- If key There is , And the value is a string of integer type , Then subtract its value decrement. If the value is not a string of integer type , May be an error :
ERR value is not an integer or out of range
. - If key non-existent , Set its value to before execution 0, Then subtract decrement.
The command is atomic
> decrby myaa 10
-10
GET
edition : >= 1.1.0
grammar :GET key
Return results : key Corresponding value
semantics :
- If key There is , And the value is of string type , Return value , If the value is not a string , False report :
WRONGTYPE Operation against a key holding the wrong kind of value
- If key non-existent , Then return to
null
> set myaa bb
OK
> get myaa
bb
GETDEL
edition : >= 6.2.0
grammar :GETDEL key
Return results : return key Corresponding value
semantics :
- obtain key Value , And will key Delete
- If key non-existent , Then return to null
- If key The value of is not a string , False report :
WRONGTYPE Operation against a key holding the wrong kind of value
> set myaa bb
OK
> getdel myaa
bb
GETEX
edition : >= 6.2.0
grammar :GETEX key [ EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | PERSIST]
Return results : return key Corresponding value
semantics :
- obtain key Value , And set up key The expiration time of .
ex
: Indicates the expiration time in secondspx
: Indicates the expiration time in millisecondspersist
: Indicates to remove the expiration time- If key non-existent , Then return to null
- If key The value of is not a string , False report :
WRONGTYPE Operation against a key holding the wrong kind of value
> set myaa bb
OK
> getex myaa ex 50
bb
GETRANGE
edition : >= 2.4.0
grammar :GETRANGE key start end
Return results : return key The string of a specified range of values
semantics :
- Get the string of the specified range
- start and end Value , It can be either positive or negative , among -1 Represents the first string starting from the tail , And so on ,-2 Is the second string starting from the tail .
- If start and end The range of exceeds the length limit of the string , Then return to
""
> set mykey hello
OK
> getrange mykey 0 -1
hello
> getrange mykey 0 2
hel
> getrange mykey -2 -1
lo
> getrange mykey 6 10
""
GETSET
edition : >= 1.0.0
grammar :GETSET key value
Return results : return key Value
semantics :
- obtain key The old value , And set the new value
The command is atomic
> getset mykey world
hello
INCR
edition : >= 1.0.0
grammar :INCR key
Return results : Add 1 After the value of
semantics :
- If key There is , And the value is a string of integer type , Then add its value 1. If the value is not a string of integer type , May be an error :
ERR value is not an integer or out of range
. - If key non-existent , Set its value to before execution 0, Then add 1.
The command is atomic
> incr k3
ERR value is not an integer or out of range
> incr k4
1
INCRBY
edition : >= 1.0.0
grammar :INCRBY key increment
Return results : Add increment After the value of
semantics : Be similar to INCR
command
- If key There is , And the value is a string of integer type , Then add its value increment. If the value is not a string of integer type , May be an error :
ERR value is not an integer or out of range
. - If key non-existent , Set its value to before execution 0, Then add increment.
The command is atomic
> incrby myaa 10
10
INCRBYFLOAT
edition : >= 2.6.0
grammar :INCRBYFLOATkey increment
Return results : Add increment After the value of
semantics : Be similar to INCRBY
command
- If key There is , And the value is a string of integer type , Then add its value increment. If the value is not a string of integer type , May be an error :
ERR value is not a valid float
. - If key non-existent , Set its value to before execution 0, Then add increment.
Last 0 By default... Will be deleted
The command is atomic
> INCRBYFLOAT myaa 10.020
10.02
LCS
edition : >= 7.0.0
grammar :LCS key1 key2 [LEN] [IDX] [MINMATCHLEN len] [WITHMATCHLEN]
Return results : Evaluate the similarity of two strings
semantics :
- LCS The implementation is the longest common subsequence algorithm
> set k1 mytext1
OK
> set k2 mynewtext2
OK
> lcs k1 k2
mytext
MGET
edition : >= 1.0.0
grammar :MGET key [key ...]
Return results : Returns all specified key Value
semantics :
- Back to all key Value
- For values that are not string type key Or it doesn't exist key, return null
> set k1 v1
OK
> set k2 v2
OK
> lpush k3 v3
1
> mget k1 k2 k3
v1
v2
null
MSET
edition : >= 1.0.1
grammar :MSET key value [ key value ...]
Return results : ok
semantics :
- Batch settings key Value
- If key Already exist , But its value type is not string , The original value will be overwritten
The command is atomic
> mset k1 v11 k2 v22 k3 v33
OK
> mget k1 k2 k3
v11
v22
v33
MSETNX
edition : >= 1.0.1
grammar :MSETNX key value [ key value ...]
Return results : be-all key Set successfully returned 1, Otherwise return to 0
semantics :
- Batch settings key Value , If key non-existent
- As long as there is one key There is , Then the command will not perform any operation
The command is atomic
> msetnx k1 v1 k2 v2
1
> mget k1 k2
v1
v2
> msetnx k2 v22 k3 v3
0
> mget k1 k2 k3
v1
v2
null
PSETEX
edition : >= 2.6.0
grammar :PSETEX key milliseconds value
Return results : ok
semantics :
- Set up key Value , Also set expiration time , The unit is millisecond
> psetex k3 1000 v3
OK
SET
edition : >= 1.0.0
grammar :SET key value [ NX | XX] [GET] [ EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | KEEPTTL]
semantics :
- Set up key Value , And can attach a series of conditions , Such as expiration date ,key Whether there is, etc
- If key Already exist , No matter key What data type is the value of , Its value will be overwritten
- NX: stay key In the absence of , Set up key
- XX: stay key In the presence of , Set up key
- GET: If key The value of is a string type , Then return to key The old value , If key Value of is not of string type , False report , If key non-existent , Then return to null
- EX seconds: Set up key The expiration time of , The unit is seconds
- PX milliseconds: Set up key The expiration time of , In milliseconds
> set k3 v3
OK
# Set expiration time
> set k3 v3 ex 60
OK
SETEX
edition : >= 2.0.0
grammar :SETEX key seconds value
semantics :
- Set up key At the same time , Set expiration time
The command is atomic
> set k3 1000 v3
OK
SETNX
edition : >= 1.0.0
grammar :SETNX key value
semantics :
- If key non-existent , Then set the value , And the return value is 1
- If key There is , The value is not set , And the return value is 0
> setnx k5 v5
1
> setnx k5 v55
0
SETRANGE
edition : >= 2.2.0
grammar :SETRANGE key offset value
semantics :
- from offset The offset starts overwriting the string , If the offset exceeds the length of the string , Then use zero-bytes fill
- Maximum offset offset by 2^29 -1 (536870911), because Redis The string size of is limited to 512MB
> set k6 v6
OK
> setrange k6 1 aaaaaa
7
> get k6
vaaaaaa
> setrange k6 8 bbb
11
> get k6
vaaaaaabbb
STRLEN
edition : >= 2.2.0
grammar :STRLEN key
semantics :
- If key There is , Its value is the string , The length of the string value is returned , If the value is not a string , False report
- If key non-existent , Then return to 0
# key There is , And the value is a string
> strlen k6
11
# key non-existent
> strlen k7
0
That's all Strings The data type command introduces
Last , Welcome to WeChat official account.
边栏推荐
- ciscn_2019_en_2
- Kylin
- 事物/现象/事情/东西/情况/表象
- 多接口调用,使用Promise.all、Promise.race和Promise.any
- Redisson 3.17.4 发布
- Openresty chapter 01 introduction and installation configuration
- 数字电路——加法器
- mysql
- Solve STM32 operation μ Solution to sudden failure of task scheduling in c/os-ii system
- FPGA notes -- implementation of FPGA floating point operation
猜你喜欢
debezium
Analyze the five root causes of product development failure
Ad20 (Altium designer) PCB highlight network
Redisson 3.17.4 release
Balanced binary tree AVL
Openresty chapter 01 introduction and installation configuration
Return value is object type method call equals()
STL tutorial 5-basic concepts of STL and the use of string and vector
Anaconda beginner's notes
统一网关Gateway
随机推荐
Atlas200dk刷机
FPGA notes -- implementation of FPGA floating point operation
Login interceptor
关于EF翻页查询数据库
关于HC-12无线射频模块使用
Idea kotlin version upgrade
Typescript for Web Learning
Middle order clue binary tree
Openresty chapter 01 introduction and installation configuration
No executorfactory found to execute the application
经典面试题之老鼠试药与汉明码
Dynamic verification code
JS逆向案例:破解登录密码
Correct writing methods of case, number and punctuation in Chinese and English papers
4 key points to help the product manage the project well
1-11Vmware虚拟机常见的问题解决
[understanding of opportunity -30]: Guiguzi - internal "chapter - empathy, stand on the other side's position and narrow the psychological distance with the other side
1-9network configuration in VMWare
Web学习之TypeScript
Classic interview questions: mouse drug test and Hamming code