Redis common commands (4) Database management, key management, subscription and publication
# Database Management
Keys pattern # lookup key, parameter wildcard lookup
Keys * # View all keys
Keys n * # View all keys that start with n
Keys * e # View all keys ending in e
Keys h?llo
Keys h[ae] llo
Exists name # check whether the key name exists. If it exists, it is 1. If it does not exist, it is 0.
Type key # check the type of value corresponding to the key
Type name
Del key1 key2... # Delete key and its corresponding value
Del addr
Rename key newkey # changes the name of the key from key to newkey, and overrides if newkey already exists
Rename num1 num3
Renamenx key newkey # changes the name of the key from key to newkey, and does not take action if newkey already exists
Renamenx num3 num2
# sort lists, collections, and ordered collections by calling the sort command
Sort key [by pattern] [limit offset count] [get pattern [get pattern]] [asc | desc] [alpha] [store destination]
Rpush numbers 9 5 1 3 2
Sort numbers # by default the sort command interprets the values contained in the key as floating-point numbers, and then sorts the floating-point numbers
Sort numbers asc
Sort numbers desc
Sadd names peter jack tom
Sort names alpha # uses alpha to have the sort command sort text based on lexicographic order
Sort numbers limit 0 3 # does not skip any values and returns the first three values
Sort numbers limit 3 3 # escapes the first three values and returns the next three values
Sort numbers store sorted-numbers # by specifying store destkey, we store the sorting results in destkey, and the sorted results are stored as a list
Randomkey # randomly returns a key from the current database, and the returned key will not be deleted
The # scan command progressively traverses the entire database multiple times and returns keys that match a given pattern
# cursor is a cursor used for traversal. To start a new traversal, you need to set cursor to 0. Every time you call scan, the command returns a new cursor value. To call scan again, you need to enter the entire cursor value.
# match pattern is used to specify the pattern to match
# count number specifies the maximum number of keys to be returned for this traversal
Scan cursor [match pattern] [count number]
Scan 0
Sscan key cursor [match pattern] [count number] # traverses the elements contained in the collection instead of the smembers command that may block the server
Sscan names 0
Hscan key cursor [match pattern] [count number] # traverses the key-value pairs contained in the hash instead of the hgetall command that may block the server
Hscan daiby::info 0
Zscan key cursor [match pattern] [count number] # traverses the elements contained in an ordered collection instead of the zrange command that may block the server
Zscan "blog::paging" 0
Dbsize # returns the number of key-value pairs currently contained in the database
Flushdb # deletes all key-value pairs contained in the current database
Select num # switching databases
Move key target-db # moves the key in the current database to the target database. If the target database already has a key with the same name, no action will be taken.
Move numbers 1
Flushall # Delete key-value pairs in all redis databases
# key expiration function
The function of # expire and pexpire is to make the key be deleted after N seconds or N milliseconds
The function of # expireat and pexpireat is to delete the key after the specified Unix time arrives.
Expire key seconds # sets the key lifetime in seconds. If no expiration time is specified, it persists until it is removed using DEL
Expire name 5
Pexpire key milliseconds # sets the key lifetime in milliseconds. One second equals 1000 milliseconds. By default, redis checks whether the key expires at a time.
Pexpire msg 5500
Expireat key timestamp # sets the expiration time of the key and specifies the second Unix timestamp
Expireat msg 100000005
Pexpireat key milliseconds-timestamp # sets the key expiration time and specifies the millisecond Unix timestamp
Pexpireat msg 100000000000005
Pttl key # View remaining time to live in milliseconds
Ttl key # View the remaining time to live in seconds
Ttl age
Persist # Delete time to live or expire
Persist msg
Setex key seconds value # sets key value and expiration time in seconds
Set age 5 18
Psetex key millisecond value # sets the key value and expiration time in milliseconds, which is equivalent to executing the set and pexpire commands
Set age 5000 18
# publish and subscribe
Subscribe channel [channel...] # subscribe to a given channel or channels
Subscribe news::it
Psubscribe pattern [pattern...] # subscribes to one or more modes, and the pattern parameter can contain glob-style matches (*, [],?)
Psubscirbe news:: [ie] t
# the behavior of the unsubscription command varies from client to client. Redis-cli exits the client directly to unsubscribe, while Python and ruby need to display the execution command.
Unsubcribe [channel | [channel...]] # unsubscribe to the specified channel, or unsubscribe to all subscribed channels if not specified
Punsubcribe [pattern | [pattern...]] # Unsubscribe the specified mode, or unsubscribe all subscribed modes if no one is specified
Channels # displays subscribed channels
Publish channel message # sends the message to the specified channel, and the command returns the number of subscribers who have received the message
Pubsub channels [pattern] # lists channels with at least one subscriber
Pubsub numsub [channel...] # returns the number of subscribers for a given channel
Pubsub numpat # returns the number of patterns subscribed to