In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
This article is about the usage scenarios of five data structures in Redis. The editor thought it was very practical, so I shared it with you as a reference. Let's follow the editor and have a look.
1. Redis data structure usage scenario
Originally read redisbook this book, have been familiar with the basic functions of redis, from last week began to read the source code of redis. The current goal is to understand the data structure of redis. As we all know, there are five data structures in redis, so what is the usage scenario for each data structure?
String-- string
Hash-- dictionary
List-- list
Set-- collection
Sorted Set-- ordered set
Let's briefly explain their respective usage scenarios:
1. Stringmuri-string
The String data structure is a simple key-value type, and value can be either a String or a number (encoding is an integer when a numeric type can be represented by Long, and the rest is stored in sdshdr as a string). Using the Strings type, you can fully implement the functions of the current Memcached and be more efficient. You can also enjoy the timing persistence of Redis (you can choose RDB mode or AOF mode), operation log, Replication and other functions. In addition to providing the same get, set, incr, decr, and so on operations as Memcached, Redis also provides the following operations:
1.LEN niushuai:O (1) gets the string length 2.APPEND niushuai redis: to the string append content, and uses intelligent memory allocation (2 times each time) 3. 5. Set and get a segment of the string 4. Set and get a bit of the string (bit) 5. 0. Batch set the contents of a series of strings 6. An ingenious use of the atomic counter 7.GETSET command, please set a new value while clearing the old value, and use it with the atomic counter
1. Hashmuri-Dictionary
In Memcached, we often package some structured information into hashmap, which is serialized on the client side and stored as a string value (usually in JSON format), such as the user's nickname, age, gender, points, and so on. At this time, when you need to modify one of these items, you usually need to take out the string (JSON), then deserialize it, modify the value of an item, and then serialize it into a string (JSON) and store it back. Simply modify an attribute to do so many things, it must be very expensive, and it is not suitable for some 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.
Store, read, and modify user attributes
1. List of listings
To put it bluntly, List is a linked list (redis uses double-ended linked lists to implement List). It is believed that anyone who has learned the knowledge of data structure should be able to understand its structure. Using the List structure, we can easily achieve functions such as ranking the latest news (such as TimeLine on Sina Weibo). Another application of List is message queue, which can use the * PUSH operation of List to store the task in List, and then the worker thread uses the POP operation to fetch the task for execution. Redis also provides API for manipulating certain elements in List. You can directly query and delete elements in List.
1. Weibo TimeLine2. Message queue
1. SetMurt-set
Set is a collection, and the concept of a set is a combination of unrepeated values. Using the Set data structure provided by Redis, some collective data can be stored. For example, in the Weibo application, you can put all the followers of a user in a collection and all its fans in a collection. Because Redis is very humanized for the collection to provide intersection, union, difference and other operations, then it can be very convenient to achieve, such as common concern, common preferences, second friends and other functions, for all the above set operations, you can also use different commands to choose to return the results to the client or save the set to a new collection.
1. Common friend, second friend 2. Using uniqueness, you can count all the independent IP3 that visit the website. When recommending a friend, you can recommend it if it is greater than a certain threshold according to the intersection of tag.
1.Sorted Set-- ordered set
Compared with Sets, Sorted Sets adds a weight parameter score to the elements in Set, so that the elements in the collection can be arranged in order by score, such as a Sorted Sets that stores the scores of the whole class, whose collection value can be the student number of students, and score can be its examination score, so that when the data is inserted into the collection, it has been sorted naturally. In addition, Sorted Sets can be used to make weighted queues, such as the score of ordinary messages is 1 and the score of important messages is 2, and then the worker thread can choose to get work tasks in reverse order of score. Give priority to important tasks.
1. An element with weight, such as a game's ranking of user scores
two。 For more complex data structures, not too many scenarios are generally used.
II. Scenarios for the use of other functions of redis
1. Subscribe-publish system
Pub/Sub literally means Publish and Subscribe. In Redis, you can set a key value to publish and subscribe to a message. When a message is published on a key value, all clients subscribing to it will receive the corresponding message. The most obvious use of this function is as a real-time messaging system, such as ordinary real-time chat, group chat and other functions.
1. Transaction-Transactions
Who says that NoSQL does not support transactions? although Redis's Transactions does not provide strict ACID transactions (such as a series of commands committed for execution with EXEC, and the server goes down during execution, then some of the commands will be executed and the rest will not be executed), this Transactions still provides the function of basic command package execution (if there is no problem with the server, it can ensure that a series of commands are executed sequentially. There will be other client commands plugged in and executed). Redis also provides a Watch function, you can Watch a key, and then execute Transactions, in the process, if the value of the Watched has been modified, then the Transactions will find and refuse to execute.
Thank you for reading! This is the end of the use of the five data structures in Redis. I hope the above content can be helpful to you, so that you can learn more knowledge. If you think the article is good, you can share it and let more people see it.
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.