In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what are the core data types of Redis". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn "what are the core data types of Redis"?
String string
The tring type is binary safe, that is, the string can contain any data.
The ordinary string in Redis adopts raw encoding, which is the original coding method, which dynamically expands the capacity and reduces the overhead of frequent memory allocation by pre-allocating redundant space in advance.
When the string length is less than 1MB, it is allocated according to 2 times the required length, and if it exceeds 1MB, it is pre-allocated according to the additional capacity of 1MB each time.
The numbers in Redis are also stored as string types, but the encoding method is different from that of ordinary string. The numbers are encoded as integers, and the string content is directly set to the binary byte sequence of integer values.
The string type of Redis can be used when storing normal strings, serialization objects, and counters, and the instructions used for string data types include set, get, mset, incr, decr, and so on.
List list
List list, a fast two-way linked list that stores a series of string type string values
For regular pop and push elements, the performance is high and the time complexity is O (1), because the list is directly appended or popped up. However, for random insertion, random deletion, and random range acquisition, the polling list is needed to determine the location, so the performance is relatively low.
When operating the list list, you can use lpush, lpop, rpush, rpop, lrange to perform routine queue entry and exit operations and range acquisition operations. In some special scenarios, you can also use lset and linsert for random insertion operations, and lrem for specified element deletion operations. Finally, when consuming the message list, you can also use Blpop and Brpop for blocking acquisition, so that when there are no elements in the list, you can quietly wait for the insertion of new elements without the need for additional continuous queries.
Set collection
Set is an unordered collection of type string, and the elements in set are unique, that is, there are no duplicate elements in set. Collections in Redis are generally implemented through the dict hash table, so insert, delete, and query elements can be located directly according to the element hash value, with a time complexity of O (1).
Operation
The sismember instruction determines whether there is an element in the set data structure corresponding to the key, and returns 1 if so, otherwise 0
Sdiff instruction to perform subtraction on multiple set collections
The sinter instruction performs the intersection of multiple sets
The sunion instruction performs union on multiple sets
The spop instruction pops up a random element
The srandmember instruction returns one or more random elements.
In social systems, it can be used to store a list of followed friends, to determine whether or not to follow, and to make recommendations for friends. In addition, the uniqueness of set can be used to accurately count the source business and source IP of the service.
Sorted set ordered set
In an ordered collection, each element is associated with a score score value of type double. Ordered collections are sorted from smallest to largest by this score value. In an ordered collection, elements are not allowed to repeat, but score score values are allowed to repeat.
Operation
Zscan directive: gets the elements in an ordered collection in order
Zscore directive: gets the score value of an element
Zrange directive: returns elements within the specified score range by specifying score
When the score value of an element changes, you can also add or subtract the score value of the element through the zincrby instruction.
The intersection and union of multiple ordered sets are carried out through zinterstore and zunionstore instructions, and then the new ordered set is stored in a new key. If there are repeating elements, the score of the repeating elements is added, and then it is used as the score value of the element in the new set.
You can use ordered sets to count rankings, refresh lists in real time, and record student scores, so as to easily obtain a list of students within a certain range of scores. It can also be used to add weighted values to the system statistics, so as to display them in dashboard in real time.
Hash hash
Slightly
Bitmap
The bitmap is a series of continuous binary numbers, and the underlying layer is actually encapsulated and stored based on string.
Instructed to operate according to the bit bit. The position of each bit bit in bitmap is offset offset. You can use setbit or bitfield to set 0 or 1 for each bit in bitmap, you can also use bitcount to count the bit number of 1 in bitmap, and you can also use bitop to sum, OR, XOR and other operations on multiple bitmap.
The characteristic of bitmap bitmap is that bitwise setting, summing, OR and other operations are very efficient, and the storage cost is very low. If it is used to store object tag attributes, a bit can save a tag. You can use bitmap to save the user's login status for the last N days, using 1 bit per day, and setting 1 for login.
Personalized recommendation is very important in social applications. You can set a series of tags for news and feed, such as military, entertainment, video, pictures, text, etc., use bitmap to store these tags, and place 1 on the corresponding tag bit bit. For users, we can also use a similar way to record a variety of attributes of users, and can be very convenient to carry out multi-dimensional statistics according to the label. The important instructions of bitmap bitmap include: setbit, getbit, bitcount, bitfield, bitop, bitpos and so on.
Use experience
Statistics of user login: login within 1 235 days
Bitmap: 1 1 1 0 1
GEO geographical location
When storing a location point, firstly, using the Geohash algorithm, the two-dimensional longitude and latitude of the location are mapped and encoded into an one-dimensional 52-bit integer value, and the location name and longitude and latitude coding score are stored in the sorted set corresponding to the classification key as key-value pairs.
When we need to calculate the people near a location point A, we first take the specified position An as the center point and the distance as the radius to calculate the range of 8 directions of the GEO hash, and then poll all the location points in turn, as long as the distance from these location points to the center position An is within the required distance, it is the target location point. After polling all the location points in the range, reorder all the targets near location point A.
Using geoadd, add the location name (such as person, vehicle, store name) and corresponding geolocation information to the specified location classification key
Use geopos to easily query the location information of a name
Use georadius to get all elements near a specified location and no more than a specified distance
Redis GEO geographical location, using Geohash to convert a large number of two-dimensional longitude and latitude into one-dimensional integer values, so that it is convenient to query the geographical location, distance measurement, range search. However, due to the large number of geographical locations, there may be a large number of elements under a geographic classification key, so it is necessary to plan in advance when designing a GEO to avoid excessive expansion of a single key.
Redis's GEO geolocation data structure has many applications, such as querying the specific location of a place, checking the distance from the current location to the destination, checking nearby people, restaurants, cinemas, and so on. In GEO geolocation data structure, important instructions include geoadd, geopos, geodist, georadius, georadiusbymember and so on.
Use geodist to get the distance between the two specified locations.
HyperLogLog cardinality statistics
HyperLogLog is a data type used to do cardinality statistics. When entering a large number of elements to do statistics, it only needs a small amount of memory to complete. HyperLogLog does not save metadata, but only records the estimated number of elements to be counted, which is an approximate value with a standard deviation of 0.81%. In most business scenarios, an error of less than 1% is acceptable for massive data.
When counting the HyperLogLog of Redis, if the number of counts is small, the space occupied by sparse matrix will gradually increase with the increase of count. When the threshold is exceeded, the space occupied by dense matrix will be changed to dense matrix, and the space occupied by dense matrix is fixed, about 12KB bytes.
Through the hyperLoglog data type, you can use pfadd to add new elements to the cardinality statistics, you can use pfcount to obtain the approximate cardinality number stored in the hyperLogLog structure, and you can also use hypermerge to merge multiple hyperLogLog into a hyperLogLog structure, thus you can easily get the cardinality number after the merger.
The characteristic of hyperLogLog is that the statistical process does not record independent elements and occupies very little memory, so it is very suitable for statistics of massive data. In large and medium-sized systems, counting the daily and monthly UV, that is, the number of unique visitors, or counting the number of independent terms searched by a large number of users, can be processed with the hyperLogLog data type.
At this point, I believe you have a deeper understanding of "what are the core data types of Redis". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.