redis string type operation command

Posted by lisa3711 on Sun, 23 Jun 2019 01:05:41 +0200

redis string type operation command
1**set key value [ex ss] | [px sss] [nx | xx]**

ex ss: Set life cycle, per second
px sss: Set up the life cycle in milliseconds
If ex,px cannot be written at the same time
nx: When the key does not exist, set key operates, otherwise nothing is done.
xx: Setkey operation when key exists, otherwise no operation is done

127.0.0.1:6379> set userid1 1101 ex 120
OK
127.0.0.1:6379> ttl userid1
(integer) 117
127.0.0.1:6379> set userid1 1101 px 500000
OK
127.0.0.1:6379> pttl userid1
(integer) 492500
127.0.0.1:6379> set userid1 1101   px 55500 nx
(nil)
127.0.0.1:6379> set userid1 1101   px 55500 xx
OK
127.0.0.1:6379> set userid2 1101   px 55500 nx
OK
127.0.0.1:6379> keys *
1) "userid1"
2) "userid2"
3) "userid"

2 Mset key1 V1 key2 v2... Setting multiple key values at one time
3 mget key1 key2... Keyn Gets the values of multiple keys

127.0.0.1:6379> mset a 1 b 2 c 3
OK
127.0.0.1:6379> mget a b c
1) "1"
2) "2"
3) "3"

4 setrange key offset value
Start at the offset byte of the string and replace it with value

127.0.0.1:6379> set userid 111111
OK
127.0.0.1:6379> setrange userid 2 aa
(integer) 6
127.0.0.1:6379> get userid
"11aa11"

Note: If offset > character length, the character will automatically fill 0x00

127.0.0.1:6379> set userid 111111
OK
127.0.0.1:6379> setrange userid 8 bb
(integer) 10
127.0.0.1:6379> get userid
"111111\x00\x00bb"
127.0.0.1:6379> 

5 append key value
Function: Append value to the original value of key

127.0.0.1:6379> set userid 111111
OK
127.0.0.1:6379> append userid 222
(integer) 9
127.0.0.1:6379> get userid
"111111222"

6 getrange key start stop
Function: Gets the value of the [start, stop] range in the string
Be careful:
For the subscripts of a string, the left number starts at 0 and the right number starts at -1.
2: Start >= length returns an empty string
3: stop > = length, truncated to the end of the character
4: If start > stop, return an empty string

127.0.0.1:6379> set userid 111111
OK
127.0.0.1:6379> set userid 123456
OK
127.0.0.1:6379> getrange userid 0 2
"123"
127.0.0.1:6379> getrange userid -3 -1
"456"
127.0.0.1:6379> getrange userid 0  -1
"123456"

7 getset key newvalue
Function: Get and return the old value and set the new value, which is equivalent to doing get operation first, then set operation.

127.0.0.1:6379> set userid 111111
OK
127.0.0.1:6379> getset userid 222222
"111111"
127.0.0.1:6379> get userid
"222222"

8 incr key
Function: The value of the specified key is added to 1, and the value after adding 1 is returned.
Be careful:
Scope 64 Signed

---Nonexistent key As 0,again incr operation
127.0.0.1:6379> incr aa
(integer) 1
127.0.0.1:6379> get aa
"1"
--incr key operation
127.0.0.1:6379> set userid 111111
OK
127.0.0.1:6379>  incr userid
(integer) 111112

9 incrby key n
The value of the specified key is added n, and the value after adding n is returned.

127.0.0.1:6379> set userid 111111
OK
127.0.0.1:6379> incrby userid 10
(integer) 111121

10 incrbyfloat key floatnumber
The value of the specified key plus float number and returns the value after adding float number

127.0.0.1:6379> set userid 111111
OK
127.0.0.1:6379> incrbyfloat userid 5.5
"111116.5"

11 decr key
The value of the specified key is subtracted by 1, and the value after adding 1 is returned.

127.0.0.1:6379> set userid 111111
OK
127.0.0.1:6379> decr userid 
(integer) 111110

12 decrby key n
The value of the specified key minus N and returns the value after adding n

127.0.0.1:6379> set userid 111111
OK
127.0.0.1:6379> decrby userid 11
(integer) 111100

13 getbit key offset
Function: From the binary representation of the value, return the value on the corresponding offset bit (from left to right)
For example, A-0100 0001

127.0.0.1:6379> set char A
OK
127.0.0.1:6379> getbit char 0
(integer) 0
127.0.0.1:6379> getbit char 1
(integer) 1
127.0.0.1:6379> getbit char 2
(integer) 0
127.0.0.1:6379> getbit char 3
(integer) 0

14 setbit key offset value
Set the offset value on the corresponding binary bit to return the old value on that bit
Be careful:
1: If offset is too large, 0 will be filled in the middle.
2 offset max. 2 ^ 32-1, max. 512M

For example, A-0100 0001

127.0.0.1:6379> set char A
OK
127.0.0.1:6379> setbit char 2  1
(integer) 0
127.0.0.1:6379> get char
"a"

15 bitop operation destkey key1 [key2 ...]
Operate key1, key2. keyN and save the results to destkey.
operation can be AND, OR, NOT, XOR
Note: For NOT operations, key s cannot be multiple

Set a binary bit of userid1 0010 0000

127.0.0.1:6379> setbit userid1 7 0
(integer) 0
127.0.0.1:6379> setbit userid1 2 1
(integer) 0

Set a userid2 to binary bit 0100 0001 corresponding to A

127.0.0.1:6379> set userid2 A
OK

Do or operations on userid1 and userid2

127.0.0.1:6379> bitop or return userid1 userid2
(integer) 1
127.0.0.1:6379> get return
"a"

Topics: Redis