Summary of common commands of the Redis string type
- 2020-05-10 23:09:25
- OfStack
Redis string type
The string type is the most basic data storage type in Redis. It is binary safe in Redis, which means it can accept data in any format, such as JPEG image data or Json object description information. The maximum length of data that Value of string type can hold in Redis is 512M.
1. The simplest commands
1. Get the list of key names that meet the rules
keys *
The * sign here means to list all the keys. At the same time, the * sign can be replaced with other glob-style wildcard formats. The specific rules are as follows:
; : matches 1 character
* : matches any number of characters (including 0)
[] : matches any number of characters between the brackets. You can use "-" to indicate the range, such as [a-z]
\x: match character x for escape, if need to match question mark, need \? To match
Note 1: the redis command is case-insensitive, and the keys command is required to traverse all the keys in Redis, which is expensive when there are many keys. So use as little as possible in a production environment.
Ex. :
redis 127.0.0.1:6379> set name "joan"
OK
redis 127.0.0.1:6379> get name
"joan"
redis 127.0.0.1:6379> set aaa 1
OK
redis 127.0.0.1:6379> set bbb 2
OK
redis 127.0.0.1:6379> keys *
1) "aaa"
2) "name"
3) "bbb"
2. Determine whether the key exists
exists foo
If there is a return certificate type 1, otherwise return 0
Ex. :
redis 127.0.0.1:6379> exists name
(integer) 1
redis 127.0.0.1:6379> exists n
(integer) 0
3. Delete key
del foo
Return is the number of deleted keys, here also can be a time to delete more than one key, just space and key can be. Such as:
del foo bar
If you delete a key, it no longer exists and it returns 0.
Ex. :
redis 127.0.0.1:6379> keys *
1) "aaa"
2) "name"
3) "bbb"
redis 127.0.0.1:6379> del aaa bbb
(integer) 2
redis 127.0.0.1:6379> del aaa
(integer) 0
4. Get the data type of the key value
type foo
The data types of redis are: string (string type), hash (hash type), list (list type), set (collection type), zset (ordered collection type).
The commands for each data type are described in detail below.
2. String type
The string type is the most basic data type in Redis. It can store any type of string, including binary data. The string type is also the basis for the other four data types. That is, the other four data types are all composed of string types.
Common commands are as follows:
set key value
# assignment, and the return value is OK
get key
#, the return value is the value of the corresponding key
incr key
# increments the number. Although it is a string type, it can be incremented by this command if it can be an integer. The return value is the incremented value.
Ex. :
redis 127.0.0.1:6379> get age
"32"
redis 127.0.0.1:6379> incr age
(integer) 33
redis 127.0.0.1:6379> get age
"33"
incrby key number
The # command is similar to the previous one, except that you can specify the amount of growth via number, and the return value is the same as the growth value
Ex. :
redis 127.0.0.1:6379> incrby age 5
(integer) 38
redis 127.0.0.1:6379> get age
"38"
decr key
# decrementing a number, similar to an increment, which I won't explain too much here
decrby key number
# is similar to incrby, and as you can probably guess, decrby key number and incrby key-number means 1
incrbyfloat key floatnumber
# increments the specified floating point number
append key value
# appends a value to the end, such as append foo "value", which is quoted to indicate that there is space, and returns a value of the total length of the value
Ex. :
redis 127.0.0.1:6379> set name "joan"
OK
redis 127.0.0.1:6379> get name
"joan"
redis 127.0.0.1:6379> set aaa 1
OK
redis 127.0.0.1:6379> set bbb 2
OK
redis 127.0.0.1:6379> keys *
1) "aaa"
2) "name"
3) "bbb"
0
strlen key
# gets the total length of the string value. The return value is the length. If key does not exist, it returns 0
mget key [key ...]
# gets a list of multiple key values at the same time
Ex. :
redis 127.0.0.1:6379> set name "joan"
OK
redis 127.0.0.1:6379> get name
"joan"
redis 127.0.0.1:6379> set aaa 1
OK
redis 127.0.0.1:6379> set bbb 2
OK
redis 127.0.0.1:6379> keys *
1) "aaa"
2) "name"
3) "bbb"
1
get key
0
# successfully returns OK by setting multiple key values at the same time
redis 127.0.0.1:6379> set name "joan"
OK
redis 127.0.0.1:6379> get name
"joan"
redis 127.0.0.1:6379> set aaa 1
OK
redis 127.0.0.1:6379> set bbb 2
OK
redis 127.0.0.1:6379> keys *
1) "aaa"
2) "name"
3) "bbb"
2
setbit/getbit
# this is a bit operation, as shown below:
redis set foo bar
OK
At this time, the value assigned to foo is bar, and the corresponding ASCII code is 98, 97, and 114. The conversion to 8-bit 2-base system is:
01100010, 01100001, 01110010
Perform the following operations:
redis 127.0.0.1:6379> set name "joan"
OK
redis 127.0.0.1:6379> get name
"joan"
redis 127.0.0.1:6379> set aaa 1
OK
redis 127.0.0.1:6379> set bbb 2
OK
redis 127.0.0.1:6379> keys *
1) "aaa"
2) "name"
3) "bbb"
4
So getbit is just getting the base 2 value (0 or 1) in the specified position of the string type key value corresponding to key, with the index starting at 0.
If the fetch exceeds the maximum value, it also returns 0.
Instead, setbit is used to set the base 2 value of the specified location for key. The return value is the old value for that location. For example,
redis 127.0.0.1:6379> set name "joan"
OK
redis 127.0.0.1:6379> get name
"joan"
redis 127.0.0.1:6379> set aaa 1
OK
redis 127.0.0.1:6379> set bbb 2
OK
redis 127.0.0.1:6379> keys *
1) "aaa"
2) "name"
3) "bbb"
5
If the value to be set exceeds the maximum length, the command sets all unset values between the maximum length and the maximum length to 0. Similarly, setting a nonexistent key value automatically sets all unset positions in the base 2 in front of it to 0. Such as:
redis 127.0.0.1:6379> set name "joan"
OK
redis 127.0.0.1:6379> get name
"joan"
redis 127.0.0.1:6379> set aaa 1
OK
redis 127.0.0.1:6379> set bbb 2
OK
redis 127.0.0.1:6379> keys *
1) "aaa"
2) "name"
3) "bbb"
6
The corresponding value for qqq is changed to 00000001.
bitcount key
The command can return the number of base 2 values of 1 in a string type key value.
At the same time, his parameters can also set the query range,
bitcount foo 0 1
# means only the number of bytes containing a base 2 value of 1 in ba.
The last command, bitop, allows for bit-manipulation.
bitop OR res foo1 foo2
# means you take foo1 and foo2 and do OR, and the result is stored in res. The bit operations supported by bitop include ND, OR, XOR and NOT.
That's all for the string operation commands. Bit commands are useful, for example, we can set the gender type to 1 bit when storing the gender, which is super space saving.
conclusion
The above is the entire content of this article, I hope to help you with your study or work, if you have any questions, you can leave a message to communicate.