In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
Editor to share with you what kinds of data structures are in redis. I hope you will get something after reading this article. Let's discuss it together.
There are five kinds of data structures in redis database, they are: string- string, Hash- dictionary, List- list, Set- collection, Sorted Set- ordered collection.
These five data structures have different usage scenarios, so let's introduce their usage scenarios.
1 、 String
The String data structure is a simple Key-Value type, and value can be not only a String but also 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 String 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
2 、 Hash
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, and so on. At this time, when you need to modify one of the items, you usually need to take out the string (JSON), then deserialize it, modify the value of an item, and store it back when serialized into a string (JSON). Simply modifying a property does so much, it must be expensive, and it is not suitable for situations where concurrent operations are possible (for example, two concurrent operations need to be modified). 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, modify user attributes)
3 、 List
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 understand its structure. Using the List structure, we can easily achieve functions such as ranking the latest news (such as the TimeLine of Sina blog). 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.
(learn video sharing: redis database tutorial)
4 、 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 collection operations, you can also use different commands to choose to return the results to the client or save 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.
5 、 Sorted Set
Compared with Set, Sorted Set 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 Set 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 Set can be used to do weighted queues, such as score of 1 for ordinary messages and 2 for important messages, and then the worker thread can choose the reverse order of sore to get the work task. Give priority to important tasks.
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 that subscribe to it will receive the corresponding message. The most obvious use of this feature is as a real-time messaging system.
2. Transaction
Who says that NoSql does not support transactions, although redis transactions do not provide strict ACID transactions (such as a series of commands committed and executed by EXEC, and the server goes down during execution, then some of the commands will be executed and the rest will not be executed), but this transaction 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 together in sequence. Other client commands will be inserted and executed in the middle. Redis also provides a watch function that allows you to watch a key and then execute a transaction, and in the process, if the value of the watch is modified, the transaction will discover and refuse to execute.
After reading this article, I believe you have a certain understanding of "what are the data structures in redis". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
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.