2021-06-16 Redis basic data type

Redis basic data type

Basic data structures include: strings, hashes, lists, sets, and sorted sets. These five data structures are often used in our work and often asked during the interview. Therefore, mastering the use and application scenarios of these five basic data structures is the most basic and important part of Redis knowledge.

String type

String is the simplest storage type of Redis. Its stored value can be string, integer or floating-point number. It performs operations on the whole string or part of the string; Performs an increment or decrement operation on an integer or floating-point number.

Redis's string is a sequence composed of bytes, which is a bit similar to ArrayList in java. It uses pre allocation of redundant space to reduce frequent memory allocation. As shown in the figure, the internal space capacity actually allocated for the current string is generally higher than the actual string length len. When the string length is less than 1M, the expansion will be doubled to the existing space. If it exceeds 1M, only 1M more space will be expanded at one time. It should be noted that the maximum length of the string is 512M.

Application scenario

String type is widely used in work. It is mainly used to cache data and improve query properties. For example, store login information, store commodity information in e-commerce, and make counters.

  • set key value: add a String type data
  • get key: get a String type data
  • mset key1 value1 key2 value2: add multiple String type data
  • mget key1 key2: get multiple String type data
  • incr key: Auto increment (+ 1)
  • incrby key step: self increment according to step
  • decr key: self subtraction (- 1)
  • decrby key step: decreases by step

Practical operation

Insert string

java
> set username zhangsan  
"OK"  

 Get string  

> get username  
"zhangsan"  


Insert multiple strings

>mset age 18 address bj  
"OK"  


Get multiple strings
  
>mget username age  
 1)  "zhangsan"  
 2)  "18"  

Self increasing

>incr num  
"1"  
>incr num  
"2"  

 Self subtraction  
>decr num  
"1"  

 Specified step size self increment  
>incrby num 2  
"3"  
>incrby num 2  
"5"  
​  
Specify step size self subtraction  
>decrby num 3  
"2"  
​  
 delete  
>del num  
"1"

Hash hash

Hash is equivalent to HashMap in Java, with an unordered dictionary inside. The implementation principle is consistent with HashMap. A hash table has multiple nodes, and each node stores a key value pair.

Different from HashMap in Java, the rehash method is different, because when the dictionary of HashMap in Java is large, redhash is a time-consuming operation and needs to rehash all at once. In order to achieve high performance, Redis cannot block the service, so it adopts the progressive rehash strategy.

Progressive rehash will retain the old and new hash structures while rehash. When querying, it will query the two hash structures at the same time, and then gradually migrate the content of the old hash to the new hash structure in subsequent scheduled tasks and hash operation instructions. When the relocation is completed, a new hash structure will be used instead.
When the hash removes the last element, the data structure is automatically deleted and the memory is recycled.

Application scenario

Hash can also be the same as object storage, such as storing user information. Unlike the string, the string can be saved only after the object is serialized (such as json serialization), while hash can store each field of the user object separately, which can save the time of serialization and deserialization. As follows:

In addition, you can also save the user's purchase records. For example, key is the user id, field is the commodity id, and value is the quantity of commodities. It can also be used to store shopping cart data, such as key for user id, field for commodity id, value for purchase quantity, etc.

Operation instruction

# set a property
hset keyname field1 value1 field2 value2
# Get a property value
hget keyname field
# Get all attribute values
hgetall keyname
# Delete an attribute
hdel keyname field
# Get the number of attributes
hlen keyname
# Increase / decrease an attribute by step (the attribute must be numeric)
hincrby keyname field step

Practical operation

# Insert hash data
>hset userInfo username zhangsan age 18 address bj
"3"
# Get hash single field data
>hget userInfo username
"zhangsan"
>hget userInfo age
"18"
# Get hash multiple field data
>hmget userInfo username age
1) "zhangsan"
2) "18"
# Get all field data of hash
>hgetall userInfo
1) "username"
2) "zhangsan"
3) "age"
4) "18"
5) "address"
6) "bj"
# Get the number of field s of hash
>hlen userInfo
"3"
# A field of self incrementing hash
>hincrby userInfo age 2
"20"
>hincrby userInfo age 2
"22"
# A field of self decreasing hahs (achieved by self increasing negative step size)
>hincrby userInfo age -2
"20"
# Delete a field of hash
>hdel userInfo age
"1"
# Delete all hash data
>del userInfo
"1"

lists

lists in Redis is equivalent to LinkedList in Java. Its implementation principle is a two-way linked list (its bottom layer is a fast list), that is, it can support reverse search and traversal, which is more convenient for operation. Insert and delete operations are very fast, with a time complexity of O(1), but index positioning is very slow, with a time complexity of O(n).

Application scenario

Lists has many application scenarios. You can use it to easily practice the best-selling list; Work queue can be realized (use the Push operation of lists to store the task in the list, and then the working thread takes out the task for execution with POP operation); Can achieve the latest list, the hottest comments, etc.

Operation instruction

# Zuo Jin
lpush key value1 value2 value3...
# Left out
lpop key
# Right entry
rpush key value1 value2 value3...
# Right out
rpop key
# Read from left to right that start and end are subscripts
lrange key start end


Practical operation
# Insert from the left side of the list
>lpush student zhangsan lisi wangwu
"3"
# Insert from the right side of the list
>rpush student tianqi
"4"
# Pop up one from the left of the list
>lpop liangshan
"wangwu"
# Pop up one from the right side of the list
>rpop liangshan
"tianqi"
# Get the data of list subscripts 0 ~ 1 (closed left and closed right)
>lrange liangshan 0 1
 1) "lisi"
 2) "zhangsan"

Note: blpop blocking version gets
Why block the version of pop? The main reason is to avoid polling. For a simple example, if we use list to implement a work queue. The thread executing the task can call the blocked version of pop to get the task, which can avoid polling to check whether there is a task. When the task comes, the worker thread can return immediately and avoid the delay caused by polling.

Keywords: Java Redis

Added by Karve on Sat, 29 Jan 2022 07:47:19 +0200