Detailed Explanation of Redis Data Structures
Category Programming Technology
Redis data types are divided into: String type, Hash type, List type, Set type, and Sorted Set type.
How fast does Redis operate, which makes it so popular? An ordinary laptop can complete one hundred thousand read and write operations in one second.
Atomic operation: The smallest unit of operation, which cannot be further divided. That is, the smallest execution unit that cannot be inserted by other commands. There is no race condition under high concurrency.
KEY naming: A good suggestion is to use article:1:title to store the title of the article with ID 1.
I. Preface
- To obtain a list of keys: KEYS pattern with wildcards including ?*[] and escape .
- To check if a key exists: EXISTS key returns 1 if it exists, and 0 if it does not.
- To set a key and delete a key: SET key and DEL key.
- To get the Redis data type stored by a key: TYPE key. The return values are string, list, hash, set, zset. The following will explain these five returned Redis data types one by one.
- To rename a key: RENAME oldkey newkey. If newkey exists, it will be overwritten.
- To rename a key without overwriting: RENAMENX oldkey newkey. If newkey exists, it will not be overwritten.
- To return a random key: RANDOMKEY.
- To move a key to a specified database: MOVE key db-index. If the key does not exist or is already in the database, it returns 0. If successful, it returns 1.
II. Redis Data Types and Commands
Redis data type - String type: This is easy to understand, one key stores a string. If you want to store data? Convert it to Json or other string serialization.
Redis data command - String type:
1) Assignment: SET key value. For example, set hello world.
2) Retrieval: GET key. For example, get hello. The return value is world.
3) Increment: INCR key. It is equivalent to MySQL's AUTO_INCREMENT. Each time INCR key is executed, the value of the key is increased by 1. If the key does not exist, it first establishes a 0, then increases by 1, and returns 1. If the value is not an integer, an error is reported. This operation is an atomic operation.
4) Decrement: DECR key. Reduce the value of the specified key by 1. For example, DECR num is equivalent to num-1.
5) Increment by N: INCRBY key increment is used to add increment to the value of the specified key. For example, INCRBY num 5 is equivalent to num+5.
6) Decrement by N: DECRBY key increment is used to subtract increment from the value of the specified key. For example, DECRBY num 5 is equivalent to num-5.
7) Increment by floating point: INCRBYFLOAT key increment.
8) Append to the tail: APPEND key value. For example, set test:key 123, append test:key 456, get test:key will be 123456.
9) Get the length: STRLEN key.
10) Assign values to multiple keys at the same time: MSET title this is the title description this is the description content this is the content.
11) Retrieve values for multiple keys at the same time: MGET title description content.
12) Bit operation - retrieval: GETBIT key offset. For example, the character 'a' is stored in Redis as 01100001 (ASCII is 98), then GETBIT key 2 is 1, GET key 0 is 0.
13) Bit operation - setting: SETBIT key offset value. For example, the character 'a' is stored in Redis as 01100001 (ASCII is 98), then SETBIT key 6 0, SETBIT key 5 1, so get key will get 'b'. Because the binary retrieved is 01100010.
14) Bit operation - count: BITCOUNT key [start] [end]: BITCOUNT key is used to get the number of 1s in the value of the key. And BITCOUNT key start end is used to count the number of 1s in the binary of the substring between start and end in the value of the key (it's quite complicated).
15) Bit
9. Redis Data Type: Sorted Set Type
Set types are unordered, with each element being unique. In contrast, sorted sets are ordered, with each element also being unique. The difference between sorted set types and set types is that sorted sets assign a score attribute to each element, which is used for sorting. Sorted sets are implemented using hash tables and skip lists, allowing for fast operations on middle elements compared to lists, with a time complexity of O(log(N)). The sorted set type in Redis data types consumes more resources than the list type in Redis data types.
10. Redis Data Commands: Sorted Set Type
1) Add: ZADD key score1 value1 score2 value2...
2) Get score: ZSCORE key value. Retrieve the score of the element with value 'value' in the sorted set of key.
3) Get range by rank: ZRANGE key start stop [WITHSCORES]. Retrieve the list of elements within the rank range start and stop, including both start and stop. Each element is on a new line. If the WITHSCORES parameter is provided, the score follows the element value on the next line. The time complexity is O(LOGn+m). If scores are the same, the order is reversed.
4) Get range by score: ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]. Retrieve the list of elements with scores between min and max, inclusive. Each element is on a new line. If the WITHSCORES parameter is provided, the score follows the element value on the next line. If min is greater than max, the order is reversed.
5) Increment score: ZINCRBY key increment value. Increase the score of the element with value 'value' in the specified sorted set by 'increment'. Return the new score after the increment.
6) Get the number of elements in the set: ZCARD key.
7) Count elements within a score range: ZCOUNT key min max.
8) Remove one or more elements: ZREM key value1 value2...
9) Remove elements by rank range: ZREMRANGEBYRANK key start end. Remove all elements with rank between start and end.
10) Remove elements by score range: ZREMRANGEBYSCORE key min max.
11) Get element rank (ascending): ZRANK key value. Get the rank of 'value' in the sorted set, from lowest to highest.
12) Get element rank (descending): ZREVRANK key value. Get the rank of 'value' in the sorted set, from highest to lowest.
13) Intersection of sorted sets: ZINTERSTORE storekey key1 key2... [WEIGHTS weight [weight...]] [AGGREGATE SUM|MIN|MAX]. Calculate the intersection of multiple sets and store the result in storekey. The return value is the number of elements in storekey. If AGGREGATE is SUM, the score of each element in storekey is the sum of the scores from the sets involved in the calculation. MIN is the minimum score from the sets involved. MAX is the maximum score from the sets involved. WEIGHTS sets the weight of each set, for example, WEIGHTS 1 0.1 means each element's score in set A is multiplied by 1, and in set B by 0.1.
14) Union of sorted sets: ZUNIONSTORE storekey key1 key2... [WEIGHTS weight [weight...]] AGGREGATE SUM|MIN|MAX
**Share my notes
-
-
-