Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the relevant commands of bitmap

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what are the relevant commands of bitmap". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Working with scen

Statistics of the number of users online, statistics of active users, check-in, likes and other identification functions

two。 Principle

Is to use a bit bit to represent the corresponding value or state of an element, where key is the corresponding element itself, bit is not byte,1byte=8bit, the advantage is highlighted, that is, the thief chicken saves space

Space occupancy and the time required to allocate space for the first time

On a 2010MacBook Pro, offset for 2 ^ 32-1 (assign 512MB) requires ~ 300ms offset for 2 ^ 30-1 (assign 128MB) requires ~ 80ms offset for 2 ^ 28-1 (assign 32MB) requires 8ms for ~ 30ms offset of 2 ^ 26-1 (assign 8MB).

The approximate formula for calculating space occupancy is: ($offset/8/1024/1024) MB)

Bitmap is not a new data type, it's just an extension of set.

3. Command

Command

Action

Setbit

Set the value of Bitmap

Getbit

Get the value of Bitmap

Bitcount

Gets the number of values of 1 in the specified range

Destkey

Operate on Bitmap, which can be and (intersection), or (union), not (non-set), or xor (XOR)

BITOP

BITOP operation destkey key [key...]

Operation can be any of the four operations: AND, OR, NOT, XOR:

● BITOP AND destkey key [key...], logically merge one or more key and save the result to destkey.

● BITOP OR destkey key [key...], logically OR one or more key, and save the result to destkey.

● BITOP XOR destkey key [key...], logically XOR one or more key, and save the result to destkey.

● BITOP NOT destkey key, logically negate a given key, and save the result to destkey.

Except for NOT operations, all operations can accept one or more key as input

BITPOS

BITPOS key bit [start] [end]

Returns the first bit bit in the string that is set to 1 or 0.

BITFIELD

BITFIELD key [GET type offset] [SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP | SAT | FAIL]

Overflow control

You can specify the behavior of the BITFIELD command when it encounters an upward overflow (overflow) or a downward overflow (underflow) when performing self-increment or self-subtraction operations through the OVERFLOW command and the three parameters shown below:

WRAP: use the wrap around method to handle the overflow of signed and unsigned integers. For unsigned integers, wrapping is like performing modular calculations using the value itself and the largest unsigned integer that can be stored, which is also the standard behavior of the C language. For signed integers, the overflow will cause the number to start again from the lowest negative number, while the underflow will cause the number to start again from the largest positive number. For example, if we add an i8 integer with a value of 127, we will get the result-128.

SAT: use the saturation calculation (saturation arithmetic) method to handle the overflow, that is, the result of the underflow calculation is the lowest integer value, while the result of the overflow calculation is the largest integer value. For example, if we add 10 to an i8 integer with a value of 120, the result of the command will be the maximum integer value of 127 that can be stored by the i8 type. In contrast, if a calculation for the i8 value causes an underflow, the i8 value will be set to-127.

FAIL: in this mode, the command rejects calculations that cause an overflow or underflow, and returns a null value to the user indicating that the calculation has not been performed.

It is important to note that the OVERFLOW subcommand will only have an effect on the INCRBY command that is executed immediately after it, and this effect will continue until the next OVERFLOW command that is executed with it. By default, the INCRBY command uses WRAP to handle overflow calculations.

4. Advantages

Provincial space

5. Shortcoming

Not yet, although one identification bit can only record one user, that's enough, after all, you don't have 2 ^ 32-1 data.

6. Realize the scene like.

New redis method setbit

/ / bitmap/** * Sets the bit at {@ code offset} in value stored at {@ code key}. * * @ param key must not be {@ literal null}. * @ param offset * @ param value * @ since 1.5 * @ see Redis Documentation: SETBIT * / Boolean setBit (K key,long offset, boolean value); / * * setbit* * / public Boolean setBitMap (String key,long commentDatailId,boolean value) {return redisTemplate.opsForValue () .setBit (key,commentDatailId,value);}

In the command, value identifies 1 or 0, that is, the corresponding value of true/false

Key: set key, such as the number of daily active users. If you check in, you can use time + id. If you don't like by the logo, you can use the message id.

Offset: that is, the identity bit occupied, and you can remember the offset at first glance.

Value: set 0Universe 1

Boolean: returns 0 or 1 to identify whether or not

Boolean flag = redisService.setBitMap (userIdStr,commentDetailId,true)

The previous logic remains unchanged before set determines whether it exists or not.

If (redisService.getBitMap (userIdStr,commentDetailId)) {redisService.setBitMap (userIdStr,commentDetailId,true);} else {redisService.setBitMap (userIdStr,commentDetailId,false);}

Question: what if my id is big or my uuid starts?

Normal id is not possible, but uuid is possible. If it is a uuid, you can use uuid minus the initial bit to place it.

Implement a blacklist with a large amount of data in the scenario

This service is suitable for the storage of a large amount of data, and you don't have to worry about it based on redis speed.

Daily active users statistics

Bitop and time moday thurday

1. Save space, count the login situation of 100 million people every day, with 100 million bits, about 1200WByte, about 10m characters can be expressed (because the return value of the bitop command is the length of the string saved to time (in bytes byte), which is equal to the length of the longest string in the input key. That is, 100 million divided by 8 bits = 12.5 million Byte)

two。 Convenient calculation

Performance:

   if your bitmap data is very large, consider using the following two methods:

● distributes a large bitmap into different key and treats it as a small bitmap. You can easily do this using the Lua script.

● uses the start and end parameters of BITCOUNT, calculates only some of the bits needed at a time, puts the bit accumulation (accumulating) to the client, and caches the results (caching).

When to use:

   if you have millions of active users, it's cost-effective to use Redis BitMap.

   if there are very few active users, and the user id is more than 10 int. That would be a waste of memory, so you might as well use the set collection and then find the intersection.

This is the end of the content of "what are the relevant commands of bitmap". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report