“This article has participated in the call for good writing activities, click to view: the back end, the big front end double track submission, 20,000 yuan prize pool waiting for you to challenge!”

You have to work really hard to look effortless!

Wechat search public number [long Coding road], together From Zero To Hero!

preface

The String type is the most basic Redis data type and the only one supported by Memcached. The key of Redis is of String type. If the Value is also of String type, it is equivalent to mapping two strings, namely key-value. Here strings are not just strings in the traditional sense, such as “Hello world”, but also JSON, HTML, and so on. Let’s look at Redis String commands.

SET

Available version: >= 1.0.0

Time complexity: O(1)

Set key-value mapping.

If the key already corresponds to a value, then the set will overwrite (including expiration time).

If the expiration time is not specified, it never expires by default.

Set provides optional arguments. Some of the following commands are simplified versions of set with optional arguments.

The command format is as follows:

SET key value [EX seconds|PX milliseconds|EXAT timestamp|PXAT milliseconds-timestamp|KEEPTTL] [NX|XX] [GET]
Copy the code

Optional parameters

  • EX seconds: Set the expiration time to seconds
  • PX MILLISECONDS: Sets the expiration time to milliseconds
  • EXAT timestamp: Set expiration timestamp (in seconds)
  • PXAT snap-timestamp: set the expired timestamp (milliseconds)
  • NX: Saves data only when the key does not exist in the database
  • XX: Data is saved only when the key exists in Redis
  • KEEPTTL: keeps the last remaining expiration time of the key
  • GET: Returns the old value of the key. If the old value of the key does not exist, an error is returned.

The return value

  • OK: indicates the return value of the set command

  • Nil: Value returned if set is not executed successfully due to ‘NX’, ‘XX’ conditions being set

  • When set is used with the optional GET command, there are two return values:

    • Old value: key existed before and returns the corresponding old value

    • Nil: The key did not exist before

Example 1:

#Set key value, return "OK" on success
127.0.0.1:6379> set key1 value1	 
OK

 #Query the value of key1127.0.0.1:6379 > get key1 "value1"
#Query the expiration time. By default, it never expires
127.0.0.1:6379> ttl key1								
(integer) -1


#Value2 corresponding to key1 is set to expire in 60 seconds. In this case, historical data is overwritten
127.0.0.1:6379> set key1 value2 ex 60		
OK

127.0.0.1:6379> get key1
"value2"

#Expiration in 45 seconds
127.0.0.1:6379> ttl key1  
(integer) 45

Copy the code

Example 2:

#Set key1 - value1
127.0.0.1:6379> set key1 value1
OK

#If key1 does not exist, set it to value2.setFailure)
127.0.0.1:6379> set key1 value2 nx
(nil)

#Save only when key2 exists (currently key2 does not exist,setFailure)
127.0.0.1:6379> set key2 value2 xx
(nil)

#Key1,setsuccessful
127.0.0.1:6379> set key1 value2 xx
OK
127.0.0.1:6379> get key1
"value2"

Copy the code

Example 3:

#Setting expiration Time
127.0.0.1:6379> set key3 value3 ex 100
OK
127.0.0.1:6379> ttl key3
(integer) 79

# setThe expiration time of the old value is retained
127.0.0.1:6379> set key3 value4 keepttl
OK
127.0.0.1:6379> ttl key3
(integer) 66

#The get argument returns the old value
127.0.0.1:6379> set key3 value5 get
"value4"
Copy the code

SETNX

Available version: >= 1.0.0

Time complexity: O(1)

SetNX: “SET if Not eXists”, which is saved only when the key does Not exist. It is a simplified version of the SET command after the NX parameter is added.

The command format is as follows:

SETNX key value
Copy the code

Example:

127.0.0.1:6379> setnx mykey "hello"
(integer) 1
127.0.0.1:6379> setnx mykey "world"
(integer) 0
127.0.0.1:6379> get mykey
"hello"
Copy the code

SETEX

Available version: >= 2.0.0

Time complexity: O(1)

Set key-value and add expiration time, which is equivalent to a simplified version of set command with EX parameter.

The command format is as follows:

SETEX key seconds value
Copy the code

SetEX is an atomic operation, equivalent to executing the following command in a transaction:

SET mykey value EXPIRE mykey secondsCopy the code

Example:

127.0.0.1:6379> setex name 60 lifelmy
OK
127.0.0.1:6379> get name
"lifelmy"
127.0.0.1:6379> ttl name
(integer) 54
Copy the code

PSETEX

Available version: >= 2.6.0

Time complexity: O(1)

PSETEX sets the expiration time in milliseconds and SETEX in seconds.

The command format is as follows:

PSETEX key milliseconds value
Copy the code

Example:

#Set the expiration time to 10000 milliseconds (10s)
127.0.0.1:6379> psetex name 10000 lifelmy
OK

#View expiration time (millisecond level)
127.0.0.1:6379> pttl name
(integer) 8288
Copy the code

GET

Available version: >= 1.0.0

Time complexity: O(1)

GET returns the value of the key.

Return nil if key does not exist;

If value is not a string, an error is returned;

The command format is as follows:

GET key
Copy the code

Example:

#Get The non-existent key
127.0.0.1:6379> get notexistkey
(nil)

127.0.0.1:6379> set name hello
OK
127.0.0.1:6379> get name
"hello"
Copy the code

GETSET

Available version: >= 1.0.0

Time complexity: O(1)

Set key-value and return the old value corresponding to the key.

Old value does not exist return nil, old value is not String error.

The command format is as follows:

GETSET key value
Copy the code

Example:

#Clearing the database127.0.0.1:6379 > flushdb OK
#No old value, return nil
127.0.0.1:6379> getset name hello
(nil)

#Return old value "hello"
127.0.0.1:6379> getset name world
"hello"

127.0.0.1:6379> get name
"world"
Copy the code

This command can be used in conjunction with the count command, and when the count is complete, we want to get the final result and clear the count to zero.

127.0.0.1:6379> incr count
(integer) 1
127.0.0.1:6379> incr count
(integer) 2
127.0.0.1:6379> getset count 0
"2"
Copy the code

GETDEL

Version available: >= 6.2.0

Time complexity: O(1)

Obtain the value of the key and delete the key.

Returns nil if key does not exist and value is not String.

The command format is as follows:

GETDEL key
Copy the code

Example:

127.0.0.1:6379> set name lifelmy
OK
127.0.0.1:6379> getdel name
"lifelmy"
127.0.0.1:6379> get name
(nil)
Copy the code

GETEX

Version available: >= 6.2.0

Time complexity: O(1)

Obtain the value of the key and set the expiration time for the key. The expiration time parameter is the same as set.

The command format is as follows:

GETEX key [EX seconds|PX milliseconds|EXAT timestamp|PXAT milliseconds-timestamp|PERSIST]
Copy the code

Example:

127.0.0.1:6379> set name lifelmy
OK
127.0.0.1:6379> get name
"lifelmy"
127.0.0.1:6379> ttl name
(integer) -1

#Getex, set the expiration time to 60 seconds
127.0.0.1:6379> getex name ex 60
"lifelmy"
127.0.0.1:6379> ttl name
(integer) 52
Copy the code

STRLEN

Version available: >= 2.2.0

Time complexity: O(1)

Return the length of the string corresponding to the value of key.

If the key does not exist, return 0. If the value is not a string, an error is reported.

The command format is as follows:

STRLEN key
Copy the code

Example:

127.0.0.1:6379> set name lifelmy OK 127.0.0.1:6379> strlen name (integer) 7 127.0.0.1:6379> strlen notexistKey (integer)  0Copy the code

APPEND

Available version: >= 2.0.0

Time complexity: O(1)

Append value to key;

If the key does not exist, it is appended to the null character.

The command format is as follows:

APPEND key value
Copy the code

Example:

#Delete the key for testing purposes127.0.0.1:6379> del Mykey (integer) 1 127.0.0.1:6379> AppEnd mykey "Hello" (integer) 5 127.0.0.1:6379> AppEnd Mykey" World "(integer) 11 127.0.0.1:6379> get mykey" Hello world"Copy the code

SETRANGE

Version available: >= 2.2.0

Time complexity: O(1)

From the specified offset, change the value corresponding to the key.

If value is less than offset, Redis will fill the data in between with whitespace (value is 2, but offset is 5, so the whitespace between the original character and the offset will be filled with zero bytes \x00).

If key does not exist, value is treated as a blank string.

Because Redis strings are limited to 512 megabytes in size, the maximum offset that can be used is 2^29-1(536870911), and multiple keys can be used if more space is required.

The command format is as follows:

SETRANGE key offset value
Copy the code

Example:

127.0.0.1:6379> set key "hello world"
OK

#Starting at offset 6, replace with "redis"
127.0.0.1:6379> setrange key 6 "redis"
(integer) 11
127.0.0.1:6379> get key
"hello redis"

#Offset is greater than the length of the old value, supplemented with "\x00"127.0.0.1:6379> setrange notexistKey 3 "Hello" (integer) 8 127.0.0.1:6379> get notexistKey "\x00\x00\ x00Hello" 127.0.0.1:6379 >Copy the code

GETRANGE

Available version: >= 2.4.0

Time complexity: O(N), where N is the length of the returned string

In Redis 2.0, the command is SUBSTR

Get the substring corresponding to key at the position specified by value, determined by the offsets start and end (both left and right are closed intervals);

A negative offset means counting from the end of the string, -1 for the last character, -2 for the penultimate character, and so on;

If the offset provided in the request exceeds the actual string length limit, the actual length prevails.

The command format is as follows:

GETRANGE key start end
Copy the code

Example:

127.0.0.1:6379> set mykey "from zero to hero"
OK

127.0.0.1:6379> getrange mykey 0 3
"from"

127.0.0.1:6379> getrange mykey -4 -1
"hero"

#Request out of scope
127.0.0.1:6379> getrange mykey 5 100
"zero to hero"

127.0.0.1:6379> getrange mykey 50 100
""

127.0.0.1:6379> getrange mykey -100 -1
"from zero to hero"

Copy the code

INCR

Available version: >= 1.0.0

Time complexity: O(1)

Increment the value of the key by 1.

If the key does not exist, initialize the value to 0 before performing the INCR operation.

If the key cannot be converted to a number, an error is reported.

Value The value is limited to 64 – bit signed digits.

The command format is as follows:

INCR key
Copy the code

Redis itself does not have an integer, but is a string. When this operation is performed, the string type is converted to a 64-bit signed integer.

Example:

#The key does not exist
127.0.0.1:6379> incr age
(integer) 1
127.0.0.1:6379> get age
"1"

#The key to exist
127.0.0.1:6379> set myage 18
OK
127.0.0.1:6379> incr myage
(integer) 19
127.0.0.1:6379> get myage
"19"

#Value type error
127.0.0.1:6379> set name "lifelmy"
OK
127.0.0.1:6379> incr name
(error) ERR value is not an integer or out of range
Copy the code

INCRBY

Available version: >= 1.0.0

Time complexity: O(1)

Similar to INCR, INCRBY adds the value of a key to an integer instead of always 1.

If the key does not exist, initialize the value to 0 before performing the INCRBY operation.

If the key cannot be converted to a number, an error message is displayed.

Value The value is limited to 64 – bit signed digits.

The command format is as follows:

INCRBY key increment
Copy the code

Example:

#Plus a positive number
127.0.0.1:6379> set age 18
OK
127.0.0.1:6379> incrby age 5
(integer) 23
127.0.0.1:6379> get age
"23"

#Plus a negative number is the same thing as minus
127.0.0.1:6379> incrby age -5
(integer) 18
127.0.0.1:6379> get age
"18"
Copy the code

INCRBYFLOAT

Available version: >= 2.6.0

Time complexity: O(1)

Similar to the INCRBY command, INCRBYFLOAT adds a float to the value of the key. If the value is negative, it subtracts.

If the key does not exist, initialize the value to 0 before performing INCRBYFLOAT.

Value and increment can both be in the form of an exponential (e.g., 5.0e3), but INCRBYFLOAT is always stored in the form of a floating point number (a number, an optional decimal point, and a decimal part of any length), up to 17 decimal places.

The command returns an error when any of the following conditions occur:

  • Value values are not strings (because numbers and floating-point numbers in Redis are stored as strings);
  • Value or the given increment increment cannot be converted to a double – precision floating-point number.

The command format is as follows:

INCRBYFLOAT key increment
Copy the code

Example:

127.0.0.1:6379> set mykey 10.50 OK 127.0.0.1:6379> INCRBYFLOAT mykey 0.1 "10.6" 127.0.0.1:6379> INCRBYFLOAT mykey -0.2 "10.4" 127.0.0.1:6379> INCRBYFLOAT mykey 2.0e2"
#index
127.0.0.1:6379> set mykey 5.0e3
OK
127.0.0.1:6379> INCRBYFLOAT mykey 2.0e2
"5200"
Copy the code

DECR

Available version: >= 1.0.0

Time complexity: O(1)

In contrast to the INCR command, DECR decreases value by one;

If the key does not exist, initialize the value to 0 before performing DECR.

If the key cannot be converted to a number, an error message is displayed.

Value The value is limited to 64 – bit signed digits.

The command format is as follows:

DECR key
Copy the code

Example:

127.0.0.1:6379> set mykey 10
OK
127.0.0.1:6379> DECR mykey
(integer) 9
127.0.0.1:6379> get mykey
"9"

#Out of the representation range of 64 - bit signed integers127.0.0.1:6379 > SET mykey "234293482390480948029348230948" OK 127.0.0.1:6379 > DECR mykey (error) ERR value is not the an integer or out of rangeCopy the code

DECRBY

Available version: >= 1.0.0

Time complexity: O(1)

In contrast to INCRBY, DECRBY subtracts the value of the key from an integer;

If the key does not exist, initialize the value to 0 before DECRBY.

If the value cannot be converted to a number, an error is reported;

Value The value is limited to 64 – bit signed digits.

The command format is as follows:

DECRBY key decrement
Copy the code

Example:

127.0.0.1:6379> set mykey 10
OK
127.0.0.1:6379> decrby mykey 3
(integer) 7

#Subtracting a negative number is the same thing as adding
127.0.0.1:6379> decrby mykey -3
(integer) 10
Copy the code

MSET

Supported version: 1.0.1

Time complexity: O(N), where N is the number of keys

Similar to the SET command, MSET can SET one or more key-values at a time.

If the key already exists, the new value overwrites the old value.

MSET is an atomic operation. All values are set at the same time. There is no case where some keys are set and some keys are not processed.

The command format is as follows:

MSET key value [key value ...]
Copy the code

Example:

127.0.0.1:6379> mset key1 "hello" key2 "world"
OK
127.0.0.1:6379> get key1
"hello"
127.0.0.1:6379> get key2
"world"
Copy the code

MSETNX

Version available: >= 1.0.1

Time complexity: O(N), where N is the number of keys

Similar to SETNX, MSETNX sets one or more values;

The execution succeeds only when all the given keys do not exist. Even if one key exists, all key-values are not set.

MSETNX is an atomic operation. All values are set at the same time. There is no case where some keys are set and some keys are left unprocessed.

The command format is as follows:

MSETNX key value [key value ...]
Copy the code

Example:

127.0.0.1:6379> MSETNX key1 "hello" key2 "world"
(integer) 1

#Key2 already exists, and neither key will be set
127.0.0.1:6379> MSETNX key2 "new world" key3 "lifelmy"
(integer) 0
127.0.0.1:6379> get key2
"world"
127.0.0.1:6379> get key3
(nil)
Copy the code

MGET

Version available: >= 1.0.1

Time complexity: O(N), where N is the number of keys

Similar to the GET command, MGET retrieves one or more keys at a time.

Returns nil if key does not exist.

The command format is as follows:

MGET key [key ...]
Copy the code

Example:

127.0.0.1:6379> mset key1 hello key2 world
OK
127.0.0.1:6379> mget key1 key2 key3
1) "hello"
2) "world"
3) (nil)
Copy the code