Redis is used in Bitmap

Advantage

1. storage unit based on the smallest bit, so it is very space-saving. 

2. When setting the time complexity of O (1), when reading the time complexity of O (n), the operation is very fast. 

3. When storing binary data, related calculations very quickly. 

4. easy expansion

limit

redis the bit map is confined within the 512MB, the maximum is 2 ^ 32. The bits are recommended for each of the control key, since the reading time to time complexity O (n), the larger string spending more time reading.

 

Operation Command

setbit key offset value: offset to the specified position of the specified key value of (offset) 0 or 1,

GETBIT key offset: Gets the value of the offset is set, not set up a default return 0

BITCOUNT key [start end]: Specifies the key statistics for the number 1 position

Bit operation, BITOP supports four arithmetic expressions:

AND (intersection)

OR (union)

XOR (exclusive or)

NOT (negation)

 

bitmap usage scenarios

Overall on two types of user-examples:

 

A lateral extension is a user, i.e., this key value is recorded in which a current value of the user of various states, allowing unlimited expansion of (2 ^ 32)

 

Comments: This use is substantially rarely used, since each key message carrying uid, if the key space is larger than the stored value, from the spatial point of view there is a certain space optimization, if the recording can be considered a long tail.

 

A longitudinal extension of a user, i.e. each key only records the current status of service attributes, each bit position uid as to record information (user exceeds 2 ^ 32 to be fragmented storage)

E.g:

1: a one hundred million the amount of data has a short video app, there are various video property (whether locked, whether the special effects, etc.), need to do a variety of markers.

2: User online status, only one key, and then the user id is offset offset, it is set to 1 if online, offline is set to 0,3 million users only need 36MB of space

3: Statistical active users, using time as the cache key, then the user id is offset, if the day is too active to be set to 1. After the binary calculation calculated by bitOp active user within a certain period of time.

 

optimization:

1. Space 

redis the bitmap is already the smallest unit of storage, and there is no way to store binary information and then compress it? Further save space?

Can be compressed binary data records. Common binary compression techniques are based on RLE (Run Length Encoding. With a large Bitmap, if the inside of the data distribution is very sparse (there are many large continuous description 0), after using RLE encoding, occupied space than the original Bitmap much smaller.

 

2. Time

redis although in the memory operation, but more than redis stored in the memory after a specified threshold, would be to get the disk. If the calculation also need to remove a wide range from disk into memory in the calculation of time-consuming and inefficient, as there is no way to put some more data in memory, shorten the time?

Bitmap need not use RLE encoded decompressed, can be calculated for various AND / OR / XOR other directly; Bitmap therefore using such compression techniques, is loaded into memory, or the presence in a compressed manner, thereby ensuring calculated time low memory consumption; the use of word (word length computer, the system is a 64-bit 64bit) and alignment techniques to ensure the efficient use of CPU resources. Thus using such compression techniques Bitmap, Bitmap holding a data structure of the most important features is efficient for each bit of the logic operation.

 

Common compression techniques include BBC (patent protection),

 

Published 50 original articles · won praise 2 · Views 2288

Guess you like

Origin blog.csdn.net/eafun_888/article/details/104714648