preface

Redis is an open source, network-enabled, memory-based, persistent, logging, key-value database written in ANSIC, and provides multiple language apis.

Redis can be widely used in microservice architectures. It may be one of the few popular software solutions that your application can leverage in many different ways. It can act as a master database, cache, and message broker on request.

In addition to these basic commands, I also organized the Redis command Reference Manual PDF, which is free to share with everyone. In addition, there are “Redis development and operation”, “Redis design and implementation”, “Redis white introductory guide” and “2021Redis latest interview questions” and other Redis learning related information, are at the end of the article, if you need to take.

All right, no more words, just sit tight and let’s go!

Redis startup

  • Local boot: redis-cli
  • Remote boot: redis-cli -h host -p port -a password

Redis connection command

  • AUTH password Verifies that the password is correct
  • ECHO message Prints a character string
  • PING to check whether the service is running
  • QUIT Closes the current connection
  • SELECT index switches to the specified database

Redis keys

1, the DEL key

The DUMP key serializes the given key and returns the serialized value

2, the EXISTS the key

Checks whether the given key exists

EXPIRE key seconds

Set the expiration time for the key

EXPIRE key timestamp

Set the expiration time of the key by timestamp

PEXPIRE key milliseconds

Sets the expiration time of the key in milliseconds

6, the KEYS of the pattern

Find all keys that match the given pattern

MOVE key DB

Move the key of the current database to the database DB

8, PERSIST the key

If you remove the expiration time of a key, the key will be persisted

9, PTTL key

Returns the remaining expiration time of the key in milliseconds

10, TTL key

Returns the remaining lifetime of a given key, in seconds

11, RANDOMKEY

Returns a random key from the current database

12, RENAME key newkey

Change the name of the key

13. RENAMENX key Newkey

Change the name of the key to newkey only when newkey does not exist

14, TYPE the key

Returns the type of the value stored by key

Reids string command

1. SET key value

2, GET the key

3. GETRANGE key start end

Returns a subcharacter of the string value in key

4. GETSET key value

Sets the value of the given key to value and returns the old value of the key

5. GETBIT KEY OFFSET

Gets the bit at the specified offset for the string value stored by key

MGET KEY1 KEY2

Gets the value of one or more given keys

7. SETBIT KEY OFFSET VALUE

Sets or clears bits at the specified offset for the string value that is stored by key

8. SETEX key seconds value

Associate the value value with the key and set the expiration time of the key to seconds.

9. SETNX key value

Set the key value only if the key does not exist.

10. SETRANGE key offset value

Overwrites the string value stored for the given key with the value argument, starting at offset.

11, STRLEN key

Returns the length of the string value stored by key.

12、MSET key value [key value …]

Set one or more key-value pairs at the same time.

13、MSETNX key value [key value …]

Sets one or more key-value pairs simultaneously if and only if none of the given keys exist.

PSETEX key milliseconds value

This command is similar to the SETEX command, but it sets the lifetime of the key in milliseconds, rather than seconds, as the SETEX command does.

15, INCR key

Increment the value of the number stored in the key by one.

16, INCRBY Key increment

Add the value stored in the key to the given increment.

17, INCRBYFLOAT Key increment

Add the value stored by key to the given floating point increment.

18, DECR key

Subtract the number stored in the key by one.

19. DECRBY Key Decrement

Values stored by key minus the given decrement value.

20. APPEND key value

If the key already exists and is a string, the APPEND command appends the specified value to the end of the original value.

4. Redis hash command

1, HDEL key field1 [field2]

Deletes one or more hash table fields

2. HEXISTS key field

Check whether the specified field in the hash table key exists.

3. HGET key field

Gets the value of the specified field stored in the hash table.

4, HGETALL key

Gets all the fields and values of the specified key in the hash table

5, HINCRBY Key field increment

Add increment to the integer value of the specified field in the hash table key.

6. HINCRBYFLOAT Key field increment

Increments the floating point value of the specified field in the hash table key.

7, HKEYS key

Gets all fields in the hash table

8, HLEN key

Gets the number of fields in the hash table

9, HMGET key field1 [field2]

Gets the values of all given fields

10、HMSET key field1 value1 [field2 value2 ]

Set multiple field-value pairs into the hash key at the same time.

11. HSET key field value

Set the value of field in hash table key to value.

12. HSETNX Key field value

Set the value of the hash table field only if the field field does not exist.

13, HVALS key

Gets all values in the hash table

14, HSCAN key cursor [MATCH pattern] [COUNT COUNT]

Iterates key-value pairs in a hash table.

Redis list command

1、BLPOP key1 [key2 ] timeout

Removes and retrieves the first element of the list. If there are no elements in the list, the list is blocked until the wait times out or an eject element is found.

2、BRPOP key1 [key2 ] timeout

Removes and retrieves the last element of the list. If there are no elements in the list, the list is blocked until the wait times out or an eject element is found.

3, BRPOPLPUSH source destination timeout

Pops a value from a list, inserts the pop-up element into another list and returns it; If the list has no elements, it blocks until the wait times out or a popup element is found.

4. LINDEX Key index

Gets the elements in the list by index

5, LINSERT key BEFORE | AFTER the pivot value

Inserts an element before or after the element in the list

6, LLEN key

Get the list length

7, LPOP key

Removes and gets the first element of the list

8, LPUSH key value1 [value2]

Inserts one or more values into the list header

9. LPUSHX key value

Inserts a value into the head of an existing list

10. LRANGE key start stop

Gets the elements in the specified range of the list

11. LREM key count value

Remove list elements

12, LSET key index value

Set the value of a list element by index

13. LTRIM key start stop

To trim a list, that is, to keep only elements within a specified range, and to remove all elements that are not within a specified range.

14, RPOP key

Removes and gets the last element of the list

15, RPOPLPUSH source destination

Removes the last element of the list and adds it to another list and returns

16, RPUSH key value1 [value2]

Adds one or more values to the list

17, RPUSHX key value

Add a value to an existing list

Redis collection command

1, SADD key member1 [member2]

Adds one or more members to a collection

2, SCARD key

Gets the number of members of the collection

SDIFF key1 [key2]

Returns the difference set of all sets given

4, SDIFFSTORE destination key1 [key2]

Returns the difference set for a given collection and stores it in destination

5 、SINTER key1 [key2]

Returns the intersection of all sets given

6, SINTERSTORE destination key1 [key2]

Returns the intersection of all the given collections and stores them in destination

7, SISMEMBER key member

Check whether the member element is a member of the collection key

8, SMEMBERS key

Returns all members of the collection

SMOVE source member

Move the member element from the source collection to the Destination collection

10, SPOP key

Removes and returns a random element from the collection

SRANDMEMBER key [count]

Returns one or more random numbers in a collection

12, SREM key member1 [member2]

Removes one or more members of a collection

13, SUNION key1 [key2]

Returns the union of all given sets

14, SUNIONSTORE Destination key1 [key2]

The union of all given collections is stored in the Destination collection

15, SSCAN key cursor [MATCH pattern] [COUNT COUNT]

Iterate over the elements in the collection

Redis ordered collection command

1, ZADD key score1 member1 [score2 member2]

Adds one or more members to an ordered collection, or updates the scores of existing members

2, ZCARD key

Gets the number of members of an ordered collection

3. ZCOUNT key min Max

Computes the number of members in an ordered set with a specified interval fraction

4, ZINCRBY key increment member

Increment the score of a specified member in an ordered set

ZINTERSTORE destination numkeys key [key…]

Computes the intersection of one or more ordered sets given and stores the result set in a new ordered set key

6. ZLEXCOUNT key min Max

Computes the number of members in the specified dictionary range in an ordered collection

7, ZRANGE key start stop [WITHSCORES]

Returns an ordered collection of the members of a specified interval through an indexed interval

7, ZRANGEBYLEX key min Max [LIMIT offset count]

Returns a member of an ordered collection through a dictionary interval

Max [WITHSCORES] [LIMIT]

Returns an ordered set of members within a specified interval by a fraction

10, ZRANK key member

Returns the index of the specified member in the ordered collection

11、ZREM key member [member …]

Removes one or more members of an ordered collection

12. ZREMRANGEBYLEX key min Max

Removes all members of the given dictionary range from the ordered collection

13, ZREMRANGEBYRANK key start stop

Removes all members of the given rank range from the ordered collection

14. ZREMRANGEBYSCORE key min Max

Removes all members of the given fractional interval from the ordered set

15, ZREVRANGE key start stop [WITHSCORES]

Returns the members of an ordered set in a specified interval, indexed from high to bottom

16, ZREVRANGEBYSCORE key Max min [WITHSCORES]

Returns the members of the ordered set within the specified range of scores, sorted from highest to lowest

17, ZREVRANK key member

Returns the ranking of the specified members of an ordered set, ordered in decreasing order (from largest to smallest) by score value

18, ZSCORE key member

Returns the score value of a member in an ordered set

19、ZUNIONSTORE destination numkeys key [key …]

Computes the union of a given one or more ordered sets and stores it in a new key

20, ZSCAN key cursor [MATCH pattern] [COUNT COUNT]

Iterating over elements in an ordered set (including element members and element scores)

Redis issues subscription commands

1、PSUBSCRIBE pattern [pattern …]

Subscribe to one or more channels that conform to a given pattern.

2、PUBSUB subcommand [argument [argument …]

View the subscription and publication system status.

PUBLISH Channel message 3

Sends the message to the specified channel.

4, subscribe [pattern [pattern…]]

Unsubscribe all channels for a given mode.

5, SUBSCRIBE channel [channel…

Subscribe to a given channel or channels of information.

6、UNSUBSCRIBE [channel [channel …]]

To unsubscribe from a given channel.

Example:

Redis 127.0.0.1:6379> SUBSCRIBE redisChat Reading messages... (press Ctrl-C to quit) "subscribe" "redisChat" (integer) 1Copy the code

Now, let’s restart a Redis client and publish twice on redisChat on the same channel so that subscribers can receive the messages. Redis 127.0.0.1:6379> PUBLISH redisChat “Redis is a great Caching technique”

(integer) 1

The subscriber's client will display the following message: 1) "message" 2) "redisChat" 3) "Redis is a great Caching technique"Copy the code

Redis transaction command

1, the DISCARD

Cancels the transaction, abandoning all commands in the transaction block.

2, the EXEC

Execute all commands within the transaction block.

3, MULTI

Marks the start of a transaction block.

4, UNWATCH

Unmonitor all keys with the WATCH command.

5. [key…]

Monitor a key (or keys) and interrupt the transaction if the key (or keys) is changed by another command before the transaction executes.

Redis script command

1, EVAL script numkeys [key… arg [arg …]

Execute the Lua script.

EVALSHA sha1 numkeys key [key…] arg [arg …]

Execute the Lua script.

3, SCRIPT EXISTS.

Checks whether the specified script has been saved in the cache.

4, SCRIPT FLUSH

Remove all scripts from the script cache.

5, the SCRIPT to KILL

Kill the currently running Lua script.

SCRIPT LOAD SCRIPT 6

The script script is added to the script cache, but it is not executed immediately.

Redis server command

1, BGREWRITEAOF

Perform an AOF (AppendOnly File) File rewrite asynchronously

2, BGSAVE

Asynchronously saves the current database data to disk in the background

3, CLIENT KILL [IP :port] [ID client-id]

Close the client connection

4, CLIENT LIST

Gets a list of client connections to the server

5, CLIENT GETNAME

Gets the name of the connection

6. CLIENT PAUSE timeout

Terminates the execution of a command from a client within the specified time

7. CLIENT SETNAME connection-name

Sets the name of the current connection

8, CLUSTER SLOTS

Gets a mapping array of cluster nodes

9, the COMMAND

Gets an array of Redis command details

10 and the COMMAND of the COUNT

Gets the total number of Redis commands

11, the COMMAND GETKEYS

Gets all keys for a given command

12, the TIME to return

Return to the current server time

13、COMMAND INFO command-name [command-name …]

Gets an array of descriptions for the specified Redis command

14, CONFIG GET parameter

Gets the value of the specified configuration parameter

15, the CONFIG REWRITE

Overwrite the redis.conf configuration file specified when starting the Redis server

16, CONFIG SET

Arameter value Modifies redis configuration parameters without restart

17, the CONFIG RESETSTAT

Reset some statistics in the INFO command

In the 18th and DBSIZE

Returns the number of keys in the current database

19. DEBUG OBJECT keys

Obtain key debugging information

20, the DEBUG SEGFAULT

Crash the Redis service

21, and FLUSHALL

Delete all keys for all databases

22, FLUSHDB

Delete all keys from the current database

23, the INFO/section

Get all kinds of information and statistics of Redis server

24, LASTSAVE

Returns the last time Redis successfully saved data to disk, in UNIX timestamp format

25, MONITOR

Real – time print Redis server received command, debugging

26, ROLE

Returns the role to which the primary and secondary instances belong

27, SAVE

Synchronization saves data to hard disks

28, SHUTDOWN [SAVE]

Asynchronously saves data to hard disk and shuts down server

29. SLAVEOF host port

Convert the current server to a slave server for the specified server

30, SLOWLOG subcommand [argument]

Manage slow logs in Redis

31, SYNC

Internal command for replication


Thanks to see here [fist bump], space is limited to write these first, sorted Redis learning materials are put here

Learn to pick up the portal