Many applications, such as check-in to send points, check-in to receive rewards:

  • 10 points will be given for 1 day of check-in, 20 points will be given for 2 consecutive days, 30 points will be given for 3 days, 50 points will be given for more than 4 days, etc
  • If continuous check-in is interrupted, reset the count, reset the count at the beginning of each month
  • Displays the number of check-ins a user has in a given month
  • In the calendar control to display the user’s monthly check-in situation, you can switch the year display

The simplest design idea is to use MySQL to store the check-in data (t_user_sign), as follows:

The field name describe
id Alter TABLE primary key (AUTO_INCREMENT)
fk_diner_id The user ID
sign_date Sign-in Date (e.g. 2010-11-11)
amount Consecutive check-in days (e.g. 2)
About a piece of data 50B, it can be calculated that 10 million users continuously check in for five years, then about 800G.
  • User check-in: Inserts a piece of data into this table and updates the number of consecutive check-in days
  • Query Query based on the check-in date
  • Statistics Statistics are collected based on amount

If the data is stored in this way, DB may not be able to support applications with a large number of users, such as 1000W users, one data a day, then a month is 300 million data, very large.

The use of bitmap

Bitmaps are not basic Redis data types (Strings, Lists, Sets, Hashes), but bitwise operations based on String data types, one of the higher-order data types. Bitmaps supports a maximum of 232 bits. Up to 4.29 billion bytes of information can be stored using 512 megabytes of memory (232 = 4,294,967,296).

It is composed of a group of bits, each of which corresponds to two states of 0 and 1. Although it is still stored as String internally, Redis provides some instructions for directly manipulating the bitmap. It can be regarded as an array of bits, and the subscript of the array is the offset.

advantages

Low memory overhead, high efficiency and simple operation, suitable for such scenarios as check-in. Stored by the month, for example, up to 31 days a month, then we will be the month end users sign in cache binary is 00000000000000000000000000000000, when you can sign in one day change 0 to 1, Redis also provides many operations on Bitmap, such as storage, fetch, statistics and other instructions, which are very convenient to use.

Common commands

The command function parameter
SETBIT Specifies the offset bit position setting value Key offset value [0=< offset< 2^32]
GETBIT Query the bit value of the specified offset position key offset
BITCOUNT Count the number of bits 1 in the specified byte range Key [start end] [@lbn]
BITFIELD Operates on multi-byte fields key [GET type offset] [SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP/SAT/FAIL]
BITPOS Query the position of the first bit set to 1 in the specified byte range Key bit [start] [end] [@lbn]

Code implementation

Bit operator determines whether to check in

Collect user check-in statistics

Gets the check-in status of the user in a certain month. The default is the current month. Returns all the dates of the current month and the check-in status of this date.

SignController

SignService: obtains the sign-in status of a month. The default month is:

  • Obtain the login user information
  • Build the Key that Redis saves
  • Get the total number of days in a month (consider February leap, regular year)
  • Get all check-in data of the current month through BITFIELD instruction
  • Traverse to judge whether to check in or not, and store in TreeMap for easy sorting

conclusion

Since the maximum length of the String data type is 512 MB, String supports 2^32 bits. 512M indicates the length of bytes. When converting bits into bits, multiply them by 8, that is, 512 2^10 2^10 x 8=2^32. The maximum length of Strings is 512 megabytes. Can you store more data? Of course not, but we can implement it in a different way, which is to replace the big key with the small key, so that the size of the storage is completely unlimited.