This article brings you relevant knowledge about data types in Redis. It mainly introduces issues related to common data type usage scenarios. I hope it will be helpful to everyone.
Recommended learning: Redis learning tutorial
Redis data types and usage scenarios
Redis data types and usage scenarios
Compared with other KV databases, one of the major features of Redis is that it supports rich data types. It supports a total of 5 data types. These 5 data types, their usage scenarios and internal implementation are introduced one by one below.string
Introduction: The string type is the most basic data type in Redis, the most commonly used data type, and is even used by many players as the only data type of redis. The string type is binary safe in redis, which means that the string value cares about the binary string and does not care about the specific format. You can use it to store strings in json format or JPEG image format.Get the string length
Append content to the string
Set and get the string A certain piece of content
Set and get a certain bit of the string
Set the contents of a series of strings in batches
Commonly used commands: set, get, decr, incr, mget, etc.
Application scenario:
(1) Store the value of a field in MySQL
Design the key as table name: primary key name: primary key value: field Name
(2) Storage object
The string type supports strings in any format. The most commonly used one is to store strings formatted by json or other objects. (It is recommended to use hash data type in this scenario)
set user:id:1 '[{"id":1,"name":"zj","email":"156577812@qq.com"},{"id":1,"name":"zj","email":"156577812@qq.com"}]'
(3) Generate auto-incrementing id
When the value of the string type of redis is in integer form, redis can automatically treat it as an integer. Increment (incr) and decrement (decr) operations. Since all redis operations are atomic, there is no need to worry about transaction problems that may occur when multiple clients connect.
-
Hash
Introduction: Hash stores the mapping between strings and string values. Hash stores various attributes of the object in the Map, and can only read/update certain attributes of the object. In this way, some properties that are too long can be left alone. In addition, different modules can only update the properties they care about without causing overwriting conflicts due to concurrency with each other.
Commonly used commands: hget, hset, hgetall, etc.
Application scenarios:
Storage structured data, such as user information. In Memcached or CKV, for user information such as the user's nickname, age, gender, points, etc., we need to serialize it first and store it as a string value. At this time, when we need to modify one of the items, we usually need to After the value is taken out and deserialized, the value of an item is modified, and then serialized and stored back. This not only increases the overhead, but is also not suitable for situations where concurrent operations are possible (for example, two concurrent operations require modification of points). The Hash structure of Redis allows you to modify only a certain attribute value just like updating an attribute in the database. As shown below:
Key is the user ID, value is a Map, the key of this Map is the attribute name of the member, and the value is the attribute value. In this way, the modification and access of the data can be directly through its internal Map. Key (the key of the internal Map is called field in Redis), that is, the corresponding attribute data can be manipulated through the key (user ID) field (attribute label). There is no need to store data repeatedly, and it will not cause serialization and concurrency. Modify control issues.
However, it should be noted here that Redis provides an interface (hgetall) to directly obtain all attribute data. However, if there are many members of the internal Map, it involves traversing the entire internal Map. Due to the Redis single-threaded model Because of this, this traversal operation may be time-consuming, and it will not respond to requests from other clients at all. This requires special attention.
Can be used to build indexes. For example, User objects, in addition to id, sometimes also need to be queried by name. You can build a Hash with the key user:name:id. When inserting the User object (set user:101{"id":101,"name":"calvin ”}), by the way, insert an entry into this hash (hset user:name:id calvin 101). At this time, calvin is used as a key in the hash, with a value of 101. When querying by name, use hgetuser:name:id calvin to get the id from the key named calvin. If you need to use multiple indexes to find a certain piece of data, you can use one hash key to avoid using multiple string keys to store index values.
HINCRBY can also be used to implement idmaker. Compared with the string type idmaker, each type requires a key, and the hash type only needs one key.
The hash data type has the advantage of being more flexible and faster than the string type when storing the above types of data. Specifically, using the string type to store, it is necessary to convert and parse the json format string, even if it is not Conversion is required. In terms of memory overhead, hash still has the advantage.
Method to realize:
Redis Hash corresponding to Value is actually a HashMap. There are two different implementations here. When the Hash has fewer members, Redis will use a one-dimensional array-like method to compactly store it in order to save memory, instead of using a real HashMap. Structure, the encoding of the corresponding value redisObject is zipmap. When the number of members increases, it will automatically be converted into a real HashMap, and the encoding is ht.
- List
Introduction:
list is a linked list of strings sorted in order of insertion, which can be Insert new elements at the head and tail (implemented by a doubly linked list, the time complexity of adding elements at both ends is O(1)). When inserting an element, if the key does not exist, redis will create a new linked list for the key. If all elements in the linked list are removed, the key will also be removed from redis.
Commonly used commands: lpush, rpush, lpop, rpop, lrange, etc.
Application scenarios:
Various lists, such as Twitter’s follow list, fan list, etc., latest news rankings, comments on each article, etc. can also be implemented using Redis’ list structure.
Message queue, you can use the PUSH operation of Lists to store tasks in Lists, and then the worker thread uses the POP operation to take out the tasks for execution. The message queue here does not have an ack mechanism. What if the consumer gives the task to Pop and crashes before finishing it? One solution is to add an additional sorted set. When distributing, send it to both the list and the sorted set at the same time. The distribution time is used as the score. After the user completes the task, he must use ZREM to eliminate the jobs in the sorted set and periodically remove the jobs from the sorted set. Remove the unfinished tasks that have timed out and put them back into the list. Another approach is to add an extra list for each worker, use RPopLPush when popping up tasks, put the job into the worker's own list at the same time, and use LREM to eliminate it when completed. If the cluster management (such as zookeeper) finds that the worker has died, the worker's list content will be returned to the main list.
Using LRANGE can easily realize the paging function of list content.
Operation to get the latest N data: LPUSH is used to insert a content ID and store it as a keyword at the head of the list. LTRIM is used to limit the number of items in the list to a maximum of 5000. If the amount of data the user needs to retrieve exceeds this cache capacity, then the request needs to be sent to the database.
Implementation method:
The implementation of Redis list is a two-way linked list, which can support reverse search and traversal, which is more convenient to operate, but it brings some additional memory overhead. Redis internal Many implementations, including send buffer queues, etc., also use this data structure.
- Set
Introduction: It is an unordered set. The elements in the set have no order. repeat. Putting duplicate elements into a Set will automatically remove them.
Common commands:
sadd, spop, smembers, sunion, etc.
Application scenarios:
Some lists need to be deduplicated, and set provides an important interface to determine whether a member is in a set collection, which is something that list cannot provide.
You can store some collective data. For example, in the Weibo application, you can store all the followers of a user in a collection, and store all their fans in a collection. Redis also provides operations such as intersection, union, and difference for collections, which can be very convenient to implement functions such as joint attention, common preferences, and second-degree friends. For all the above collection operations, you can also use different command selections. Return the results to the client or save them in a new collection. For another example, QQ has a social function called "Friend Tags". You can tag your friends, such as "big beauty", "tycoon", "Ouba", etc. Here you can also store each user's tags in within a collection.
If you want to know how many specific registered users or IP addresses have visited a certain page, you can do this: SADD page:day1:
Implementation method:
The internal implementation of set is a HashMap whose value is always null. In fact, it is quickly sorted by calculating hash. This is also what set can provide to determine whether a member is Reason within the set.
- Sorted Set
Introduction: Ordered sets, compared to sets, need to be provided when elements are put into the set The element's score can be automatically sorted based on the score.
Commonly used commands:
zadd, zrange, zrem, zcard, etc.
Usage scenarios:
Store an ordered and non-duplicate set list, For example, Twitter's public timeline can be stored with the publication time as the score, so that it will be automatically sorted by time when retrieved.
You can make a weighted queue. For example, the score of ordinary messages is 1, and the score of important messages is 2. Then the worker thread can choose to obtain the work tasks in the reverse order of the score. Prioritize important tasks.
Expired item processing: Use unix time as the key to keep the list sorted by time. Retrieve current_time and time_to_live to complete the difficult task of finding expired items. Another background task uses ZRANGE...WITHSCORES to query and delete expired entries.
Implementation method:
Redis sorted set internally uses HashMap and skip list (SkipList) to ensure the storage and ordering of data. HashMap places the mapping from members to scores, and skip All members are stored in the table, and the sorting basis is the score stored in the HashMap. Using the jump table structure can achieve higher search efficiency, and is relatively simple to implement.
Recommended learning: Redis video tutorial
The above is the detailed content of Summarize Redis data types and usage scenarios. For more information, please follow other related articles on the PHP Chinese website!

Redis stands out because of its high speed, versatility and rich data structure. 1) Redis supports data structures such as strings, lists, collections, hashs and ordered collections. 2) It stores data through memory and supports RDB and AOF persistence. 3) Starting from Redis 6.0, multi-threaded I/O operations have been introduced, which has improved performance in high concurrency scenarios.

RedisisclassifiedasaNoSQLdatabasebecauseitusesakey-valuedatamodelinsteadofthetraditionalrelationaldatabasemodel.Itoffersspeedandflexibility,makingitidealforreal-timeapplicationsandcaching,butitmaynotbesuitableforscenariosrequiringstrictdataintegrityo

Redis improves application performance and scalability by caching data, implementing distributed locking and data persistence. 1) Cache data: Use Redis to cache frequently accessed data to improve data access speed. 2) Distributed lock: Use Redis to implement distributed locks to ensure the security of operation in a distributed environment. 3) Data persistence: Ensure data security through RDB and AOF mechanisms to prevent data loss.

Redis's data model and structure include five main types: 1. String: used to store text or binary data, and supports atomic operations. 2. List: Ordered elements collection, suitable for queues and stacks. 3. Set: Unordered unique elements set, supporting set operation. 4. Ordered Set (SortedSet): A unique set of elements with scores, suitable for rankings. 5. Hash table (Hash): a collection of key-value pairs, suitable for storing objects.

Redis's database methods include in-memory databases and key-value storage. 1) Redis stores data in memory, and reads and writes fast. 2) It uses key-value pairs to store data, supports complex data structures such as lists, collections, hash tables and ordered collections, suitable for caches and NoSQL databases.

Redis is a powerful database solution because it provides fast performance, rich data structures, high availability and scalability, persistence capabilities, and a wide range of ecosystem support. 1) Extremely fast performance: Redis's data is stored in memory and has extremely fast read and write speeds, suitable for high concurrency and low latency applications. 2) Rich data structure: supports multiple data types, such as lists, collections, etc., which are suitable for a variety of scenarios. 3) High availability and scalability: supports master-slave replication and cluster mode to achieve high availability and horizontal scalability. 4) Persistence and data security: Data persistence is achieved through RDB and AOF to ensure data integrity and reliability. 5) Wide ecosystem and community support: with a huge ecosystem and active community,

Key features of Redis include speed, flexibility and rich data structure support. 1) Speed: Redis is an in-memory database, and read and write operations are almost instantaneous, suitable for cache and session management. 2) Flexibility: Supports multiple data structures, such as strings, lists, collections, etc., which are suitable for complex data processing. 3) Data structure support: provides strings, lists, collections, hash tables, etc., which are suitable for different business needs.

The core function of Redis is a high-performance in-memory data storage and processing system. 1) High-speed data access: Redis stores data in memory and provides microsecond-level read and write speed. 2) Rich data structure: supports strings, lists, collections, etc., and adapts to a variety of application scenarios. 3) Persistence: Persist data to disk through RDB and AOF. 4) Publish subscription: Can be used in message queues or real-time communication systems.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool