当前位置:网站首页>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 it Strings 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 seconds
  • px: Indicates the expiration time in milliseconds
  • persist: 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

 Insert picture description here

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.

原网站

版权声明
本文为[StoNENeee]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206252257066576.html