07 Appendix More String Operation Commands

07 Appendix More String Operation Commands #

Expiring Key-Value Pair Operations #

a. Add Key-Value Pair and Set Expiration Time #

Syntax: set key value [expiration EX seconds|PX milliseconds] [NX|XX] Example:

127.0.0.1:6379> set k1 val1 ex 1000
OK

Set the key-value pair k1=val1 with an expiration time of 1000 seconds. You can check the expiration time of a key by using the ttl key command, as shown below:

127.0.0.1:6379> ttl k1
(integer) 997

b. Set String Value and Expiration Time (in seconds) #

Syntax: setex key seconds value Example:

127.0.0.1:6379> setex k1 1000 v1
OK
127.0.0.1:6379> ttl k1
(integer) 999
127.0.0.1:6379> get k1
"v1"

If the key already exists, the setex command will overwrite the old value.

c. Set String Value and Expiration Time (in milliseconds) #

Similar to setex, but the psetex command sets the expiration time in milliseconds. Syntax: psetex key milliseconds value Example:

127.0.0.1:6379> psetex k1 100000 v11
OK
127.0.0.1:6379> ttl k1
(integer) 97
127.0.0.1:6379> get k1
"v11"

Advanced String Operations #

a. Substring Extraction based on Specified Range #

Syntax: getrange key start end Example:

127.0.0.1:6379> get hello
"hello world"
127.0.0.1:6379> getrange hello 0 4
"hello"
127.0.0.1:6379> getrange hello 0 -1
"hello world"
127.0.0.1:6379> getrange hello 0 -2
"hello worl"

Negative numbers count from the end of the string, -1 represents the last character, -2 represents the second last character, and so on.

b. Set New String Value and Return Old Value #

Syntax: getset key value Example:

127.0.0.1:6379> get db
"redis"
127.0.0.1:6379> getset db mysql
"redis"
127.0.0.1:6379> get db
"mysql"

When using the getset command, if the key is not a string, an error will be thrown, as shown below:

127.0.0.1:6379> type myset
set
127.0.0.1:6379> getset myset v1
(error) WRONGTYPE Operation against a key holding the wrong kind of value

The type command can be used to determine the data type of a key, and if it is not a string, using the getset command will result in an error.

c. Set Key-Value Pair (Create) when Key Does Not Exist #

If the key already exists, the command will be ineffective and will not modify the original value. Otherwise, a new key-value pair will be created. Syntax: setnx key value Example:

127.0.0.1:6379> setnx k9 v9
(integer) 1
127.0.0.1:6379> get k9
"v9"
127.0.0.1:6379> setnx k9 v99
(integer) 0
127.0.0.1:6379> get k9
"v9"

d. Set One or More Key-Value Pairs when All Keys Do Not Exist #

Syntax: msetnx key value [key value …] Example:

127.0.0.1:6379> msetnx k5 v5 k6 v6
(integer) 1
127.0.0.1:6379> mget k5 k6
1) "v5"
2) "v6"

Note: msetnx is an atomic operation, so if one operation fails, all other operations will also fail. For example, if one key already exists, all key-value pairs will fail to be set, as shown below:

127.0.0.1:6379> get k1
"val1"
127.0.0.1:6379> get k8
(nil)
127.0.0.1:6379> msetnx k1 v1 k8 v8
(integer) 0
127.0.0.1:6379> get k1
"val1"
127.0.0.1:6379> get k8
(nil)

e. Substring Replacement and Value Assignment #

Syntax: setrange key offset value Example:

127.0.0.1:6379> get hello
"hello java"
127.0.0.1:6379> setrange hello 6 redis
(integer) 11
127.0.0.1:6379> get hello
"hello redis"

If the key to be replaced does not exist, it will be treated as an empty string, as shown below:

127.0.0.1:6379> setrange mystr 3 mystring
(integer) 11
127.0.0.1:6379> get mystring
(nil)

These commands mentioned above cover most of the string operations. Some of them are not commonly used but are very useful, such as the setnx command. This command is ineffective if the key already exists and does not overwrite the existing value. It only creates a new key-value pair if the key does not exist.