A review of the five major Redis objects (data types) A review of the five major Redis objects (data types)

Excerpt from: https://www.cnblogs.com/hunternet/p/12742390.html

An article reviewing the five major Redis objects (data types)

 


Category:  Redis  Tags:  Redis

Redis is a high-performance distributed in-memory database, which is widely used in major Internet companies at home and abroad. Even some non-Internet companies have very important application scenarios, so the mastery of Redis has also become the backend. The basic skills necessary for engineers. In interviews, Redis has long been a topic that is often talked about, and in actual work, we need to deal with Redis every moment. Therefore, it is very important to master the various martial arts cheats of the Redis technology stack!

Redis provides five main objects (data types) for developers to use, which provides powerful and practical functions. However, in actual development, most developers simply use Redis String Get and Set. This is like the eighteen palms of the dragon, you only learned one palm. In the actual combat against the enemy, it is inevitably slightly thin! In this article, we will review the five major objects of Redis, so that we can truly achieve ease in actual combat.

First, the string (after all, I carried all) #

The string type is the most basic data structure of Redis. Several other data structures are built on the basis of the string type. The value of the string type is a string (simple string, complex string (such as JSON, XML)), number (integer, floating point), or even binary (picture, audio, video), etc.

String object intra-coded three kinds: int, rawand embstr. Redis will decide which encoding to use based on the current value type and length.

  • int: If a string object holds an integer value, and the integer value can longbe represented by type
  • raw: If the string object holds a string value, and the length of the string value is greater than 32 bytes
  • embstr: If the string object holds a string value, and the length of the character application value is less than or equal to 32 bytes

 The use scenarios of reids strings are the most extensive. Even some people who are not familiar with several other objects of redis, basically all scenarios will use strings (serialize them and throw them directly), which makes it very simple. Because of the weight it should not bear at this age. In fact, the main use scenarios of Redis are mainly the following:

  1. As a cache layer, cache hot data
  2. The Redis string can increase and decrease automatically, and can be used as a counter, speed limiter, and increment ID generation.
  3. Session sharing in distributed systems
  4. Binary data storage

For a more detailed introduction to Redis strings, you can check this article of mine.

Redis object-string

Second, hash (I can also store objects) #

Hash objects are used to store a set of data pairs. Each data pair contains two parts: key and value.

Redis-Hash

There are also two ways to implement hash objects. ziplist (compressed list) , hashtable (hash table)

Similarly, Redis uses compressed lists to implement hash objects only when the amount of data stored is relatively small. There are two specific requirements:

  • The size of the keys and values ​​stored in the dictionary should be less than 64 bytes;
  • The number of key-value pairs in the dictionary should be less than 512.

When the above two conditions cannot be met at the same time, Redis uses a hash table to implement the hash object.

When the stored content is an object, many functions of Redis string objects can also be implemented using Redis hash objects. For example, when caching user information, using Redis hash object storage is simple and intuitive. If it is used reasonably, it can reduce the use of memory space. But it also has its disadvantages, that is, it is necessary to control the conversion of hashes in the two internal encodings of ziplist and hashtable.

In addition, Redis hash objects can also implement functions such as shopping carts and counters.

For a more detailed introduction to Redis hash objects, you can check my article.

Redis object-Hash

Three, list (I can do both stack and queue) #

Objects such as lists support the storage of an ordered set of non-repeating data. Because of its order, it can obtain a list of elements in a specified range, and can obtain elements with a specified index subscript in O (1) time complexity.

Before Redis 3.2, there were two types of internal encodings for the list type. When the following two conditions are met, Redis list objects are implemented using ziplist (compressed list).

  • When the number of elements in the list is less than the list-max-ziplist-entries configuration (512 by default)

  • When the value of each element in the list is less than the list-max-ziplist-value configuration (default 64 bytes)

When the list type cannot meet the conditions of ziplist, Redis will use linkedlist as the internal implementation of the list.

In the Redis 3.2 version, the list data structure has been modified, using quicklist instead of ziplist and linkedlist.

Due to the orderly and non-repeatable nature of the list object, it is more suitable for storing lists such as articles and commodities.

The list type can be lpush (push on the left), and the first element of rpop (push on the right) (query and delete) can be used at the same time, so the list type has a first-in first-out feature and can be used to implement message queues. It can also be lpush (push on the left)-> lpop (pop on the left), which has the feature of last in first out, so we can also use list objects to implement when we need to use the stack in development.
For a more detailed introduction to Redis list objects, you can check my article.

Redis object-List

Fourth, the collection (I am in the label system) #

Set object (Set) is an unordered and unique set of key values. Its storage order will not be stored in the order of insertion. The difference with the list is that the data it stores is unordered and not repeated.

There are also two internal encodings for collection objects: intset (integer set) and hashtable (hash table). The collection object is implemented using intset when the following two conditions are met.

  • The elements in the collection are all integers
  • The number of elements in the collection is less than the set-maxintset-entries configuration (512 by default)

When the above two conditions are not met, the collection object is implemented using hashtable.

The main characteristics of the collection object are that it is unordered, non-repeatable, supports and crosses, so it can be used as a labeling system.

The collection of  SPOP (randomly remove and return one or more elements in the collection)  and  SRANDMEMBER (randomly return one or more elements in the collection)  commands can help us implement a lottery system.

For a more detailed introduction to Redis collection objects, you can check my article.

Redis object-Set

Fifth, ordered collection (my name is the best) #

Sorted set type (Sorted Set or ZSet) Compared to the set type, there is an additional sorting attribute score (score). For an ordered set ZSet, each storage element is equivalent to two values, one is The element value of the order combination, one is the order value. An ordered collection retains the characteristic that a collection cannot have duplicate members (scores can be repeated), but the difference is that elements in an ordered collection can be ordered.

The ordered set is composed of  ziplist (compressed list)  or  skiplist (skip list)  .

When the data is relatively small, the ordered set is stored using ziplist, and the ordered set stored using ziplist format must meet the following two conditions:

  • The number of elements in an ordered set is less than 128;
  • The length of all element members stored in an ordered set must be less than 64 bytes.

If any of the above two conditions cannot be met, the ordered set will be stored using a skiplist structure.

A typical use case for ordered sets is a ranking system such as ranking of student performance. User likes, play rankings, sales rankings of products in the e-commerce system of a video (blog, etc.) website, etc.

For a more detailed introduction to Redis ordered collection objects, you can check my article.

Redis object-ordered set (ZSet)

Summary #

Redis provides the five most basic and commonly used objects (data types): String, Hash, List, Set, ZSet. Understanding these five objects helps us better use Redis in daily development. Through this article, we can see that each object is realized by multiple data structures, you can think about why.

-----END-----#

Author:  older than `

Source: https://www.cnblogs.com/hunternet/p/12742390.html

This site uses the " CC BY 4.0 " creative sharing agreement, please indicate the author and source in the obvious position of the article.

 

WeChat public account:

 

 
 
 
4
 
 
 
«  Previous:  Redis object-ordered set (ZSet)

Redis is a high-performance distributed in-memory database, which is widely used in major Internet companies at home and abroad. Even some non-Internet companies have very important application scenarios, so the mastery of Redis has also become the backend. The basic skills necessary for engineers. In interviews, Redis has long been a topic that is often talked about, and in actual work, we need to deal with Redis every moment. Therefore, it is very important to master the various martial arts cheats of the Redis technology stack!

Redis provides five main objects (data types) for developers to use, which provides powerful and practical functions. However, in actual development, most developers simply use Redis String Get and Set. This is like the eighteen palms of the dragon, you only learned one palm. In the actual combat against the enemy, it is inevitably slightly thin! In this article, we will review the five major objects of Redis, so that we can truly achieve ease in actual combat.

First, the string (after all, I carried all) #

The string type is the most basic data structure of Redis. Several other data structures are built on the basis of the string type. The value of the string type is a string (simple string, complex string (such as JSON, XML)), number (integer, floating point), or even binary (picture, audio, video), etc.

String object intra-coded three kinds: int, rawand embstr. Redis will decide which encoding to use based on the current value type and length.

  • int: If a string object holds an integer value, and the integer value can longbe represented by type
  • raw: If the string object holds a string value, and the length of the string value is greater than 32 bytes
  • embstr: If the string object holds a string value, and the length of the character application value is less than or equal to 32 bytes

 The use scenarios of reids strings are the most extensive. Even some people who are not familiar with several other objects of redis, basically all scenarios will use strings (serialize them and throw them directly), which makes it very simple. Because of the weight it should not bear at this age. In fact, the main use scenarios of Redis are mainly the following:

  1. As a cache layer, cache hot data
  2. The Redis string can increase and decrease automatically, and can be used as a counter, speed limiter, and increment ID generation.
  3. Session sharing in distributed systems
  4. Binary data storage

For a more detailed introduction to Redis strings, you can check this article of mine.

Redis object-string

Second, hash (I can also store objects) #

Hash objects are used to store a set of data pairs. Each data pair contains two parts: key and value.

Redis-Hash

There are also two ways to implement hash objects. ziplist (compressed list) , hashtable (hash table)

Similarly, Redis uses compressed lists to implement hash objects only when the amount of data stored is relatively small. There are two specific requirements:

  • The size of the keys and values ​​stored in the dictionary should be less than 64 bytes;
  • The number of key-value pairs in the dictionary should be less than 512.

When the above two conditions cannot be met at the same time, Redis uses a hash table to implement the hash object.

When the stored content is an object, many functions of Redis string objects can also be implemented using Redis hash objects. For example, when caching user information, using Redis hash object storage is simple and intuitive. If it is used reasonably, it can reduce the use of memory space. But it also has its disadvantages, that is, it is necessary to control the conversion of hashes in the two internal encodings of ziplist and hashtable.

In addition, Redis hash objects can also implement functions such as shopping carts and counters.

For a more detailed introduction to Redis hash objects, you can check my article.

Redis object-Hash

Three, list (I can do both stack and queue) #

Objects such as lists support the storage of an ordered set of non-repeating data. Because of its order, it can obtain a list of elements in a specified range, and can obtain elements with a specified index subscript in O (1) time complexity.

Before Redis 3.2, there were two types of internal encodings for the list type. When the following two conditions are met, Redis list objects are implemented using ziplist (compressed list).

  • When the number of elements in the list is less than the list-max-ziplist-entries configuration (512 by default)

  • When the value of each element in the list is less than the list-max-ziplist-value configuration (default 64 bytes)

When the list type cannot meet the conditions of ziplist, Redis will use linkedlist as the internal implementation of the list.

In the Redis 3.2 version, the list data structure has been modified, using quicklist instead of ziplist and linkedlist.

Due to the orderly and non-repeatable nature of the list object, it is more suitable for storing lists such as articles and commodities.

The list type can be lpush (push on the left), and the first element of rpop (push on the right) (query and delete) can be used at the same time, so the list type has a first-in first-out feature and can be used to implement message queues. It can also be lpush (push on the left)-> lpop (pop on the left), which has the feature of last in first out, so we can also use list objects to implement when we need to use the stack in development.
For a more detailed introduction to Redis list objects, you can check my article.

Redis object-List

Fourth, the collection (I am in the label system) #

Set object (Set) is an unordered and unique set of key values. Its storage order will not be stored in the order of insertion. The difference with the list is that the data it stores is unordered and not repeated.

There are also two internal encodings for collection objects: intset (integer set) and hashtable (hash table). The collection object is implemented using intset when the following two conditions are met.

  • The elements in the collection are all integers
  • The number of elements in the collection is less than the set-maxintset-entries configuration (512 by default)

When the above two conditions are not met, the collection object is implemented using hashtable.

The main characteristics of the collection object are that it is unordered, non-repeatable, supports and crosses, so it can be used as a labeling system.

The collection of  SPOP (randomly remove and return one or more elements in the collection)  and  SRANDMEMBER (randomly return one or more elements in the collection)  commands can help us implement a lottery system.

For a more detailed introduction to Redis collection objects, you can check my article.

Redis object-Set

Fifth, ordered collection (my name is the best) #

Sorted set type (Sorted Set or ZSet) Compared to the set type, there is an additional sorting attribute score (score). For an ordered set ZSet, each storage element is equivalent to two values, one is The element value of the order combination, one is the order value. An ordered collection retains the characteristic that a collection cannot have duplicate members (scores can be repeated), but the difference is that elements in an ordered collection can be ordered.

The ordered set is composed of  ziplist (compressed list)  or  skiplist (skip list)  .

When the data is relatively small, the ordered set is stored using ziplist, and the ordered set stored using ziplist format must meet the following two conditions:

  • The number of elements in an ordered set is less than 128;
  • The length of all element members stored in an ordered set must be less than 64 bytes.

If any of the above two conditions cannot be met, the ordered set will be stored using a skiplist structure.

A typical use case for ordered sets is a ranking system such as ranking of student performance. User likes, play rankings, sales rankings of products in the e-commerce system of a video (blog, etc.) website, etc.

For a more detailed introduction to Redis ordered collection objects, you can check my article.

Redis object-ordered set (ZSet)

Summary #

Redis provides the five most basic and commonly used objects (data types): String, Hash, List, Set, ZSet. Understanding these five objects helps us better use Redis in daily development. Through this article, we can see that each object is realized by multiple data structures, you can think about why.

-----END-----#

Guess you like

Origin www.cnblogs.com/xichji/p/12751067.html