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 either 0 or 1.
When key does not exist, a new string value is automatically generated.
The string is stretched (grown) to ensure that it can 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 may cause the Redis server to be blocked.
Warning: When generating a very long string, Redis needs to allocate memory space, which can sometimes
Cause server blocking (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 many business scenarios, we need to implement configurations for many features, such as the following scenarios:
- 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, need to induce users, such as open the function, you can get 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