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

How to use Redis bitmap

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

Share

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

Editor to share with you how to use the Redis bitmap, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Redis also has such a structure called bitmap, the smallest unit of bitmap is bit, each bit is composed of 0 or 1, our string is made up of many bit arrays, so we can convert a set of bit into strings or strings into bit arrays when using bitmaps.

Basic usage of bitmap

As follows, the ACSLL code of "h" is 0110 1000

> setbit name 1 1 # sets the location of the bitmap (integer) 0 > setbit name 2 1 (integer) 0 > setbit name 4 1 (integer) 0 > get name # to get the value "h" > getbit name 1 # for converting this bitmap into a string. Get whether a bitmap has a value (integer) 1 > getbit name 3 (integer) 0.

String transposition map

> set kh h # store string OK > getbit kh 1 # the bit of the second location is 1 (integer) 1 > the bit of the fourth location of getbit kh 3 # is 0 (integer) 0

Note: if the byte of the corresponding bit is a non-printable character, redis displays the hexadecimal form of the character.

> setbit x O 1 (integer) 0 > setbit x 1 1 (integer) 0 > get x "\ xc0" Statistics and search

Redis bitmaps provide bitcount and bitpos instructions, bitcount is used to count the number of 1s in a certain range, and bitpos is used to find the first 0 or 1 in a specified range.

What we need to note here is that the parameters start_index and end_index are byte indexes, which simply means that they can only be multiples of 8 rather than specified values.

Let's look at the picture below.

> set name helloOK127.0.0.1:6379 > bitcount name # count the number of all 1s (integer) 21127.0.0.1 integer > bitcount name 0 1 # get the number of the first two characters appearing 1 (integer) 7127.0.0.1 integer > bitcount name 0 0 # get the number of the first character appearing 1 (integer) 3127.0.0.1 integer 6379 > bitpos name 1 # get the position where the first 1 appears ( Integer) 1127.0.0.1 bitpos name 6379 > bitpos name 0 # get the position where the first 0 appears (integer) 0127.0.0.1 integer > bitpos name 11 1 # get the position where the second character 1 appears (integer) 9 about bitmap batch operation

We use setbit and getbit instructions to manipulate bitmaps one bit at a time, so sometimes we need to manipulate more than one bit at a time.

Before the Redis3.2 version, we can use pipes to operate multiple instructions at a time. Redis provides us with bitfield instructions, which have three sub-instructions, namely get, set and incrby, which can read and write finger segments, but can only handle up to 64 consecutive bits. If more than 64 bits, you have to use multiple sub-instructions, bitfield can execute multiple sub-instructions at a time.

Get

Let's look at the example, or use the name=hello above.

Letter

Numeric binary h

one hundred and four

0110 1000

E101

0110 0101

L108

0110 1100

L108

0110 1100o111

0110 1111

> bitfield name get u40 # the first bit starts with 4 bits (0110), the result is unsigned number (u) (integer) 6 > bitfield name get u32 # the third bit starts with 3 bits (101l), the result is unsigned number (u) (integer) 5 > bitfield name get i40 # first bit starts with 4 bits (0110), the result is signed number (I) (integer) 6 > bitfield name get i32 # the third bit starts with 3 bits (0110) The result is signed number (I) (integer)-3

Let's try batch processing next.

> bitfield name get u4 0 get u3 2 get i4 0 get i3 2 (integer) 6 (integer) 5 (integer) 6 (integer)-3

Signed number means that the first bit in the acquired bit array is the symbol bit, and the rest is the value. If the first place is 1, it is negative. Unsigned numbers represent non-negative numbers, there are no signed bits, and the array of bits obtained are all values. Signed numbers can get up to 64 bits, and unsigned numbers can only get 63 bits (because integer in Redis protocol is a signed number, a maximum of 64 bits, and cannot pass 64-bit unsigned values).

Set

Let's use set to change the e of hello into an ASCL code of a, which is 97.

> bitfield name set u8 97 # starts from the eighth bit, and the next 8 bits are replaced by unsigned ASCL code 97 (integer) 101 > get name "hallo" Incrby

This instruction is used to increase. If it reaches the maximum, it will overflow. If it is unsigned, it will become zero. If it is signed, it will become negative. If it is signed, it will become negative.

The bitfield instruction provides the instruction overflow of the overflow policy. The user can specify the type. The default is: wrap, overflow and return, and you can also choose to fail: fail, fail not to execute, truncate: sat, and stay on the overflow.

> bitfield name incrby u4 2 1 # start with the third bit Keep the maximum value (integer) 15####fail > bitfield name overflow fail incrby u421 # do not execute (nil) # wrap > bitfield name incrby u421 # overflow for the next 4 digits + 1 (integer) 11 > bitfield name incrby u421 (integer) 12 > bitfield name incrby u421 (integer) 13 > bitfield name incrby u421 (integer) 14 > bitfield name incrby u421 (integer) 15####sat > bitfield name overflow sat incrby u4 21 # Return (integer) above 0 is all the content of the article "how to use Redis bitmaps" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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