[Redis Express] Basic Knowledge 2 - Common Data Structures

Redis common instructions

The following are some common commands of Redis. You can conduct a simple review according to the following table:

  1. Key operation commands:

    • SET: Set the value of the specified key.
    • GET: Get the value of the specified key.
    • DEL: Delete the specified key.
    • EXISTS: Checks whether the specified key exists.
    • KEYS: Get a list of keys that match the specified pattern.
  2. String manipulation commands:

    • APPEND: Appends the specified value to the end of the key.
    • STRLEN: Get the length of the specified key value.
    • INCR: Increase the value of the specified key by 1.
    • DECR: Decrease the value of the specified key by 1.
  3. List operation commands:

    • LPUSH: Inserts one or more values ​​to the left side of the list.
    • RPUSH: Inserts one or more values ​​to the right of the list.
    • LPOP: Removes and returns the first element on the left side of the list.
    • RPOP: Removes and returns the first element on the right side of the list.
    • LLEN: Get the length of the list.
  4. Hash operation command:

    • HSET: Set fields and values ​​in a hash.
    • HGET: Gets the value of the specified field in the hash.
    • HGETALL: Get all fields and values ​​in the hash.
    • HDEL: Remove the specified field from the hash.
    • HKEYS: Get all fields in the hash.
  5. Collection operation command:

    • SADD: Add one or more members to a set.
    • SMEMBERS: Get all members in the set.
    • SREM: Remove one or more members from the set.
    • SINTER: Returns the intersection of multiple sets.
    • SUNION: Returns the union of multiple sets.
  6. Ordered set operation commands:

    • ZADD: Adds one or more members to an ordered set, specifying a score.
    • ZRANGE: Get the members of an ordered set in fractional order.
    • ZREM: Remove one or more members from an ordered set.
    • ZSCORE: Gets the score of a specified member in an ordered set.
    • ZCOUNT: Counts the number of members in an ordered set within a specified fractional range.

Basic string operations

All redis instructions are not case-sensitive. It is easier to distinguish if they are written in all uppercase letters.

Set a single value (install key-value pair)SET <key> <value>

Query a given keyGET <key>

Get old value and set new valueGETSET <key> <value>

Set multiple values ​​at once MSET <key1> <value1> <key2> <value2> ...
Get multiple values ​​at onceMGET <key1> <key2> ...


Get the string length in bytesSTRLEN sentence

Get the text at the specified index of the string (the following code indicates getting the text at index 0-4)
GETRANGE msg 0 4


Plastic increment by 1 INCR <K>
Numerical addition INCRBY <K> <V>
Numerical subtractionDECRBY <K> <V>

If value is omitted, it means +1 or -1


Key hierarchy

Redis keys are named by running multiple words plus colons. The naming format is basically as follows
项目名:业务名:类型:ID

for example:zhiller:user:1


Hash

The format for creating a hash: HSET <KEY> <FIELD> <VALUE>(the three values ​​represent the key, field name, and value respectively)

Create a new key: hello, and fill in two key-value pairs
HSET hello a 10
HSET hello b 10

Only assign a value to a field if it does not existHSETNX

Get the specified value from the specified databaseHGET <K> <F>

Get all fields HKEYS
Get all values HVALS
​​Get both of the aboveHGETALL

Let the value of the specified field increase by itselfHINCRBY <K> <F> [自增值]


List

In Redis, a List is an ordered, repeatable data structure. It can store an ordered list of string elements, allowing insertion and deletion operations at both ends of the list.

The following are some commonly used Redis List commands:

  1. LPUSH: Inserts one or more elements to the left (head) of the list.
  2. RPUSH: Inserts one or more elements to the right (tail) of the list.
  3. LPOP: Removes and returns the first element from the left (head) of the list.
  4. RPOP: Removes and returns the first element on the right (tail) of the list.
  5. LLEN: Get the length of the list.
  6. LRANGE: Get the elements in the specified range in the list.
  7. LINDEX: Gets the element at the specified index position in the list.
  8. LSET: Sets the element value at the specified index position in the list.
  9. LREM: Removes the specified number of specified elements from the list.
  10. LINSERT: Inserts a new element before or after the specified element in the list.
  11. LTRIM: Trim (crop) the list, retaining only elements within the specified range.
  12. RPOPLPUSH: Removes the last element of a list and adds it to the head of another list.

The List structure of Redis is implemented based on a linked list. The time complexity of inserting and deleting operations at the head and tail is O(1), and the time complexity of inserting and deleting operations in the middle of the list is O(N) (N is list length).


SET

Similar to HashSet, unordered, non-repeatable elements, fast search, and supports intersection, difference and union

The following is the SET set instruction and its brief introduction:

  1. SADD: Add one or more members to the set.
  2. SREM: Remove one or more members from the set.
  3. SMEMBERS: Get all members in the set.
  4. SISMEMBER: Checks whether a member exists in the set.
  5. SCARD: Get the number of members of the collection.
  6. SPOP: Randomly removes and returns a member of the set.
  7. SRANDMEMBER: Randomly obtain one or more members of the set.
  8. SINTER: Get the intersection of multiple sets.
  9. SUNION: Gets the union of multiple sets.
  10. SDIFF: Get the difference set of multiple sets.

SortedSet

Can be sorted, elements are not repeated, and query speed is fast

The bottom layer is implemented based on a jump table + hash table;
each element in it has a score attribute and can be sorted based on the score attribute;

Introduction to SortedSet Common Instructions

  1. ZADD: Adds one or more members to an ordered set, with each member associated with a score.
  2. ZREM: Remove one or more members from an ordered set.
  3. ZRANGE: Returns members within the specified index range in ascending order of scores.
  4. ZREVRANGE: Returns members within the specified index range in descending order of scores.
  5. ZRANK: Gets the ranking of the specified member in the ordered set (in order from small to large scores).
  6. ZREVRANK: Gets the ranking of the specified member in the ordered set (in descending order of scores).
  7. ZSCORE: Gets the score of a specified member in an ordered set.
  8. ZCARD: Gets the number of members of an ordered set.
  9. ZCOUNT: Counts the number of members in an ordered set within a specified fractional range.
  10. ZINCRBY: Increases the score of the specified member by the specified increment.
  11. ZINTER ZUNION ZDIFF: intersection, union and difference

Guess you like

Origin blog.csdn.net/delete_you/article/details/132760100