Redis-Bitmaps application

One . Data structure model

Redis Provided Bitmaps this “ data structure ” Can realize the operation of alignment .Bitmaps Not a data structure in itself , It's actually a string , But it can operate on the bits of a string .

You can Bitmaps Think of it as an array of bits , Each cell in an array can only be stored 0 perhaps 1, The subscript of the array is bitmaps Is called offset . single bitmaps The maximum length of is 512MB, Namely 2^32 Bits .

Two . command

 

 

2.1 Set value

command :setbit key offset value

for example :setbit unique:users:2017-07-11 0 1
representative 2017-07-11 This day's independent access user's Bitmaps, Use subscripts to represent users ID,1 Indicates access ,0 Indicates no access , The above command represents the first user 2017-07-11 This day I visited the system .

 

2.2 Get value

command :getbit key offset

for example : setbit unique:users:2017-07-11 0 1

getbit unique:users:2017-07-11 0

return : 1

If offset=0 Element of does not exist , Then return the result 0

 

2.3 obtain Bitmaps The specified range value is 1 Number of

command :bitcount  key  [start] [end]

for example :setbit unique:users:2017-07-11 0 1

setbit unique:users:2017-07-11 9 1

bitcount unique:users:2017-07-11

return : 2

bitcount unique:users:2017-07-11 0 0

return :1

 

2.4 Bitmaps Operation between

2.4.1 bitop and destkey key[key…]   calculation Bitmaps Number of intersections of

command :bitop and destkey key[key…]

return : Save to  destkey  String of (1 Character equals 8 position ) Length of , And input  key  The longest string in is the same length .

 

for example :

redis> SETBIT bits-1 0 1        # bits-1 = 1001

(integer) 0

 

redis> SETBIT bits-1 3 1

(integer) 0

 

redis> SETBIT bits-2 0 1        # bits-2 = 1101

(integer) 0

 

redis> SETBIT bits-2 1 1

(integer) 0

 

redis> SETBIT bits-2 3 1

(integer) 0

 

redis> BITOP AND and-result bits-1bits-2

(integer) 1

 

redis> GETBIT and-result 0      # and-result = 1001

(integer) 1

 

redis> GETBIT and-result 1

(integer) 0

 

redis> GETBIT and-result 2

(integer) 0

 

redis> GETBIT and-result 3

(integer) 1

 

2.4.2 bitop or destkey key[key…]   calculation Bitmaps Number of union sets of

command :bitop or destkey key[key…]

return : Save to  destkey  String of (1 Character equals 8 position ) Length of , And input  key  The longest string in is the same length .

 

 

for example :

redis> SETBIT bits-1 0 1        # bits-1 = 1001

(integer) 0

 

redis> SETBIT bits-1 3 1

(integer) 0

 

redis> SETBIT bits-2 0 1        # bits-2 = 1101

(integer) 0

 

redis> SETBIT bits-2 1 1

(integer) 0

 

redis> SETBIT bits-2 3 1

(integer) 0

 

redis> BITOP or or-result bits-1 bits-2

(integer) 1

 

redis> GETBIT or-result 0      # or-result = 1101

(integer) 1

 

redis> GETBIT or -result 1

(integer) 1

 

redis> GETBIT or -result 2

(integer) 0

 

redis> GETBIT or -result 3

(integer) 1

2.4.3 bitop not destkey key  calculation Bitmaps Number of non sets for

command :bitop not destkey key

return : Save to  destkey  String of (1 Character equals 8 position ) Length of , And input  key  The longest string in is the same length .

 

 

for example :

redis> SETBIT bits-1 0 1        # bits-1 = 1001

(integer) 0

 

redis> SETBIT bits-1 3 1

(integer) 0

 

 

redis> SETBIT bits-2 3 1

(integer) 0

 

redis> BITOP not not-result bits-1

(integer) 1

 

redis> GETBIT not -result 0      # and-result = 0110

(integer) 0

 

redis> GETBIT not -result 1

(integer) 1

 

redis> GETBIT not -result 2

(integer) 1

 

redis> GETBIT not -result 3

(integer) 0

 

2.4.4 bitop xor destkey key  calculation Bitmaps The number of exclusive or sets of

command :bitop xor destkey key[key..]

return : Save to  destkey  String of (1 Character equals 8 position ) Length of , And input  key  The longest string in is the same length .

 

for example :

redis> SETBIT bits-1 0 1        # bits-1 = 1001

(integer) 0

 

redis> SETBIT bits-1 3 1

(integer) 0

 

 

redis> SETBIT bits-2 3 1

(integer) 0

 

redis> BITOP xor xor-result bits-1bits-2

(integer) 1

 

redis> GETBIT not -result 0      # xor-result = 0100

(integer) 0

 

redis> GETBIT not -result 1

(integer) 1

 

redis> GETBIT not -result 2

(integer) 0

 

redis> GETBIT not -result 3

(integer) 0

2.4.5 calculation Bitmaps The first value in is targetBit Offset of

command :bitpos key targetBit [start][end]

return : Offset with first value

 

for example :

10.3.34.101:6378> setbit testbit 0 1   testbit 111010

(integer) 0

10.3.34.101:6378> setbit testbit 1 1

(integer) 0

10.3.34.101:6378> setbit testbit 2 1

(integer) 0

10.3.34.101:6378> setbit testbit 3 0

(integer) 0

10.3.34.101:6378> setbit testbit 4 1

(integer) 0

10.3.34.101:6378> setbit testbit 5 0

(integer) 0

10.3.34.101:6378> bitpos testbit 0     The first value is 0 The offset of is 3

(integer) 3

Three .Bitmaps Application scenario

3.1 Count active users

For each user id corresponding bitmaps A subscript on , By setting the corresponding bit of the active user , You can use one value Record all active user information .

If we have the following scenario :

Statistics required N Users visiting the website within days , continuity N Days of users visiting the site .

Solution :

  
We store the users who visit the website every day in one bitmaps in , Generate key by date , for example :unique:users:2017-07-11, In this way, we can count the daily user access , Pass at the same time bitmaps The sum or operation function of can be used to count the active users and continuous users in a time period N Day visit website users and other related needs .

Four . Bitmaps analysis

When the number of active users recorded is high ,bitmaps Save a lot of memory compared to collection type , But if the number of active users per day is small , be bitmaps Don't try . Below 1 Billion users , Each day 5 Ten million and 10 Analysis of WANLAI :

Technology
©2019-2020 Toolsou All rights reserved,
Three methods of value transfer between non parent and child components Element-UI Implementation of secondary packaging TreeSelect Tree drop-down selection component Ai Wei also talks about project management , On grass roots management about Bellman-Ford Personal understanding of algorithms ( Essence )2020 year 7 month 13 day Wechat applet Inter communication page Determine whether the current object is an array 4 Methods be based on STM32 Design of infrared obstacle avoidance car ( There is a code )SSM Project's excel File upload and add to database python Short circuit logic in c Language rose advertising code