This is the 14th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge
Redis is a very mainstream KV database at present. It is famous for its high performance reading and writing ability. In fact, another advantage is that Redis provides richer data types, which makes Redis more widely used. What data types does Redis provide to users? The main ones are string, List, Set, Hash, Zset, HyperLogLogs (a data structure for calculating cardinality), and Streams (Redis 5.0 provides a new data structure for modeling logging).
Note that the data type here refers to the data type of the Redis value, whereas the Redis key type is always string.
This article focuses on the first five, the most commonly used data types. The other two are available on the Redis website (redis.io). In addition, Redis is one of the most popular interview questions for Java programmers. “What data types does Redis have?” It is the basic question that the interviewer opens his mouth. If you can’t get past that first question, Redis is basically cold.
String | string type
Redis is a string type that can store strings, integers, or floating point numbers. If you store integers or floating point numbers, you can also perform increment or decrement operations.
And redis string is binary safe, it can contain any data, such as a serialized object, a picture byte stream, etc. However, the storage size is limited to -512MB
Here’s what binary security means: in simple terms, strings are not parsed according to some special flag bit (\0 in C), and whatever the input is, the output is always the original input that was processed and not according to some special format.
How does Redis implement binary security for string?
The answer is Sds (Simple Dynamic String). Redis defines its own data structure. (A brief introduction)
typedef char *sds; Struct SDSHDR {// buf occupied length int len; // buf remaining available length int free; // char buf[]; };Copy the code
Commands that manipulate strings
Basic set, get, and del commands and examples
Get keyName Gets the value stored in the given key set keyname value Sets the value stored in the given key del keyname Deletes the value stored in the given key (generic command, applicable to all types)
127.0.0.1:6379> set happy today
OK
127.0.0.1:6379> get happy
"today"
127.0.0.1:6379> del happy
(integer) 1
127.0.0.1:6379> get happy
(nil)
127.0.0.1:6379>
Copy the code
Increment and decrement commands
Incr keyName Increases the value in the key store by 1. Decr kename decreases the value in the key store by 1. Incrby keyName Amount Increases the value in the key store by an integer Incrbyfloat keyName Amount Adds float amount to the value of the key store
127.0.0.1:6379> set number 1 OK 127.0.0.1:6379> get number "1" 127.0.0.1:6379> incr number (INTEGER) 2 127.0.0.1:6379> Get number "2" 127.0.0.1:6379> decr number (INTEGER) 1 127.0.0.1:6379> get number "1" 127.0.0.1:6379> incrby number 3 (INTEGER) 4 127.0.0.1:6379> get number "4" 127.0.0.1:6379> decrby number 2 (INTEGER) 2 127.0.0.1:6379> get number "2" 127.0.0.1:6379> incrbyfloat number 1.23 "3.23" 127.0.0.1:6379> get number "3.23" float number 1.23 "3.23"Copy the code
Substring and binary bit commands
Append keyName value Appends a value to the end of the specified string. Getrange keyName start end Obtains a substring consisting of all characters from start to end. Include start and end setrange keyName Offset Value Overwrite the string value stored by the keyname key starting from offset. Getbit KeyName offset Specifies the string value stored for keyname. The value is a bit at the specified offset. Setbit keyName offset Value Specifies the string value stored for keyname. Sets or clears bits at the specified offset.
Note that the redis index starts at 0
127.0.0.1:6379> get hello "world" 127.0.0.1:6379> Append Hello, Java (INTEGER) 10 127.0.0.1:6379> get hello "world, Java" 127.0.0.1:6379> getrange hello 2 5 "RLD," 127.0.0.1:6379> setrange hello 6 redis (INTEGER) 11 127.0.0.1:6379> get hello "World,redis" 127.0.0.1:6379> 127.0.0.1:6379> setbit bitstr 100 1 (INTEGER) 0 127.0.0.1:6379> getbit bitstr 100 (INTEGER) 1 127.0.0.1:6379> get bitstr "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00\b" 127.0.0.1:6379>Copy the code
Several other important commands
Setnx key value sets the value of the key to value only if the key does not exist. If the key already exists, the SETNX command does not take any action. Setex key seconds value Sets the value of the key to value and the lifetime of the key to seconds seconds. If the key key already exists, the SETEX command overwrites the existing value.
Note: -setnx is short for “SET if Not eXists” (SET if it does Not exist). This command returns 1 on success and 0 on failure. The -setex command has the same effect as the SET key value and EXPIRE key seconds commands, except that SETEX is an atomic operation.
127.0.0.1:6379> exists mark
(integer) 0
127.0.0.1:6379> setnx mark abcd
(integer) 1
127.0.0.1:6379> setnx mark defg
(integer) 0
127.0.0.1:6379> get mark
"abcd"
127.0.0.1:6379> setex cachekey 20 ak98
OK
127.0.0.1:6379> get cachekey
"ak98"
127.0.0.1:6379> ttl cachekey
(integer) 2
Copy the code
The List | List types
The list type in Redis is similar to the list type in many programming languages and can store multiple strings in an ordered manner.
Elements can be pushed or popped from the left and right ends of the list. The underlying implementations of Redis lists are compressed lists (data structures implemented by Redis content itself) and double-ended linked lists. See below
Image from Redis Design & Implementation
Detailed list operation commands
lpush key value [value...]
Inserts one or more values into the list header. If the key does not exist, an empty list is created and an LPUSH operation is performed. Returns an error when the key exists but is not a list type.
After executing the LPUSH command, the length of the list is returned.
127.0.0.1:6379> lpush listKey a (INTEGER) 1 127.0.0.1:6379> lpush listKey a b c (INTEGER) 4 127.0.0.1:6379> lrange Listkey 0-1 (1) "c" (2) "a" "b" (3) (4) "a" 127.0.0.1:6379 >Copy the code
- The list type can add duplicate elements, unlike the set type.
- Lrange listKey 0-1 is used to retrieve the entire list
- Similarly, the rpush command adds elements from the right side of the list
LPOP key
Ejects a value from the left end of the list and returns the ejected value
127.0.0.1:6379> lrange listKey 0-1 1) "c" 2) "b" 3) "a" 4) "a" 127.0.0.1:6379> lpop listKey "c" 127.0.0.1:6379> lrange Listkey 0-1 1) "b" 2) "a" 3) "a" 127.0.0.1:6379> listKey 0-1 1) "b" 2) "a" 3) "a"Copy the code
lrange key start end
Gets the values of all elements of the list key in the given start to end range.
0 is the first element, and -1 is the last element.
127.0.0.1:6379> lrange listKey 0-1 1) "b" 2) "a" 3) "a" 127.0.0.1:6379> lrange listKey 0 1 127.0.0.1:6379 >Copy the code
lindex key index
Gets the value of a single element of the list at the given index position.
It could be -1 for the last element, -2 for the penultimate element, and so on.
127.0.0.1:6379> lrange listKey 0-1 1) "b" 2) "a" 3) "a" 127.0.0.1:1 > lindex ListKey 0 "b" 127.0.0.1:6379> lindex listKey 0 "b" 127.0.0.1:6379> lindex Lindex ListKey 3 (nil) 127.0.0.1:6379> lindex listKey 3 (nil)Copy the code
Blpop key [key]... timeout
Blpop is a blocking pop-up command, which is the blocking version of the LPOP key command. When there are no elements in the given list to eject, the connection is blocked by the blpop command until the wait times out or until an eject element is found.
When more than one key argument is given, the list is examined in order of the key argument, and the header element of the first non-empty list is popped.
Therefore, the blpop command will not block if at least one key exists and the list is not empty, and the list in the blpop command is empty, and the blpop command will block within the timeout period.
In the non-blocking scenario, the return value is the first non-empty list name and the element that was ejected.
127.0.0.1:6379> lpush list1 hello java
(integer) 2
127.0.0.1:6379> lpush list2 hello redis
(integer) 2
127.0.0.1:6379> blpop list2 list1 list3 0
1) "list2"
2) "redis"
127.0.0.1:6379>
Copy the code
In the blocking scenario, the blpop book1 book2 300 command will remain blocked.
127.0.0.1:6379> EXISTS Book1 (INTEGER) 0 127.0.0.1:6379> EXISTS Book2 (INTEGER) 0 127.0.0.1:6379> BLpop book1 book2 300 The block is hereCopy the code
At this point, if we open another Redis client, run the following lpush command to push an element to the book1 list.
127.0.0.1:6379> lpush book1 springboot
(integer) 1
127.0.0.1:6379>
Copy the code
At this point, back to the original blocking client, the element has been ejected.
127.0.0.1:6379> exists book1
(integer) 0
127.0.0.1:6379> exists book2
(integer) 0
127.0.0.1:6379> blpop book1 book2 300
1) "book1"
2) "springboot"
(237.45s)
127.0.0.1:6379>
Copy the code
By taking advantage of the blocking command nature of the Redis list type, it is easy to think of implementing a simple version of message queues.
The set | collection types
A collection of Redis stores multiple different elements in an unordered manner. The thing to notice here is the disorder and the difference.
In addition to the collection can quickly add, delete, check whether an element is in the collection, also can perform multiple sets of intersection, union and difference operations.
Overview of the underlying implementation
Redis’ underlying implementation of collection types is primarily through a data structure called a dictionary. However, Redis, in pursuit of extreme performance, selects an Intset data structure based on whether the stored values are integers or not. When certain conditions are met, the implementation of the dictionary is switched.
A dictionary is a set of key-value pairs, each with a different key. The program can add new pairs to the dictionary or lookup, update, or delete them based on the key.
When Redis uses a dictionary data structure to store data, the set stores the elements in the dictionary key, and the dictionary value is set to NULL.
Description of set type operation commands
sadd key member [member...]
When one or more elements are added to a collection key, elements already in the collection are ignored. Returns the number of newly added elements, excluding ignored elements.
srem key member [member...]
Removes one or more elements from the collection. Non-existent elements are ignored. Returns the number of successfully removed elements.
sismember key meber
Check whether the element member exists in the collection key. Return 1 if yes, 0 if no or the key does not exist.
Scard Key returns the number of elements in the set. Spop Key randomly removes an element from the set and returns the removed element. The sMembers key returns all the elements contained in the collection
127.0.0.1:6379> sadd set1 java spring redis
(integer) 3
127.0.0.1:6379> smembers set1
1) "redis"
2) "spring"
3) "java"
127.0.0.1:6379> scard set1
(integer) 3
127.0.0.1:6379> srem set1 spring
(integer) 1
127.0.0.1:6379> sismember set1 spring
(integer) 0
127.0.0.1:6379> smembers set1
1) "redis"
2) "java"
127.0.0.1:6379> sadd set1 mysql spring
(integer) 2
127.0.0.1:6379> spop set1
"redis"
127.0.0.1:6379> smembers set1
1) "mysql"
2) "spring"
3) "java"
127.0.0.1:6379>
Copy the code
Here are some commands for working with multiple collections
sdiff key [key…] Returns an element that exists in the first set, but not in any other set (a mathematical difference set operation). Returns elements that exist simultaneously in all sets (mathematical intersection operations). Returns the elements that exist in at least one set (a mathematical union operation)
127.0.0.1:6379> smembers set1 1) "mysql" 2) "spring" 3) "Java" 127.0.0.1:6379> smembers set2 1) "mysql" 3) "redis" 127.0.0.1:6379> sdiff set1 set2 1) "Java" 2) "spring" 127.0.0.1:6379> sinter set1 set2 1) "mysql" 127.0.0.1:6379> sunion set1 set2 1) "mysql" 2) "springboot" 3) "Java" 4) "spring" 5) "redis" 127.0.0.1:6379>Copy the code
Hash | hash (hash table)
The hash type of Redis is simply a reduced version of Redis. It stores key and value pairs. Multiple key and value pairs are stored in a Single Redis key.
Overview of the underlying implementation
The bottom layer of hash type is also based on the data structure of dictionary.
Redis internally uses two data structures when implementing hash data types. When creating an empty hash table, the default is to use the ziplist data structure, which will be converted into a dictionary if certain conditions are met.
Details of hash table operation commands
hmget hash-key key [key…] Get the value of one or more keys from the hash table hmset hash-key key value [key value…] Set values for one or more keys in the hash table. Hdel hash-key key [key…] Delete one or more key-value pairs from the hash table, Return the number of successfully deleted key pairs. Hlen hash-key Returns the number of key pairs in the hash table. Hexists hash-key key Checks whether the given key exists in the hash table Get all the values contained in the hash. Hgetall hash-key Gets all the key-value pairs contained in the hash
127.0.0.1:6379> hmset hash1 username tom email 123@123 year 12
OK
127.0.0.1:6379> hmget hash1 email
1) "123@123"
127.0.0.1:6379> hlen hash1
(integer) 3
127.0.0.1:6379> hdel hash1 year
(integer) 1
127.0.0.1:6379> hexists hash1 year
(integer) 0
127.0.0.1:6379> hkeys hash1
1) "username"
2) "email"
127.0.0.1:6379> hvals hash1
1) "tom"
2) "123@123"
127.0.0.1:6379> hgetall hash1
1) "username"
2) "tom"
3) "email"
4) "123@123"
127.0.0.1:6379>
Copy the code
An ordered set zset |
We know that the elements stored by the set type are unordered. How can Redis be ordered? Using the score value, the ordered collection stores the mapping between the member and the score value, and provides the score value processing command, and the order to obtain the member or score value according to the size of the score value command.
Overview of the underlying implementation
Redis’s implementation of ordered collections uses a data structure called a skip list, as well as the compressed list mentioned earlier. If certain conditions are met, it will convert itself.
Description of ordered set operation commands
zadd z-key score memer [score member…] Zrem z-key member [member…] zrem z-key member [member… Removes a given member from an ordered set, Zcard z-key return the number of members in an ordered set zincrby z-key Increment member Increment zCount z-key min Max; zcard z-key return the number of members in an ordered set zincrby Z-key Increment member Increment zcount z-key min Max Zrank z-key member Returns the rank of member member in the ordered set. Zscore z-key member Returns the score of member member. Zrange z-key start stop [withscores] returns the members of the ordered collection that are ranked between start and stop. If the optional withscores option is given, the name command returns the member’s score as well. Zrevrank z-key Member Returns the rank of member members in an ordered set. Zrevrange z-key start Stop returns the members in a given ranking range of the ordered collection, in order of the largest to smallest point value. Zrangebyscore z-key min Max Returns all members of an ordered collection whose score values are between min and Max
127.0.0.1:6379> zadd zset1 10 a 12 B 1 C 3 d 20 e (INTEGER) 5 127.0.0.1:6379> zcard zset1 (integer) 5 127.0.0.1:6379> zcard Zcount zset1 2 10 (INTEGER) 2 127.0.0.1:6379> zrank zset1 d (integer) 1 127.0.0.1:6379> zscore zset1 e "20" 127.0.0.1:6379> zrange zset1 3 5 1) "b" 2) "e" 127.0.0.1:6379> zrevRank zset1 d (INTEGER) 3 127.0.0.1:6379> zRevrange Zset1 3 5 1) "d" 2) "c" 127.0.0.1:6379> zrangeByScore zset1 5 10 1) "a" 127.0.0.1:6379>Copy the code