In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "what are Redis data types and usage scenarios". In daily operation, I believe many people have doubts about Redis data types and usage scenarios. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "Redis data types and usage scenarios". Next, please follow the editor to study!
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 five data types. The following describes each of these five data types and their usage scenarios and internal implementation.
String
Introduction: string type is the most basic data type in Redis, the most commonly used data type, and even used by many players as the only data type of redis. The string type is binary safe in redis, which means that string values care about binary strings, not the specific format, and you can use it to store strings in json format or JPEG image format.
Get string length
To the string append content
Set and get a segment of a string
Set and get a bit of a string (bit)
Set the contents of a series of strings in batch
Common commands: set,get,decr,incr,mget, etc.
Application scenarios:
(1) Store the value of a field in MySQL
Design key as table name: primary key name: primary key value: field name
(2) Storage object
The string type supports strings of any format, and the most common application is to store strings formatted by json or other objects. (hash data type is recommended 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 self-increasing id
When the value of the string type of redis is in the form of an integer, redis can incr it as if it were an integer. Because all the operations of redis are atomic, you don't have to worry about transaction problems that may occur when multiple clients connect.
Hash
Summary: Hash stores the mapping between strings and string values. Hash stores the properties of an object in Map and can read / update only some of the properties of the object. In this way, some properties are too long to leave it motionless, and different modules can only update the properties they care about without cocurring with each other.
Common commands: hget,hset,hgetall, etc.
Application scenarios:
Store structured data, such as user information. In Memcached or CKV, for user information such as user's nickname, age, gender, points, etc., we need to serialize and then store the value as a string. When we need to modify one of the items, we usually need to take out and deserialize all the values, modify the value of a certain item, and then serialize and store it back. This not only increases the overhead, but also does not apply to situations where concurrent operations are possible (for example, two concurrent operations need to modify the integral). The Hash structure of Redis allows you to modify only one attribute value as if you were Update 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 value is the attribute value, so the data can be modified and accessed directly through the Key of the internal Map (the key of the internal Map in the Redis is called field), that is, through key (user ID) + field (attribute tag), the corresponding attribute data can be manipulated without repeated storage, serialization and concurrency modification control.
However, it should be noted that Redis provides an hgetall to fetch all the attribute data directly, but if there are many members of the internal Map, it involves traversing the entire internal Map. Due to the Redis single-threaded model, this traversal operation may be time-consuming and completely unresponsive to requests from other clients, which requires special attention.
Can be used to build an index. For example, for User objects, in addition to id and sometimes query by name, you can build a Hash with Key as user:name:id. When inserting User objects (set user:101 {"id": 101, "name": "calvin"}), insert a bar into this hash (hset user:name:id calvin 101and calvin as a key in hash, with a value of 101l). When you press name to query, you can use hgetuser:name:id calvin to extract id from the key named calvin. If you need to use multiple indexes to find a piece of data, you can use one hash key to avoid using multiple string key to store index values.
HINCRBY can also be used to implement idmaker. Relative to the idmaker of string type, each type requires a key,hash type to use a key.
The hash data type has the advantage of more flexibility and faster than the string type in storing the above types of data. specifically, using string type storage, it is necessary to convert and parse the string in json format. Even if no conversion is needed, hash still has the advantage in terms of memory overhead.
Implementation method:
Redis Hash is actually a HashMap inside the Value. There are two different implementations. When the number of members of this Hash is small, the Redis will use a compact storage method similar to an one-dimensional array to save memory, instead of using the real HashMap structure. The encoding of the corresponding value redisObject is zipmap. When the number of members increases, it will be automatically converted to the real HashMap, and the encoding will be ht.
List
Brief introduction:
List is a string linked list sorted in insertion order, and new elements can be inserted at the head and tail (bidirectional linked list implementation, the time complexity of adding elements at both ends is O (1)). When you insert an element, if the key does not exist, redis creates a new linked list for the key, and if all the elements in the linked list are removed, the key is also removed from the redis.
Common commands: lpush,rpush,lpop,rpop,lrange, etc.
Application scenarios:
Various lists, such as twitter's follow list, fan list, ranking of the latest news, comments on each article, etc., can also be implemented using Redis's list structure.
Message queuing can take advantage of the PUSH operation of Lists to store the task in Lists, and then the worker thread uses the POP operation to fetch and execute the task. There is no ack mechanism in the message queue here. What if the consumer leaves the task to Pop and crashes before it is finished? One of the solutions is to add an additional sorted set, and send it to both list and sorted set at the same time, so that the distribution time is score. After the user has finished the task, he / she needs to use ZREM to eliminate the job in the sorted set, and regularly take out the unfinished tasks from the sorted set and put them back into the list. Another way is to add an extra list for each worker, use RPopLPush when popping up tasks, put job in worker's own list at the same time, and eliminate it with LREM when finished. If the cluster management (such as zookeeper) finds that the worker is dead, it puts the list content of the worker back into the main list.
The function of list content paging can be easily realized by using LRANGE.
Operation to take the latest N pieces of data: LPUSH is used to insert a content ID, which is stored as a keyword in the header 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 retrieved by the user exceeds the cache capacity, the request needs to be sent to the database.
Implementation method:
The implementation of Redis list is a two-way linked list, that is, it can support reverse search and traversal, which is more convenient to operate, but it brings some additional memory overhead. Many implementations within Redis, including send buffer queues, also use this data structure.
Set
Introduction: it is a kind of unordered collection in which the elements in the collection are not sequenced and do not repeat. Putting duplicate elements into Set will automatically remove the weight.
Common commands:
Sadd,spop,smembers,sunion et al.
Application scenarios:
There are some lists that need to be deduplicated, and set provides an important interface to determine whether a member is in an set collection, which list cannot provide.
You can store some collective data, such as in Weibo applications, you can store all the followers of a user in a collection and all their fans in a collection. Redis also provides intersection, union, difference and other operations for the collection, which is very convenient to achieve, such as common concern, common preferences, second-degree friends and other functions. For all the above collection operations, you can also use different commands to choose whether to return the results to the client or save to a new collection. For example, QQ has a social function called "friend tag". People can tag your friends, such as "Beauty", "Tuhao", "Brother" and so on. Here, you can store each user's tag in a collection.
If you want to know how many registered users or IP addresses have visited a page, you can do this: SADD page:day1:. To know the number of specific users, use SCARD page:day1:. Need to test whether a particular user has visited this page? SISMEMBER page:day1: .
Implementation method:
The internal implementation of set is a HashMap in which value is always null. In fact, the weight is arranged quickly by calculating hash, which is why set can determine whether a member is in the collection or not.
Sorted Set
Introduction: ordered collection, compared to set, the element into the collection, but also provide the score of the element, can be sorted automatically according to the score.
Common commands:
Zadd,zrange,zrem,zcard et al.
Use the scene:
Store an ordered and non-repeating list of collections, such as twitter's public timeline, which can be stored as a score at the time of publication, so that the fetches are automatically sorted in time.
You can do weighted queues, such as score 1 for ordinary messages and score 2 for important messages, and then the worker thread can choose to get work tasks in reverse order of score. Give priority to important tasks.
Overdue items processing: use unix time as the keyword to keep the list sorted by time. Search current_time and time_to_live to complete the arduous task of finding overdue items. Another background task uses ZRANGE... WITHSCORES makes a query to delete expired entries.
Implementation method:
Redis sorted set internal use HashMap and jump table (SkipList) to ensure data storage and order, HashMap is put in the member to score mapping, while the jump table is stored in all the members, sorting according to the score stored in HashMap, the use of jump table structure can achieve higher search efficiency, and relatively simple in implementation.
At this point, the study on "what are the Redis data types and usage scenarios" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.