What else can redis do?

Time:2021-10-23

In the previous article, we talked about the usage scenarios of the five basic data types of redis. In addition to string, hash, list, set and Zset, redis also provides some other data structures (of course, it is not a data structure in the strict sense). Let’s see what else redis can do?

One bitmaps

In the computer, binary is used as the basic unit of information, that is, any input information will eventually be transferred to a string of binary numbers at the bottom of the computer. In redis, bitmaps are provided for bit operations. We can think of bitmaps as an array in bits, and the subscript of the array is called offset. The advantage of using bitmaps is that it takes less space.

If we want to record whether an employee has logged in to the company’s official website today, we can use the date as the key and the employee ID as the offset (where the employee ID is self incremented in the database). If the ID starts from 1000, in order to save space, we will generally subtract this initial value from the employee ID as the offset. The offset is generally from 10000Start. Do you want to access the official website0and1To show.

In this case, the ID is3The employee visited the official website and wrote his value as1

#The employee with ID 3 visited the official website
setbit user:2020-11-04 3 1
#The employee with ID 18 visited the official website
setbit user:2020-11-04 18 1

Check whether an employee has visited the official website

getbit user:2020-11-04 1

Query the number with a value of 1 in the specified range (bytes). For example, I want to see how many employees with IDs from 1 to 30 have accessed the official website

bitcount user:2020-11-04 0 3

II. Hyperlog

HyperLogLogData statistics can be completed by using a very small memory space. A single piece of data cannot be obtained. It can only be used for statistics, and there will be a certain error rate.

If I want to count the IP addresses accessing the official website

Add the IP list accessed by the official website today

#IP accessed on November 4, 2020
pfadd 2020-11-04:ip "ip1" "ip2" "ip3" 
#IP accessed on November 5, 2020
pfadd 2020-11-05:ip "ip3" "ip4" "ip5"

Calculate the number of IP addresses accessed on the official website today

pfcount 2020-11-04:ip

The returned result is 3

Check how many independent IPS have visited the website in 2020-11-04 and 2020-11-05

First, merge the data of two days and copy it to a value

pfmerge 2020-11:ip 2020-11-04:ip 2020-11-05:ip

Then use the pfcount command to query, and the value obtained is 5

pfcount 2020-11:ip

III. Geo

stayRedis3.2Geo (geographic location) function is added in the version, which can be used to get people nearby.

The add command is as follows, which can be added in batch

geoadd city longitud latitude member

We add the location information of several cities to obtain the cities near a city

geoadd city 116.28 39.55 beijing 117.12 39.08 tianjin

The command to obtain the longitude and latitude of Beijing is as follows

geopos city beijing

View the distance between Beijing and Tianjin

geodist city beijing tianjin km

The last km indicates the distance, and the unit is km. The following units are supported:

  • m. Meters
  • Km, KM
  • Mi, miles
  • FT, ruler

There are two commands to get the location nearby,georadiusObtained according to longitude and latitude,georadiusbymemberGet by member

Georadius key longitude latitude [unit]
Geordiusbymember key member [unit]

Non mandatory parameters can also be followed. The parameters are as follows:

  • Withcoord: longitude and latitude are included in the returned result
  • Withlist: the return result contains the distance from the center
  • Withhash: the returned result contains geohash (that is, convert longitude and latitude into hash value)
  • Count: Specifies the number of returned results
  • Asc|desc: the returned results are sorted by the distance from the center
  • Store key: saves the geographic location information of the returned result to the specified key
  • Storedest key: saves the distance from the returned result to the center to the specified key

IV. publish subscribe mode messages

In the previous article, it was mentioned that you can use list and Zset to implement message queue, but the message queue implemented above is a point-to-point mode, that is, a message can only be consumed by one consumer. In addition, redis also supports the publish subscribe mode, that is, a message is consumed by all subscribers, such as broadcasts, announcements, etc. after publishing an announcement, all users who pay attention to me can receive the announcement.

  1. Release news

Publish to channel: message a message, the message content ishi

pulish channel:message hi
  1. Subscription channel

Subscribers can subscribe to one or more channels, such as channel: message

subscribe channel:message
  1. Unsubscribe
unsubscribe channel:message
  1. View active channels
pubsub channels
  1. View subscriptions

View the number of channel: Message subscriptions

pubsub numsub channel:message

The publish subscribe mode of redis is slightly rough compared with the professional message middleware, but it is very simple to implement and the learning cost is low.

V bloom filter

Bloom filter isredis4A new feature in version. Its implementation principle is similar to that of bitmaps. It also uses a single digit group to pass your value throughMultipleHash function, get the position of the corresponding bit group, and set these values to 1. Bloom filters are often not used to prevent cache penetration.

If an element does not exist, it must not exist. If an element exists, it may not exist. This is because if there are three elementsabcTo put it in the same array, supposeaAfter three hashes, you can get three positions 1, 5 and 7, and then these three positions will be modified to1bAfter three hashes, three positions 2, 4 and 6 are obtained, and these three positions are modified to1cAfter three hashes, the positions 2, 5 and 7 are obtained, but after the first two element hashes, these three positions have been modified to1Okay, so what can we saycDoes it have to exist? Obviously not!


Focus and don’t get lost

If you think the article is good, welcomefollowgive the thumbs-upCollection, your support is the driving force of my creation. Thank you.

If there is a problem with the writing of the article, please don’t save your writing. Welcome to leave a message and point out that I will check and modify it in time.

If you want to know more about me, you can search “on wechat”Java journey“Pay attention. Reply1024“You can get learning videos and exquisite e-books. Push technical articles on time at 7:30 every day, so that your way to work is not lonely, and there are book delivery activities every month to help you improve your hard power!