Implement the sign-in Function Based on Redis bitmap and redisbitmap

Source: Internet
Author: User

Implement the sign-in Function Based on Redis bitmap and redisbitmap

Author: zhanhailiang Date:
Scenario

Bitmap is very effective for some specific types of computing.

Assume that we want to record the frequency of online users on our website, for example, how many days have user A been online, and how many days have user B been online?
The number of days, and so on, as data, determines which users are allowed to participate in beta testing and other activities-this mode can be used
Use SETBIT and BITCOUNT.

For example, whenever a User goes online on a certain day, we use SETBIT to use the user name as the key and use the website represented on that day
And set the offset value to 1.

For example, if today is the first day of the website's launch and the user (uid = 100th) has viewed the website today, run the SETBIT sign: 10086 10086 100 1 command; if the user (uid = 10086) will continue reading the website tomorrow, execute the command SETBIT sign: 10086 101 1, and so on.

When you want to calculate the total number of online calls of a user (uid = 10086), run the BITCOUNT command: BITCOUNT sign: 10086. The result is that the user (uid = 10086) the total number of days of the launch.

Performance

In the preceding Statistical example, even if the instance runs for 10 years, the occupied space is only 10*365 bits per user, that is, each
The user is 456 bytes long. For data of this size, the processing speed of BITCOUNT is like the O (1) Complexity of GET and INCR.
The operation is as fast as possible.

If your bitmap data is very large, consider using the following two methods:

1. distribute a large bitmap to different keys and process it as a small bitmap. You can use the Lua script to conveniently complete this task. 2. use the start and end parameters of BITCOUNT to calculate only some of the required bits at a time. Place the accumulated bits (accumulating) to the client and cache the results (caching ).
Code Implementation

Https://github.com/billfeller/billfeller.github.io/blob/master/code/ISign.php

Reference:
《Redis.pdf》

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.