Home  >  Article  >  Database  >  What is the Redis data structure?

What is the Redis data structure?

PHPz
PHPzforward
2023-05-28 10:16:291135browse

Redis is a high-performance key-value database. The emergence of redis has largely compensated for the shortcomings of keyvalue storage such as memcached, and can play a very good supplementary role to relational databases in some situations.

What is the Redis data structure?

1. String

The string type is the most basic data structure of redis. First, the key is a string type, and several other structures are It is built on the basis of the string type, so the string type can set the foundation for learning the other four data structures. The string type can actually be a string (simple string, complex string (xml, json), number (integer, floating point number), binary (image, audio, video)), but the maximum cannot exceed 512M.

Usage scenario: Cache function: The most classic usage scenario of strings, redis is the cache layer, Mysql is the storage layer, most of the request data is obtained from redis, because redis has the feature of supporting high concurrency, so Caching can usually speed up reading and writing and reduce back-end pressure. (Why redis has the characteristics of supporting high concurrency will be explained in the next article). Counter: Many applications use redis as the basic tool for counting. It can implement fast counting and query caching functions, and the data can be transferred to other data sources in one step. The core component of the video play count system is Redis, which is used to count the number of video plays. Shared session: For load balancing considerations, distributed services will balance access to user information to different servers. Users may need to log in again to refresh their access. To avoid this problem, redis can be used to centrally manage user sessions. Here In this mode, as long as the high availability and scalability of redis are ensured, each user update or login information query is directly obtained from redis. Speed ​​limit: For security reasons, users are required to enter a mobile phone verification code every time they log in. In order to prevent the SMS interface from being accessed frequently, the frequency of users obtaining verification codes per minute will be limited.

2. Hash

In redis, the hash type refers to the key itself and a key-value pair structure, such as value={{field1,value1},……fieldN ,valueN}}

The hash structure is more intuitive than string serialization and more convenient for update operations, so it is more suitable for caching information. Therefore, it is often used for user information management, etc., but the hash type is different from the relational database. The hash type is sparse, while the relational database is completely structured. The relational database can do complex relational queries, and redis To simulate complex relational queries, development is difficult and maintenance costs are high.

3. List

The list type is used to store multiple ordered strings. Each string in the list becomes an element. A list can store up to 2 to 32 -1 element. In redis, you can insert (pubsh) and pop (pop) at both ends of the queue table. You can also get a list of elements in a specified range, get elements in the table under a specified index, etc. Lists are a more flexible A data structure that can act as a stack and a queue and has many application scenarios in actual development.

Lists have ordered elements, so you can use index subscripts to get single or multiple elements. Elements in the list can be repeated.

Usage scenarios: Message queue: Redis’s lpush brpop command combination can realize blocking queue. The producer client uses lupsh to insert elements from the left side of the list, and multiple consumer clients use the brpop command to block. "Grab" the elements at the end of the list, multiple clients ensure load balancing and high availability of consumption clipboard.png message queue model↑ article list: Each user has his own article list, and now the article list needs to be displayed in pages. At this time, you can consider using a list. The list is not only ordered, but also supports obtaining elements according to the index range.

4. Collection

The collection type is also used to save the elements of multiple strings, but unlike the list, duplicate elements are not allowed in the collection, and the elements in the collection are In order, elements cannot be obtained through index subscripts. In addition to supporting additions, deletions, modifications, and searches within a collection, redis also supports intersection, union, and difference sets of multiple collections. It can also use collection types rationally, which can be solved in actual development. Many practical issues.

Usage scenarios: Tag: Typical usage scenarios for collection types. For example, one user is more interested in entertainment and sports, and another may be interested in news. These interests are tags. With this data You can get people with the same tags and tags of common hobbies of users. This data is more important for user experience and strong user stickiness. The relationship between users and tags should be maintained in the same transaction to prevent certain command failures from causing data inconsistency

5. Ordered Sets

Ordered sets are inevitably related to sets. They retain the feature that sets cannot have duplicate members, but the difference is that the elements in ordered sets can be sorted. , but what is different from using index subscripts as the sorting basis for lists is that it sets a score for each element as the basis for sorting. (Elements in an ordered set cannot be repeated, but csore can be repeated, just like the student numbers of students in a class cannot be repeated, but their test scores can be the same).

The above is the detailed content of What is the Redis data structure?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete