Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to record the status of online users by redis bitmap method

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is about how redis records the status of online users through bitmap. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Preface

Before moving on to today's topic, let's briefly explain what the bitmap in Redis is. The official Redis documentation describes bitmaps as follows:

A bitmap is not a real data type, but a collection of bit-oriented operations defined on a string type. Because the string type is a binary-safe binary large object, and the maximum length is 512MB, it is suitable to set 2 ^ 32 different bits.

Bit operations are divided into two groups: constant-time single-bit operations, such as setting a bit to 1 or 0, or getting the value of that bit. An operation on a set of bits, such as calculating the number of bits in a specified range.

The biggest advantage of bitmaps is that it is sometimes a very significant way to save space to store information. For example, in a system where different users are represented by an incremental user ID, the memory of 512MB can be used to represent the individual bit information of 4 million users (such as whether they need to receive letters).

In short, bitmap operations are used to manipulate bits, which have the advantage of saving memory space. Why can you save memory space? If we need to store the login status of 1 million users, we only need at least 1 million bits to use the bitmap (bit 1 means login, bit 0 means not logged in), and if it is stored in the form of a string, such as userId as key, whether to log in (string "1" means login, string "0" means not logged in) is stored for value You need to store 1 million strings, which takes up much less space than using bitmap storage, which is the advantage of bitmap storage.

In the past few days, I came across a case at work, that is, I need to implement a record of the online user status of IM. I checked many ways to implement it at that time. Let's share it today.

Main ideas

Construct a bitmap that contains binary data, such as: 1010101. Modify the user's online status by modifying 0 and 1 in the corresponding position of userId. Because the default value is 0, 1 means the user is online, and 0 means the user is offline, as shown in the figure:

Three bitmaps of Mon, Thus and Web are constructed.

Three bitmaps of Mon, Thus and Web are constructed. For Mon, the user of userId=1 is online, the user of userId=2 is offline, and the user of userId=3 is online. When the user of userId=10 is online, the upper value of the 10th is changed to 1.

Spatial estimation

1 bit of binary data is 1bit

1 gigabyte (gb) = 8589934592 bits (bit)

Theoretically, one gigabyte of memory can record more than 8.5 billion of the user's status. If the userId is not coherent, some userId bits are more than 8.5 billion bits, which can be solved by using some algorithms or segmenting the userId bit by bit.

Use the command

In setbit key offset value modified key, the value of offset bit is value.

Setbit

Getbit key offset acquires the value on the offset bit in key

Getbit

Bitcount key counts the number of 1s in key

Bitcount

Bitop op destKey key1 key2... .. Where op can be AND (in), OR (or), NOT (non), XOR (XOR)

The main function of the command is to give key1 and key2.. And so on, this kind of binary data is calculated logically by bit, and the result is paid to destkey. The position without setbit defaults to 0.

Take a chestnut.

Three bitmaps of Mon, Thus and Web are constructed as the bitmaps of the login status in the past three days.

Three bitmaps of Mon, Thus and Web are constructed.

First day

Users of userId=10000,userId=9999,userId=8888 have logged in

Setbit mon 10000 1; setbit mon 9999 1; setbit mon 8888 1

Count the number of online users on the first day

Bitcount mon

Three users are logged in

Three users logged in on the first day

The second day

Users of userId=9999,userId=7777 have logged in

Setbit thus 9999 1; setbit thus 7777 1

Get userId=7777 user presence status

Getbit thus 7777

7777 users are logged in

The third day

Users of userId=9999,userId=6666 have logged in

Setbit web 9999 1; setbit web 6666 1

Get the number of users who have logged in for three days

Bitop and resultand mon thus web

Get the number of users who have logged in for three days

Get the number of users who have logged in within three days

Bitop or resultor mon thus web

Get the number of users who have logged in within three days

Redis uses bitmap method to record the status of online users. This is all for you. Welcome to communicate and point out some mistakes in the article. Let me deepen my understanding.

Thank you for reading! This is the end of the article on "how redis records the status of online users through bitmap method". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report