redis basic operation - string

Keywords: ascii Redis

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, Get 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 the 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 key, get the bit on the specified offset

Command: getbit

Format: getbit key offset

  test_key current value "test"_ value_ 2 ", the binary of the ASCII code of the 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 key s

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 the ASCII code of 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

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"
//10s after
127.0.0.1:6379> get test_key_2
(nil)

9, Set the value of key only 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 with the offset offset

Command: 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"
//10s after
127.0.0.1:6379> get key1
(nil)

15, Increase the number 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 value

Command: 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 key to the given floating-point increment value

Command: 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, Reduce the number value stored in the key by one

Command: 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, The value stored by key minus the given decrement

Command: decrby

Format: decrby 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, To specify the value of the key, append the value

Command: 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

Posted by fatmikey on Sun, 31 May 2020 21:54:18 -0700