1,geospatial
It mainly specifies the geospatial location (latitude, longitude and name), which can calculate the information of geographical location, the distance between the two places, the people within a few miles, etc!
City longitude and latitude query address: http://www.jsons.cn/lngcode/
Official document address: https://www.redis.net.cn/order/3685.html
(1)geoadd
# Syntax: geoadd parameter key value (longitude and latitude name) # Function: adds the specified geospatial location (latitude, longitude, name) to the specified key # Range: valid longitude from - 180 degrees to 180 degrees. # The effective latitude ranges from -85.05112878 degrees to 85.05112878 degrees. # When the coordinate position exceeds the above specified range, the command will return an error.
(2)geopos
# Syntax: geopos parameter value (name) # Function: returns the position (longitude and latitude) of all the given positioning elements from the key # Return value: returns an array. Each item in the array consists of two elements: # The first element is the longitude of the given positioning element, # The second element is the latitude of the given location element. # When the given location element does not exist, the corresponding array item is null.
(3)geodist
# Syntax: the unit parameter specified by the geodist parameter value (name) # Function: returns the distance between two given positions # Return value: the calculated distance will be returned in the form of double precision floating-point number. # If the given location element does not exist, the command returns a null value. # Parameters: Parameters for specifying units unit Must be one of the following: m Indicates in meters. km Expressed in kilometers. mi Indicates in miles. ft Indicates in feet. If the user does not explicitly specify the unit parameter, then GEODIST The default unit is meters.
(4)georadius
# Syntax: georadius parameter longitude latitude radius [withcool withlist] WITHDIST: Returns the distance between the location element and the center while returning the location element Also return together. The unit of distance is consistent with the range unit given by the user. WITHCOORD: The longitude and dimension of the location element are also returned. # Function: find out the elements within a certain radius with the given longitude and latitude as the center
(5)georadiusbymember
# Syntax:# Syntax: georadius parameter value (name) radius [withcool withlist] # Function: find out the elements within the specified range. The center point is determined by the given location element # Difference: difference from deoradius: deoradius: The center point is determined by the entered latitude and longitude georadiusbymember: The center point is determined by a given location element
Underlying principle:
The underlying implementation principle of geo is actually Zset! So we can also use the Zset command to operate geo!
2,hyperloglog
Hyperlog is an algorithm for cardinality statistics
The advantage of hyperlog is that when the number or volume of input elements is very, very large, the space required to calculate the cardinality is always fixed and very small.
In Redis, each hyperlog key only needs 12 KB of memory to calculate the cardinality of nearly 2 ^ 64 different elements. This is in sharp contrast to a collection where the more elements consume more memory when calculating the cardinality.
What is cardinality?
For example, if the dataset {1, 3, 5, 7, 5, 7, 8}, the cardinality set of the dataset is {1, 3, 5, 7, 8}, and the cardinality (non repeating elements) is 5. Cardinality estimation is to quickly calculate the cardinality within the acceptable error range.
Example: page views of web pages (a person visits a website many times, but still counts as a person)
Traditional method: set saves the user's id, and then you can count the number of elements in set as the judgment standard!
Deficiency: if a large number of user IDs are saved in this way, it will be troublesome to compare prices. Because our purpose is to count, not to save the user id.
(1)pfadd
# Syntax: PFADD key element [element...] # Function: add specified elements to hyperlog
(2)pfcount
# Syntax: PFCOUNT key [key...] # Function: returns the cardinality estimate of a given hyperlog
(3)pfmerge
# Syntax: PFMERGE destkey sourcekey [sourcekey...] # Function: merge multiple hyperloglogs into one HyperLogLog
3,Bitmaps
What is Bitmaps? (bit storage)
-
Bitmaps is not an actual data type, but a collection of byte oriented operations defined on the String type. Because strings are binary secure blocks, their maximum length is 512M, which is best set to 2 ^ 32 different bytes.
-
bitmaps' bit operations fall into two categories:
-
1. Fixed time single bit operation, such as setting a bit of String to 1 or 0, or obtaining a value on a bit
-
2. For the operation of a group of bits, count the number with the set value of 1 within a given bit range (such as population statistics).
-
-
The biggest advantage of bitmaps is that it can save a lot of space when storing data. For example, if you use a self growing id to identify users in a project, you can only use 512M memory to record the information of 4 billion users (for example, if you want to receive a new notification, use 1 and 0)
Usage scenario:
Because each position of bit array can only store 0 or 1 states; Therefore, in real life, bitmaps can be considered for business scenarios dealing with two states. Such as user login / not login, sign in / not sign in, pay attention / not pay attention, punch in / not punch in, etc. At the same time, bitmap also makes rapid statistics through relevant statistical methods.
(1)setbit
# Syntax: SETBIT key offset value # Function: set or clear the bit value of key's value (string) at offset, which is determined by value (only 0 or 1). # Return value: the original bit value at offset
Example:
For example, a platform needs to count the activity of users every day (a user who logs in on the same day is an active user on the same day), and a new bitmap needs to be generated every day to store the login status of users on the same day.
The design here is: setbit active:{date} {userId} value; For example, store the user activity of the platform on 2021-08-01, key=active:2021-08-01;
(2)getbit
# Syntax: GETBIT key offset # Function: returns the bit value of the string corresponding to the key at offset. # Return value: bit value at offset
Example:
For example, query whether the user with userId=666 logs in on August 1, 2021:
(3)bitcount
# Syntax: BITCOUNT key [start end] # Function: counts the number of bit s whose string is set to 1. # Return value: the number of bits set to 1
Example:
For example, count the number of active users (login) on the platform on August 1, 2021: