Registration function based on Redis bitmap

Source: Internet
Author: User

zhanhailiang 日期:2014-12-21
Demand Scenarios

Bitmap is very effective for some specific types of computations.

Suppose now we want to record the frequency of the users on our site, for example, how many days user a goes online, User B
The number of days, and so on, as data to decide which users to participate in activities such as beta testing-this mode allows
Use Setbit and bitcount to achieve.

For example, whenever a user goes online on a certain day, we use Setbit, with the username as key, and the site represented by that day
The on-line date as the offset parameter, and set the offset to 1.

For example, if today is the 100th day of the Web site, and the user (uid=10086) read the site today, then execute the order setbit sign:10086 100 1, if the user (uid=10086) continue to read the site tomorrow, then execute the command setbit sign:10086 101 1, etc.

When you want to calculate the total number of users (uid=10086) on-line, use the Bitcount command: Execute Bitcount sign:10086, and the result is the total number of days that the user (uid=10086) is online.

Performance

The above line count example, even if run 10 years, occupy the space is only each user 10*365 bit (bit), also is each
User 456 bytes. For this size of data, bitcount processing speed is like get and incr O (1) Complexity of the
Act as fast.

If your bitmap data is very large, consider the following two ways to do it:

1. 将一个大的bitmap分散到不同的key中,作为小的bitmap来处理。使用Lua脚本可以很方便地完成这一工作。2. 使用BITCOUNT的start和end参数,每次只对所需的部分位进行计算,将位的累积工作(accumulating)放到客户端进行,并且对结果进行缓存(caching)。
Code implementation

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

Reference Documentation:
《Redis.pdf》

Registration function based on Redis bitmap

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.