Redis basic operation – string

Time:2021-8-25

1、 Set the value of the specified key

Commands: Set

Format: set key value

127.0.0.1:6379> set test_key test_value
OK

2、 Gets the value of the specified key

Command: get

Format: get key

127.0.0.1:6379> get test_key
"test_value"

3、 For the string value stored in key, obtain the substring in the specified interval

Command: getrange

Format: getrange key start end

Note: start is the start index, end is the end index, and index starts from 0

127.0.0.1:6379> GETRANGE test_key 2 3
"st"
127.0.0.1:6379> getrange test_key 1 3
"est"
127.0.0.1:6379> getrange test_key 0 3
"test"
127.0.0.1:6379> getrange test_key 3 4
"t_"

4、 Set the value of the given key to value and return the old value of the key.

Command: GetSet

Format: GetSet key value

127.0.0.1:6379> getset test_key test_value_2
"test_value"
127.0.0.1:6379> get test_key
"test_value_2"

5、 For the string value stored by the key, obtain the bit on the specified offset

Command: getbit

Format: getbit key offset

test_ Key current value “test”_ value_ 2 “, the binary of ASCII code with initial” t “is 0111 0100, so:

127.0.0.1:6379> getbit test_key 0
(integer) 0
127.0.0.1:6379> getbit test_key 1
(integer) 1
127.0.0.1:6379> getbit test_key 2
(integer) 1
127.0.0.1:6379> getbit test_key 3
(integer) 1
127.0.0.1:6379> getbit test_key 4
(integer) 0
127.0.0.1:6379> getbit test_key 5
(integer) 1
127.0.0.1:6379> getbit test_key 6
(integer) 0
127.0.0.1:6379> getbit test_key 7
(integer) 0

6、 Gets the value of all (one or more) given keys

Commands: mget

Format: mget key1 [key2]

127.0.0.1:6379> set key1 value1
OK
127.0.0.1:6379> set key2 value2
OK
127.0.0.1:6379> set key3 value3
OK
127.0.0.1:6379> mget key1 key2 key3
1) "value1"
2) "value2"
3) "value3"

7、 For the string value stored by key, set or clear the bit on the specified offset

Command: setbit

Format: setbit key offset value

test_ Key current value “test”_ value_ 2 “, the binary of ASCII code with the initial” t “is 0111 0100. If the 6th bit is set to 1, the value is 0111 0110, corresponding to the letter” V ““

127.0.0.1:6379> get test_key
"test_value_2"
127.0.0.1:6379> setbit test_key 6 1
(integer) 0
127.0.0.1:6379> get test_key
"vest_value_2"

8、 Set the value of the specified key, and set the expiration time of the key to seconds (in seconds)

Command: setex

Format: setex key seconds value

127.0.0.1:6379> setex test_key_2 10 test_value
OK
127.0.0.1:6379> get test_key_2
"test_value"
127.0.0.1:6379> get test_key_2
"test_value"
//After 10s
127.0.0.1:6379> get test_key_2
(nil)

9、 The key value can only be set when the key does not exist

Command: setnx

Format: setnx key value

127.0.0.1:6379> get test_key
"vest_value_2"
127.0.0.1:6379> setnx test_key test_value
(integer) 0
127.0.0.1:6379> get test_key
"vest_value_2"
127.0.0.1:6379> setnx test_key_3 test_value_3
(integer) 1
127.0.0.1:6379> get test_key_3
"test_value_3"

10、 Overwrite the string value stored by the given key with the value parameter, starting from the offset

Commands: setrange

Format: setrange key offset value

127.0.0.1:6379> set test_key test_value
OK
127.0.0.1:6379> get test_key
"test_value"
127.0.0.1:6379> setrange test_key 3 a
(integer) 10
127.0.0.1:6379> get test_key
"tesa_value"
127.0.0.1:6379> setrange test_key 3 abcde
(integer) 10
127.0.0.1:6379> get test_key
"tesabcdeue"

11、 Returns the length of the string value stored by the key

Command: strlen

Format: strlen key

127.0.0.1:6379> get test_key
"tesabcdeue"
127.0.0.1:6379> strlen test_key
(integer) 10

12、 Set one or more key value pairs at the same time

Command: Mset

Format: Mset key value [key value]

127.0.0.1:6379> mset key1 value1 key2 value2 key3 value3
OK
127.0.0.1:6379> mget key1 key2 key3
1) "value1"
2) "value2"
3) "value3"

13、 Set one or more key value pairs at the same time if and only if all given keys do not exist

Command: msetnx

Format: msetnx key value [key value]

127.0.0.1:6379> mset key1 v1 key2 v2 key3 v3 key4 v4
OK
127.0.0.1:6379> mget key1 key2 key3 key4
1) "v1"
2) "v2"
3) "v3"
4) "v4"
127.0.0.1:6379> msetnx key1 value1 key2 value2 key3 value3 key4 value4 key5 value5
(integer) 0
127.0.0.1:6379> mget key1 key2 key3 key4 key5
1) "v1"
2) "v2"
3) "v3"
4) "v4"
5) (nil)
127.0.0.1:6379> msetnx key5 v5 key6 v6
(integer) 1
127.0.0.1:6379> mget key1 key2 key3 key4 key5 key6
1) "v1"
2) "v2"
3) "v3"
4) "v4"
5) "v5"
6) "v6"

14、 Set the value of the specified key, and set the expiration time of the key to MS (in milliseconds)

Command: psetex

Format: psetex key MS value

127.0.0.1:6379> psetex key1 10000 value
OK
127.0.0.1:6379> get key1
"value"
127.0.0.1:6379> get key1
"value"
//After 10s
127.0.0.1:6379> get key1
(nil)

15、 Increment the numeric value stored in the key by one

Command: incr

Format: incr key

127.0.0.1:6379> set int_key 1
OK
127.0.0.1:6379> get int_key
"1"
127.0.0.1:6379> incr int_key
(integer) 2
127.0.0.1:6379> get int_key
"2"
127.0.0.1:6379> incr int_key
(integer) 3
127.0.0.1:6379> get int_key
"3"

16、 Add the value stored by the key to the given increment

Commands: incrby

Format: incrby key increment

127.0.0.1:6379> get int_key
"3"
127.0.0.1:6379> incrby int_key 10
(integer) 13
127.0.0.1:6379> get int_key
"13"

17、 Add the value stored by the key to the given floating-point increment value

Commands: incrbyfloat

Format: incrbyfloat key increment

127.0.0.1:6379> set float_key 1
OK
127.0.0.1:6379> incrbyfloat float_key 0.5
"1.5"
127.0.0.1:6379> get float_key
"1.5"

18、 Subtract the numeric value stored in the key by one

Commands: decr

Format: decr key

127.0.0.1:6379> get int_key
"13"
127.0.0.1:6379> decr int_key
(integer) 12
127.0.0.1:6379> get int_key
"12"
127.0.0.1:6379> decr int_key
(integer) 11
127.0.0.1:6379> get int_key
"11"

19、 Key subtracts the given decrement from the stored value

Commands: decrby

Format: Derby key increment

127.0.0.1:6379> get int_key
"11"
127.0.0.1:6379> decrby int_key 5
(integer) 6
127.0.0.1:6379> get int_key
"6"

20、 Append value to the value of the specified key

Commands: append

Format: append key value

127.0.0.1:6379> set key1 value1
OK
127.0.0.1:6379> get key1
"value1"
127.0.0.1:6379> append key1 value2
(integer) 12
127.0.0.1:6379> get key1
"value1value2"
127.0.0.1:6379> set int_key 1
OK
127.0.0.1:6379> get int_key
"1"
127.0.0.1:6379> append int_key 1
(integer) 2
127.0.0.1:6379> get int_key
"11"
127.0.0.1:6379> incrbyfloat int_key 0.5
"11.5"
127.0.0.1:6379> get int_key
"11.5"
127.0.0.1:6379> append int_key 1
(integer) 5
127.0.0.1:6379> get int_key
"11.51"

 

 

Reference link:https://www.runoob.com/redis/redis-strings.html