In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to use setbit to count active users in Redis". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use setbit to count active users in Redis".
I. demand background
First of all, let's take a look at the requirements: the website counts the number of times users log in, as follows:
The site has 100 million users, some who log in frequently and some who log in infrequently.
How to record a user's login information
How to query active users: for example, log in 3 times a week
Second, use traditional relational databases
We use traditional relational databases (such as Oracle) to store such data.
Generating 700 million pieces of data per week is very difficult for any relational database to maintain and has a great impact on performance.
3. Setbit operation using Redis
Redis supports binary bit-based setting operations on String-type value. By setting the id of a user to a bit on the value and setting the corresponding bit of the active user, it is possible to record the information of all active users with a value. In the following figure, nine bits of bitmap are set to 1, indicating that the corresponding users in these nine bits are today's active users. The 15th represents the user whose uid is 15, and the first one represents the user whose uid is 0. (if your uid does not start with 1, such as 100000, you can actually use uid minus the initial value to represent the number of digits, for example, 1000000 users correspond to the first bit of bitmap.)
The specific code looks like this:
Redis.setbit (key, user_id, 1)
The complexity of such a record is O (1), which is very fast in Redis.
The following code is a Demo demonstration performed in Redis CLI. For example, if a user with an ID of 100 logs in to the site on Monday, we set the value of the corresponding 100 bits on Monday to 1 through the setbit operation.
127.0.0.1 setbit monday 6379 > 100 (integer) 0
On the other hand, we separate the daily active user status records by switching to a different key every day. And statistics such as N-day active users and connected N-day active users can be calculated through some and / OR operations.
As shown in the figure below, the first line shows the number of active users on Monday, the second line on Tuesday, and so on. For this reason, we can get a list of active users in N days by recording and merging active users in N days.
Thank you for reading, the above is the content of "how to use setbit to count active users in Redis". After the study of this article, I believe you have a deeper understanding of how to use setbit to count active users in Redis, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.