Redis 5 data types

1, String (string)

In any programming language, String is the most basic data structure. Have you ever thought about the operation of storing a String in Redis?

In Redis, String can be modified, which is called Simple Dynamic String (SDS for short). It is a String, but its internal structure is more like an ArrayList. A byte array is maintained internally, and a certain space is pre allocated in it to reduce the frequent allocation of memory.

Redis's memory allocation mechanism is as follows:

  • When the length of the string is less than 1MB, each expansion will double the existing space.

  • If the string length exceeds 1MB, only 1MB of space will be expanded each time.

In this way, the memory space is sufficient without causing a waste of memory. The maximum length of the string is 512MB

 

 

1. Application scenario:

Storing key value pairs is relatively simple and will not be discussed in detail

 

2. Common commands for String:

set   [key]  [value]   Assign to key Set value( set (old values can be overwritten)

get  [key]   Get specified key Value of

del  [key]   Delete assignment key

exists  [key]  Determine whether there is a specified key

mset  [key1]  [value1]  [key2]  [value2] ...... Batch save key value pair

mget  [key1]  [key2] ......   Batch fetching key

expire [key]  [time]    Assign to key Set expiration time in seconds

setex    [key]  [time]  [value]  Equivalent to set + expire Command combination

setnx  [key]  [value]   If key Does not exist set Create, otherwise return 0

incr   [key]           If value Available as an integer incr The command increases by 1 each time

incrby  [key] [number]  use incrby Command to increment an integer value number

 

2, List

The list in Redis is very similar to the LinkedList in Java. The bottom layer is a linked list structure. The insertion and deletion operations of list are very fast, and the time complexity is 0 (1). Unlike the array structure, the insertion and deletion operations need to move data.

Like the target image, but the bottom layer of list in redis is not as simple as a two-way linked list.

When the amount of data is small, its underlying storage structure is a continuous memory, called ziplost (compressed list), which stores all elements next to each other and allocates a continuous memory; When there is a large amount of data, it will become a QuickList structure.

But the simple linked list is also defective. The front and back pointers prev and next of the linked list will occupy more memory, waste more space, and aggravate the fragmentation of memory. After redis 3.2, the hybrid structure of ziplist + linked list is used, which is called QuickList (quick linked list).

 

1. Application scenario:

Because list is a list sorted by insertion order, there are relatively many application scenarios, such as:

  • Message queuing: lpop and rpush (or vice versa) can implement the function of queue

  • Like list, comment list and ranking list of the circle of friends: lpush command and lrange command can realize the function of the latest list. Each time, insert new elements into the list through lpush command, and then read the latest element list through lrange command.

2. Common naming of list operation:

127.0.0.1:6379> lpush summer xiaxinyu
(integer) 1
127.0.0.1:6379> lpush summer 30
(integer) 2
127.0.0.1:6379> lpush summer "java engineer"
(integer) 3
127.0.0.1:6379> lrange summer 0 10
1) "java engineer"
2) "30"
3) "xiaxinyu"

 

3, hash (Dictionary)

The hash in Redis , which is more similar to the HashMap in Java , is an array + linked list structure. In case of hash collision, elements will be added to the linked list. It is worth noting that in the hash in Redis , value can only be a string

Both hash and string can be used to store user information, but the difference is that hash can store each field of user information separately; String stores the serialized string of all user information. If you want to modify a user field, you must query all user information strings and parse them into corresponding user information objects. After modification, they are serialized into a string and stored. Hash can modify only one field to save network traffic, but the memory occupation of hash is greater than that of string, which is the disadvantage of hash.

 

1. Application scenario:

  • Shopping cart: the hset [key] [field] [value] command can be implemented with user Id, commodity Id as field and commodity quantity as value, which just constitute the three elements of shopping cart.
  • Storage object: the structure of hash type (key, field, value) is similar to that of object (object id, attribute, value). It can also be used to store objects.

 

2. Common operation commands of hash:

127.0.0.1:6379> HMSET summer name "xiaxinyu" description "java engineer" age 30 colleage "sz university"
OK
127.0.0.1:6379> HGETALL summer
1) "name"
2) "xiaxinyu"
3) "description"
4) "java engineer"
5) "age"
6) "30"
7) "colleage"
8) "sz university"
127.0.0.1:6379> HGET summer name
"xiaxinyu"

 

4, Set

The set in Redis is somewhat similar to the HashSet in Java. Its internal key value pairs are unordered and unique. Its internal implementation is equivalent to a special dictionary. All values in the dictionary are NULL. When the last element in the collection is removed, the data structure is automatically deleted and the memory is reclaimed.

 

1. Application scenario:

  • Collection of friends, followers, fans and interested people:
    1. The sinter command can obtain the common friends of users A and B;
    2. The sismember command can judge whether A is A friend of B;
    3. The scard command can obtain the number of friends;
    4. When following, the smove command can transfer B from A's fan collection to A's friend collection
  • Random display on the home page: meituan home page has many recommended businesses, but not all of them can be displayed. The set type is suitable for storing all the contents to be displayed, and the srandmember command can obtain several at random.
  • Store the winning user ID in an activity. Because of the de duplication function, it can ensure that the same user will not win the prize twice.

 

2. Common commands for set:

127.0.0.1:6379> sadd lang java
(integer) 1
127.0.0.1:6379> sadd lang c#
(integer) 1
127.0.0.1:6379> sadd lang javascript
(integer) 1
127.0.0.1:6379> sadd lang java
(integer) 0
127.0.0.1:6379> SMEMBERS lang
1) "c#"
2) "javascript"
3) "java"
127.0.0.1:6379> scard lang
(integer) 3

 

5, Zset (ordered set)

zset is also called SortedSet. On the one hand, it is a set to ensure the uniqueness of the internal value. On the other hand, it can give each value a score to represent the sorting weight of the value. Its internal implementation uses a data structure called "jump list".

 

1. Application scenario:

zset can be used as a leaderboard, but unlike the list, zset can realize dynamic sorting. For example, it can be used to store the list of fans. value is the user ID of fans and score is the time of interest. We can sort the list of fans according to the time of interest.

zset can also be used to store the student's score. value is the student's ID and score is his test score. We can get his ranking by ranking his grades.

 

2. Common operation commands of zset ordered set:

127.0.0.1:6379> zadd lang 1 java
(integer) 1
127.0.0.1:6379> zadd lang 2 c#
(integer) 1
127.0.0.1:6379> zadd lang 3 javascript
(integer) 1
127.0.0.1:6379> zadd lang 3 javascript
(integer) 0
127.0.0.1:6379> zadd lang 4 grovy
(integer) 1
127.0.0.1:6379> zrange lang 0 10
1) "java"
2) "c#"
3) "javascript"
4) "grovy"
127.0.0.1:6379> zrange lang 0 10 withscores
1) "java"
2) "1"
3) "c#"
4) "2"
5) "javascript"
6) "3"
7) "grovy"
8) "4"
127.0.0.1:6379> zrange lang 2 3 withscores
1) "javascript"
2) "3"
3) "grovy"
4) "4"

 

Keywords: Redis

Added by bass123 on Fri, 28 Jan 2022 23:58:48 +0200