Redis bitmap method that records online state of the user

Redis bitmap method that records online state of the user

 bitmap

Redis official documentation for the introduction of the bitmap as follows:

Bitmap is not a real data type, but defined set of operations on the bit string type faces. Because the string type binary blob safe, and the maximum length is 512MB, adapted to set the 2 ^ 32 different bits.

Bit operations divided into two groups: the time constant of a single bit operating as a bit set to 1 or 0, or acquired value of the bit. Operation of a set of bits, the number of bits specified range is set, for example, calculations.

The biggest advantage of the bitmap is sometimes a very significant space-saving way to store information. For example, in a system, different users represented by increasing user ID, 512MB of memory may be used to represent a single bit 4,000,000 user information (e.g., whether they need to receive mail).

Briefly, the operation is used to operate the bitmap bits, the advantage is to save memory space. Why you can save memory space? If we need to store one million users logged in, using a bitmap, then only a minimum of 1,000,000 bits (bit 1 indicates log, bit 0 indicates unregistered) can be stored, if a string storage, for example, to userId is key, is logged on (string "1" indicates that the login, the string "0" indicates not logged in) is stored as a value, then you need to store a string of 1 million, compared to the use of space bitmap memory footprint is much smaller, which is a bitmap storage advantage.

 

Leads to problems

How to effectively statistics:

1 : How to record the user's login

2 : How to check active users (continuous landing)

 

 

Thinking

A bitmap structure, which is stored binary data, such as: 1 0 1 0 0 1 1 0 by modifying the userId and a corresponding position to modify the user online state, the default value is 0, it represents the user is online state 0 represents the user is offline, as shown:

Constructed Mon, Thus, Web three bitmaps for Mon is, userId = 1, the user is online, userId = 2, the user is offline, userId = 3 users in an online state, when the user userId = 10 on the line, put on the value of 1 becomes 10

Estimated spatial 
binary data is a 1bit 
. 1 gigabyte (GB) = 8589934592 bits (bit) 
theoretically 1G memory can record more than 8.5 billion user state, if the userId incoherent, some more than 8.5 billion bits userId bit, you can use a number of algorithms, or for userId bit segments to solve

 

Examples

 Suppose there are eight users

Uid: 1 2 3 4 5 6 7 8 
Monday: 0   . 1. 1 0 0 0 0. 1 
Tuesday: 0   . 1. 1. 1. 1 0 0. 1 
Wednesday: 11011001 
Thursday: 0   . 1. 1 0 0 0. 1 1 
Friday: 11,011,011 
Saturday: 0   1 0 0 0 1 1 1 
seven weeks: 0   1 0 1 0 0 0 1

How do we count users continuous landing one week of it? (Bitwise AND)

One week of continuous landing: 01000001 : II users and user continuous landing VIII

 

Use the command

SetBit key modification key offset value, the value of the offset value of the bit

getbit key offset acquisition value key in on the first offset position

bitcount key key statistics, the number 1

itop op destKey key1 key2 ... .. which may op AND (in), OR (or), the NOT (non), the XOR (exclusive or)
the main role of the command is to key1, key2 .. etc. This binary data , do bitwise logical operation, to pay destkey result, there is no default position setbit 0

 

Show

Uid: 1 2 3 4 5 6 7 8 
Monday: 01010001 
Tuesday: 01110101 
Wednesday: 11011001
repeat 127.0.0.1:6379>setbit my 8 0 
(integer) 0 
repeat 127.0.0.1:6379>setbit my January 2 
(integer) 0 
repeat 127.0.0.1:6379>setbit my January 3 
(integer) 0 
repeat 127.0.0.1: 6379> SetBit my 8 1 
(integer) 0 

repeat 127.0.0.1:6379>setbit feb 8 0 
(integer) 0 
repeat 127.0.0.1:6379>setbit feb January 2 
(integer) 0 
repeat 127.0.0.1:6379>setbit feb 3 1 
(integer) 0 
repeat 127.0.0.1:6379>setbit feb 4 1 
(integer) 0 
repeat 127.0.0.1:6379>setbit feb 6 1 
(integer) 0 
repeat127.0.0.1:6379>setbit feb  8 1
(integer) 0 




redis 127.0.0.1:6379>setbit wen 8 0
(integer) 0 
redis 127.0.0.1:6379>setbit wen 1 1
(integer) 0 
redis 127.0.0.1:6379>setbit wen 2 1
(integer) 0 
redis 127.0.0.1:6379>setbit wen 4 1
(integer) 0
redis 127.0.0.1:6379>setbit wen 5 1
(integer) 0 
redis 127.0.0.1:6379>setbit wen 8 1
(integer) 0 

redis 127.0.0.1:6379>bitop and res mon feb wen  #最后得出结果

Guess you like

Origin www.cnblogs.com/-wenli/p/10958214.html