Library instruction

1. The switch

Select the library numberCopy the code

2. Clear the current library

flushDB
Copy the code

3. Clear all libraries

flushAll
Copy the code

Operation Key instruction

Redis is a key-value in-memory database. There are many types of Value (String, List, Set, ZSet, Hash). Before learning Value, we need to know some KEY-related APIS.

1. The delete Key

You can delete one or more keys

del key [key...]
Copy the code

2. Check whether the Key exists

Key returns 1 if it exists and 0 if it does not

exists key 
Copy the code

If one Key exists, return 1

exists key [key...]
Copy the code

3. Set the expiration time

By default, keys are permanently stored. If the expiration time is set, keys are automatically deleted after the specified expiration time.

The unit is seconds

expire key seconds
Copy the code

4. Key set expiration time (ms)

The unit of expiration time is ms

pexpire key millseconds
Copy the code

5. Look for the Key

The keys directive works with wildcards to implement filtering.

Find all keys (* can match zero or more characters)

keys *
Copy the code

Find “hex”, “hax”, “HXX”, etc. Matches only 1 character strictly.

keys h? xCopy the code

Find “heax”, “haex”, “haax”, “heex” ([ae] can only be a or E)

keys h[ae][ae]x
Copy the code

6. Move the Key

Moves a Key from the current library to the specified library.

move key index
Copy the code

7. Check the Key expiration time

Check how long the Key has to expire.

Check the expiration time unit: s

Returns -1 for keys that are permanently stored and -2 for keys that are not stored.

ttl key
Copy the code

8. Check the Key expiration time

The unit of expiration time is ms

pttl key
Copy the code

9. Obtain keys randomly

Retrieves an unexpired Key randomly from the current library.

randomkey
Copy the code

10. The Key name

Only the Key is changed, and the corresponding Value remains the same.

rename key newKey
Copy the code

11. Check the Key type

type key
Copy the code

None indicates that the Key does not exist. Normally, String, List, Set, ZSet, Hash is mandatory.

Type String

1. Memory model

2. Common commands

The command instructions The sample
set Set a pair of key-values set key value
get Obtain the corresponding Value by Key get key
mset Example Set more key-value mset key1 value1 key2 value2 [key value …]
mget Obtain multiple values from multiple keys mget key1 key2 [key …]
getset Get the original Value of the Key and assign a new Value getset key value
strlen View the length of the corresponding Value by Key strlen key
append Append content to a Value by Key append key content
getrange Truncate part of Value by subscripts of Key and Value (subscripts start at 0 and close left and close right) getrange key startIndex endIndex
setex Set a pair of key-values and specify the expiration time (unit: s) setex key seconds value
psetex Set a pair of key-values and specify expiration time (unit: ms) setex key millseconds value
setnx Set a pair of key-values. If the Key does not exist (not), the setting succeeds. Otherwise, the setting fails setnx key value
msetnx If multiple key-value pairs are set, the setting succeeds if all keys do not exist. Otherwise, the setting fails mset key value [key value …]
decr Subtract 1 from the Value of the String by Key decr key
decrby Subtracting the Value of a String by Key decrby key decrement
incr Increments the Value of a String by Key incr key
incrby Adds a specified number to a String Value by Key incrby key increment
incrbyfloat Specified floating-point addition to String Value by Key (up to 17 bits reserved) incrbyfloat key increment

List command

Characteristics of 1.

The values stored in the List are ordered and can be duplicated.

The leftmost part of the List is the header, and the rightmost part is the tail.

You can logically build a Stack and Queue by combining the apis provided by a List.

2. Memory model

3. Common commands

The command instructions The sample
lpush Place multiple values in a List (from left to right). Create a List if no keys exist lpush key value [value …]
lpushx Put the Value into the List from the left. The Key must exist lpushx key value
rpush Place multiple values in a List (from right to left). Create a List if no keys exist rpush key value [value …]
rpushx To put a Value into a List from the right, the Key must exist rpushx key value
lpop Get the List header Value and remove the List header Value lpop key
rpop Get the Value at the end of the List and remove the Value at the end of the List rpop key
lrange Retrieve Value from partial List by subscript (Value subscript 0) lrange key startIndex stopIndex
llen Query the number of values in the List (counting from 1) llen key
lset Modify Value by subscript (subscript must exist) lset key index value
lindex Get Value by subscript lindex key index
lrem Delete a specified number of duplicate values (from left to right) lrem key count value
ltrim By using the subscript of the List, only certain values are retained and all other values are deleted ltrim key startIndex stopIndex
linsert Inserts a new Value before or after matching the specified Value in the List (matching from left to right) linsert key BEFORE | AFTER target value

Set command

Characteristics of 1.

The values stored in the Set are out of order and cannot be repeated.

2. Memory model

3. Common commands

The command instructions The sample
sadd Put several values into a Set (duplicate values overwrite), and create a Set if the Key does not exist sadd key value [value …]
smembers View all values in the Set smembers key
scard Returns the number of values in a Set scard key
spop Returns a random Set of values and removes them spop key count
smove Move a Value from one Set to another smove source destination value
sismember Check whether the Value exists in the Set sismember key value
srandmember Returns a random number of values in a Set srandmember key [member]
sdiff View all values of the first set after the difference set with the other sets sdiff key [key …]
sinter View all values that intersect the first collection with other collections sinter key [key …]
sunion View all values after the first set is joined with other sets (the result is de-duplicated) sunion key [key …]
srem Delete several values from a Set srem key value [value …]

ZSet command

Characteristics of 1.

The values stored in a ZSet are unordered and cannot be repeated, but they can be sorted according to certain criteria (think of as sortable sets).

The principle of ZSet sorting is as follows: when a Value is stored in ZSet, ZSet will set a score for the Value. When sorting, the Value will be sorted according to the score size, and each Value will be assigned a subscript according to the sorting order.

2. Memory model

3. Common commands

The command instructions The sample
zadd Put several values and their scores into a ZSet (duplicates are overwritten), and create a ZSet if the Key does not exist zadd key score value [score value …]
zcard Check the number of values in a ZSet zcard key
zrange Through the subscript of ZSet, the Value of part of ZSet is obtained in ascending order (the Value with the lowest score is 0), and the score can be displayed zrange key startIndex stopIndex [withscores]
zrevrange Through the subscript of ZSet, the Value of part of ZSet is obtained in descending order (the Value with the lowest score is 0), and the score can be displayed zrevrange key startIndex stopIndex [withscores]
zrangebyscore Through the score of ZSet, the Value of part of ZSet can be obtained in ascending order, and the score can be displayed in pages zrangebyscore key min max [withscores] [limit offset count]
zrank View the ascending ranking of a Value in ZSet (starting from 0) zrank key value
zrevrank View the descending ranking of a Value in ZSet (starting from 0) zrevrank key value
zscore View the score of a Value in ZSet zscore key value
zrem Delete several values from ZSet zrem key value [value …]
zincrby Add the specified number to a ZSet Value zincrby key increment value

Hash command

Characteristics of 1.

Hash is similar to the Java Map structure. Fields in a Map cannot be repeated.

2. Memory model

3. Common commands

The command instructions The sample
hset Add a pair of field-values to the Hash, creating the Hash if the Key does not exist (overwriting the Field if it does exist) hset key field value
hget Gets a Value from the Hash hget key field
hgetall View all field-values in the Hash hgetall key
hdel Delete several pairs of field-values from the Hash hdel key field [field …]
hexists Check whether the Field exists in the Hash hexists key field
hkeys View all fields in the Hash hkeys key
hvals View all values in the Hash hvals key
hmset Add pairs of field-values to the Hash (overwrite if the Field exists) hmset key filed value [field value …]
hmget View several values through Field hmget key filed [field …]
hsetnx Add a pair of field-values to Hash, create Hash if Key does not exist (add Hash if Field does exist) hsetnx key field value
hincrby Add a specified number to Value in the Hash hincrby key field increment
hincrbyfloat Add the specified floating-point number to the Hash Value (up to 17 bits) hincrbyfloat key field increment