This is the 14th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021.

In addition to the five basic data types, Redis also has three special data types, namely HyperLogLogs (cardinal statistics), Bitmaps and Geospatial.

HyperLogLogs (Base statistics)

Redis version 2.8.9 updates the Hyperloglog data structure!

  • What is the cardinal number?

For example, A = {1, 2, 3, 4, 5}, B = {3, 5, 6, 7, 9}; So cardinality (non-repeating elements) = 1, 2, 4, 6, 7, 9; (Error tolerance, that is, acceptance of certain errors)

  • What problems are HyperLogLogs radix statistics used to solve?

This structure can save very much to count all kinds of statistics, such as registered IP number, daily access IP number, page real-time UV, online users, number of mutual friends, etc..

  • What are its advantages?

For a large website with, say, 1 million IP addresses per day, one IP consumes 15 bytes, so 1 million IP addresses is 15 megabytes. However, HyperLogLog occupies 12K for each key in Redis, and its theoretical storage is approximately 2^64 values. No matter what the stored content is, its algorithm based on cardinality estimation can only accurately estimate the cardinality, and it can use a small amount of fixed memory to store and identify the unique element in the set. And the base of this estimate is not necessarily accurate, it is an approximation with 0.81% standard error (which is negligible for business scenarios that accept some tolerance, such as IP number statistics, UV, etc.).

  • Usage of related commands
127.0.0.1:6379> pfadd key1 a b c d e f g h ICreate the first set of elements
(integer) 1
127.0.0.1:6379> pfcount key1					Count the number of cardinals of elements
(integer) 9
127.0.0.1:6379> pfadd key2 c j k l m e g a		Create a second set of elements
(integer) 1
127.0.0.1:6379> pfcount key2
(integer) 8
127.0.0.1:6379> pfmerge key3 key1 key2			# merge two groups: key1 key2 -> key3
OK
127.0.0.1:6379> pfcount key3
(integer13)Copy the code

Bitmap (bit storage)

Bitmaps, or Bitmap data structures, manipulate binary bits for recording, with only 0 and 1 states.

  • To solve what problem?

For example: statistics user information, active, inactive! Logged in, not logged in! Punch, don’t punch! You can use Bitmaps for both!

How much memory does it take to store a year’s worth of clocking? 365 days = 365 bits 1 byte = 8bits 46 bytes!

  • Usage of related commands

Use bitmap to record your clocking from Monday to Sunday! Monday: 1 Tuesday: 0 Wednesday: 0 Thursday: 1……

127.0.0.1:6379> setbit sign 0 1
(integer) 0
127.0.0.1:6379> setbit sign 1 1
(integer) 0
127.0.0.1:6379> setbit sign 2 0
(integer) 0
127.0.0.1:6379> setbit sign 3 1
(integer) 0
127.0.0.1:6379> setbit sign 4 0
(integer) 0
127.0.0.1:6379> setbit sign 5 0
(integer0 127.0.0.1:6379> setbit sign 6 1 (integer) 0
Copy the code

Check to see if you clocked in on a particular day!

127.0.0.1:6379> getbit sign 3
(integer) 1
127.0.0.1:6379> getbit sign 5
(integer) 0
Copy the code

Statistical operation, statistics of the number of days!

127.0.0.1:6379 > bitcount signCount this week's clocking record, you can see whether there is full attendance!
(integer) 3
Copy the code

Geospatial

Redis Geo is available in Redis 3.2! This function can deduce geographical information: the distance between two places, and people within a radius of several miles.

geoadd

Add geographic location

Taiyuan 123.43 41.80 shenyang (123.0.1:6379 > Geoadd China: City 118.76 32.04 Manjing 112.55 37.86 shenyang 123.43 41.80integer) 3 127.0.0.1:6379> Geoadd China: City 144.05 22.52 shengzhen 120.16 30.24 Hangzhou 108.96 34.26 xian (integer) 3
Copy the code

The rules

The two levels cannot be added directly, we usually download the city data (the website can be found at GEO: www.jsons.cn/lngcode)!

  • Effective longitude ranges from -180 degrees to 180 degrees.
  • Valid latitudes range from -85.05112878 degrees to 85.05112878 degrees.
# This command will return an error if the coordinates are outside the range specified above.127.0.0.1:6379> geoadd China: City 39.90 116.40 Beijin (error) ERR invalid longitude, Latitude pair 39.900000,116.400000Copy the code

geopos

Gets the longitude and latitude of the specified member

Geopos China: City Taiyuan Manjing 1) 1)"112.54999905824661255"
   1) "37.86000073876942196"
2) 1) "118.75999957323074341"
   1) "32.03999960287850968"
Copy the code

Get the current position, must be a coordinate value!

geodist

If it does not exist, return null

The unit is as follows

  • m
  • km
  • Mi miles
  • Ft feet
Geodist China: City Taiyuan SHENYANG M"1026439.1070"Geodist China: City Taiyuan Shenyang KM"1026.4391"
Copy the code

georadius

Nearby people ==> Get the addresses of all nearby people, location, and query by radius

A person who gets a specified amount

127.0.0.1:6379> Georadius China: City 110 30 1000km"xian"
2) "hangzhou"
3) "manjing"
4) "taiyuan"127.0.0.1:6379> Georadius China: City 110 30 500 km 1)"xian"127.0.0.1:6379> Georadius China: City 110 30 500 km withDist 1) 1)"xian"
   2) "483.8340"127.0.0.1:6379> Georadius China: City 110 30 1000 km withcoord withdist Count 2 1) 1)"xian"
   2) "483.8340"
   3) 1) "108.96000176668167114"
      2) "34.25999964418929977"
2) 1) "manjing"
   2) "864.9816"
   3) 1) "118.75999957323074341"
      2) "32.03999960287850968"
Copy the code

Parameter Key Latitude and longitude radius unit [latitude and longitude to display results] [distance to display results] [Number of displayed results]

georadiusbymember

Displays other members within a radius of the specified member

Taiyuan 1000 km 1)"manjing"
2) "taiyuan"
3) "xian"12120.1:6379 > Georadiusbymember China: City taiyuan 1000 km withCoord withDist Count 2) 3)"taiyuan"
   2) "0.0000"
   3) 1) "112.54999905824661255"
      2) "37.86000073876942196"
2) 1) "xian"
   2) "514.2264"
   3) 1) "108.96000176668167114"
      2) "34.25999964418929977"
Copy the code

The parameters are the same as georadius

Geohash (less used)

This command returns an 11-character hash string

Taiyuan Shenyang 1)"ww8p3hhqmp0"
2) "wxrvb9qyxk0"
Copy the code

Convert a two-dimensional latitude and longitude into a one-dimensional string. The closer the two strings are, the closer they are

The underlying

The underlying implementation principle of Geo is actually Zset. We can operate Geo through Zset command

127.0.0.1:6379 >type china:city
zset
Copy the code

View all elements and delete the specified element

127.0.0.1:6379> zrange china:city 0 -1 withscores
 1) "xian"
 2) "4040115445396757"
 3) "hangzhou"
 4) "4054133997236782"
 5) "manjing"
 6) "4066006694128997"
 7) "taiyuan"
 8) "4068216047500484"
 9) "shenyang"
1)  "4072519231994779"
2)  "shengzhen"
3)  "4154606886655324"127.0.0.1:6379> Zrem China: City Manjing (integer) 1
127.0.0.1:6379> zrange china:city 0 -1
1) "xian"
2) "hangzhou"
3) "taiyuan"
4) "shenyang"
5) "shengzhen"
Copy the code