Linux redis commonly used commands

A problem was encountered on the project recently. The project was undergoing a domain name update recently. After the replacement of the new domain name was completed, it was found that the videos and pictures displayed in the front-end query list could not be displayed correctly. After the investigation, it was determined that the old domain name was still stored in the redis cache. Therefore, a wave of redis commands was checked in linux.

1. Log in to the redis command line

1. Login command: ./redis-cli (if redis has multiple ports, use ./redis-cli -p 6380  , 6380 is the corresponding port number)

When we want to enter the redis command line from linux, we first need to cd into the corresponding redis-cli directory. Then you can use ./redis-cli to enter the redis command line. Examples are as follows:

2. Authentication command auth password

When we use the login command, some redis requires password verification to have the authority to perform various command operations. At this time, we use the auth command, and the following parameter is your redis password. At this point, you can successfully log in to the corresponding redis to perform command operations. Examples are as follows:

3. Exit the command quit

When we complete the redis command, exit the redis command line command. Examples are as follows:

Two. Redis commonly used command description

1. Find all keys: keys *

2. Fuzzy matching to find the corresponding key: keys user* (indicating to find all keys with a prefix of user, * is a wildcard, indicating to replace any number of all characters)

3. Assign the value jony to the value of the user in redis, which can be used as a new addition and update: set user jony (indicating to set a key to user, value to jony data)

4. Obtain the value of value by key: get user (user represents key)

5. Set the key expiration time: expire user 100 (user represents the key, 100 represents the expiration time of this key)

6. Check how long the redis key has expired: ttl user (user represents the key, when the key exists but the remaining survival time is not set, it returns -1. Otherwise, it returns the remaining survival time of the key in seconds)

7. Check how many keys exist in the current redis: dbsize

8. Means to switch the database index of redis: select 1 ( 1 means to switch to redis with database 1, and when entering redis by default, the database of redis is 0)

9. Rename a key: keyrename user user1 (user represents the old key, user1 represents the new key)

10. Confirm whether a key exists: exists user (user means key)

11.Delete a key: del user (user means key)

12. Add to the key whose value is a number: incrby num 10 (num means key, 10 means adding 10 to the value corresponding to num)

13. Subtract the key whose value is a number: decrby num 10 (num means key, 10 means subtract 10 from the value corresponding to num)

14. Add a string of strings to the end of the value: append user jony (means splicing a string jony to the end of the value corresponding to the user key)

15.Add an element with a value of value to the list header named key : lpush key value (used when using redis as a list message queue)

16.Add an element with a value of value at the end of the list named key : rpush key value (used when using redis as a list message queue)

17. Return and delete the first element in the list named key : lpop key (used when using redis as the list message queue)

18.Return and delete the last element in the list named key : rpop key (used when using redis as the list message queue)

19. Assign a value to the element at the index position in the list named key : lset key index value (used when using redis as the list message queue)

In addition, redis has many other advanced operations or less-used operations, which will not be described here.