zhanhailiang 日期:2014-12-21
Bitmap API
Setbit Key Offset value
Sets or clears a bit (bit) on the specified offset for the string value stored by key.
The setting or clearing of bits depends on the value parameter, which can be 0 or 1.
When key does not exist, you are actively generating a new string value.
The string is stretched (grown) to ensure that it is able to save value at the specified offset.
When the string value is stretched, the blank position is filled with 0.
The offset parameter must be greater than or equal to 0, less than 2^32 (bit mapping is limited to 512MB).
Return value: Specifies the bit that the offset was originally stored in.
Warning: For setbit operations that use large offset, memory allocation can cause Redisserver to be blocked.
Warning: When generating a very long string, Redis needs to allocate memory space, which can sometimes
Causes the server to Jam (block). On the 2010 MacBook Pro, set the offset to 536870911 (within 512MB
300 milliseconds, set the offset to 134217728 (128MB memory allocation), consumes about 80 milliseconds,
Sets the offset 33554432 (32MB memory allocation), consumes about 30 milliseconds, sets the offset to 8388608 (8MB memory allocation),
Consumes about 8 milliseconds.
Getbit Key Offset
Gets the bit on the specified offset for the string value stored by key.
Returns 0 when offset is greater than the length of the string value, or if the key does not exist.
Return value: The string value specifies the bit on the offset (bit).
Demand Scenarios
In very many business scenarios, we need to implement configurations for very versatile implementations, such as the following scenario:
- Add a feature, want to have a switch to control whether to turn on the function;
- Game on-Line a new feature, want to guide users to open the function, the need to induce users, for example, to open the function, to obtain XX reward, at this time need to mark a bit to record the prize record;
Code implementation
https://github.com/billfeller/billfeller.github.io/blob/master/code/IBit.php
Realization of switch configuration function based on Redis bitmap