当前位置:网站首页>Complete tutorial for getting started with redis: bitmaps
Complete tutorial for getting started with redis: bitmaps
2022-07-04 22:53:00 【Gu Ge academic】
3.5.1 Data structure model
Modern computers use binary ( position ) As the basic unit of information ,1 Bytes are equal to 8 position , example
Such as “big” Strings are created by 3 Byte composition , But it is actually stored in a binary table
in ,“big” Respectively corresponding ASCII The codes are 98、105、103, The corresponding binaries are
01100010、01101001 and 01100111, Pictured 3-9 Shown .

Many development languages provide the function of operation bits , Reasonable use of bits can effectively improve internal
Storage utilization and development efficiency .Redis Provides Bitmaps This “ data structure ” Can realize alignment
operation . The data structure is quoted mainly because :
·Bitmaps It's not a data structure in itself , It's actually a string ( Pictured 3-10 the
in ), But it can operate on the bits of a string .
·Bitmaps A separate set of commands is provided , So in Redis Use in Bitmaps And use characters
The method of string is different . You can put Bitmaps Think of it as an array of bits , Array of
Each unit can only store 0 and 1, The subscript of the array is in Bitmaps It's called offset .

3.5.2 command
This section stores whether each individual user has visited the website in Bitmaps in , Users to be accessed
Remember to do 1, Do not visit users remember to do 0, Use offset as user's id.
1. Set the value
setbit key offset value
Set the number of the key offset The value of a bit ( from 0 Count up ), Suppose there are now 20 Users ,
userid=0,5,11,15,19 's users visited the site , Then the current Bitmaps initial
The result is shown in the figure 3-11 Shown .

The specific operation process is as follows ,unique:users:2016-04-05 representative 2016-04-05 On this day
Independent access to the user's Bitmaps:
127.0.0.1:6379> setbit unique:users:2016-04-05 0 1
(integer) 0
127.0.0.1:6379> setbit unique:users:2016-04-05 5 1
(integer) 0
127.0.0.1:6379> setbit unique:users:2016-04-05 11 1
(integer) 0
127.0.0.1:6379> setbit unique:users:2016-04-05 15 1
(integer) 0
127.0.0.1:6379> setbit unique:users:2016-04-05 19 1
If there is a userid=50 's users visited the website , that Bitmaps The structure of becomes
Figure 3-12, The first 20 position ~49 All places are 0.

Users of many applications id With a specified number ( for example 10000) start , Direct users id
and Bitmaps A certain amount of offset is bound to cause waste , The usual practice is to do it every time setbit
The user will be id Subtract the specified number . At first initialization Bitmaps when , If offset
It's very large , Then the whole initialization process will be slow , It may cause Redis The block .
2. Get value
gitbit key offset
Get the second offset The value of a ( from 0 Start counting ), The following operation gets id=8 Is the user of
stay 2016-04-05 I visited , return 0 I haven't visited :
127.0.0.1:6379> getbit unique:users:2016-04-05 8
(integer) 0
because offset=1000000 It doesn't exist at all , So the return result is 0:
127.0.0.1:6379> getbit unique:users:2016-04-05 1000000
(integer) 0
3. obtain Bitmaps The specified range value is 1 The number of
bitcount [start][end]
The following operation calculates 2016-04-05 The number of independent users that day :
127.0.0.1:6379> bitcount unique:users:2016-04-05
(integer) 5
[start] and [end] Represents the number of start and end bytes , The following operation calculates the user id In the 1 Bytes
To the first 3 Number of independent access users between bytes , The corresponding user id yes 11,15,19.
127.0.0.1:6379> bitcount unique:users:2016-04-05 1 3
(integer) 3
4.Bitmaps Operation between
bitop op destkey key[key....]
bitop It's a composite operation , It can do more than one Bitmaps Of and( intersection )、or( and
Set )、not( Not )、xor( Exclusive or ) Operate and save the result in destkey in . hypothesis 2016-
04-04 Visit the website userid=1,2,5,9, Pictured 3-13 Shown .

The following operation calculates 2016-04-04 and 2016-04-03 The number of users who have visited the website in two days
The amount , Pictured 3-14 Shown .
127.0.0.1:6379> bitop and unique:users:and:2016-04-04_03 unique: users:2016-04-03
unique:users:2016-04-03
(integer) 2
127.0.0.1:6379> bitcount unique:users:and:2016-04-04_03
(integer) 2
If you want to figure it out 2016-04-04 and 2016-04-03 The number of users who have visited the site on any given day
( For example, the active moon is like this ), have access to or Union , The specific command is as follows :
127.0.0.1:6379> bitop or unique:users:or:2016-04-04_03 unique:
users:2016-04-03 unique:users:2016-04-03
(integer) 2
127.0.0.1:6379> bitcount unique:users:or:2016-04-04_03
(integer) 6

5. Calculation Bitmaps The first value in is targetBit The offset
bitpos key targetBit [start] [end]
The following operation calculates 2016-04-04 The smallest user currently visiting the website id:
127.0.0.1:6379> bitpos unique:users:2016-04-04 1
(integer) 1
besides ,bitops There are two options [start] and [end], Represents the start byte and end word respectively
section , For example, calculate the number of 0 Byte to byte 1 Between bytes , The first value is 0 The offset , From the picture 3-
13 We can know that the result is id=0 Users of .
127.0.0.1:6379> bitpos unique:users:2016-04-04 0 0 1
(integer) 0
3.5.3 Bitmaps analysis
Suppose the website has 1 Billion users , The users who visit independently every day are 5 Ten million , If you use collection classes every day
The type and Bitmaps Store active users separately and get tables 3-3.
Obviously , Use... In this case Bitmaps Can save a lot of memory space , Especially with
The memory saved over time is still very considerable , See table 3-4.

but Bitmaps It's not a panacea , If the website has few independent visitors every day , for example
Only 10 ten thousand ( A lot of zombie users ), Then the comparison between the two is shown in the table 3-5 Shown , Obviously , this
When using Bitmaps Not really , Because most of the bits are 0.

边栏推荐
- The difference between Max and greatest in SQL
- Redis入门完整教程:发布订阅
- [roommate learned to use Bi report data processing in the time of King glory in one game]
- The sandbox has reached a cooperation with digital Hollywood to accelerate the economic development of creators through human resource development
- 【室友用一局王者荣耀的时间学会了用BI报表数据处理】
- Insert sort, select sort, bubble sort
- 剑指 Offer 65. 不用加减乘除做加法
- Now MySQL cdc2.1 is parsing the datetime class with a value of 0000-00-00 00:00:00
- SQL中MAX与GREATEST的区别
- [OpenGL] note 29 anti aliasing (MSAA)
猜你喜欢

攻防世界 MISC 进阶区 hit-the-core

质量体系建设之路的分分合合

SPSS installation and activation tutorial (including network disk link)

攻防世界 MISC 进阶区 Ditf

Mongodb aggregation operation summary

Persistence mechanism of redis

Redis入門完整教程:Pipeline
![[roommate learned to use Bi report data processing in the time of King glory in one game]](/img/06/22dde3fcc0456bd230e1d0cde339ec.png)
[roommate learned to use Bi report data processing in the time of King glory in one game]

业务太忙,真的是没时间搞自动化理由吗?

Logo special training camp section II collocation relationship between words and graphics
随机推荐
La prospérité est épuisée, les choses sont bonnes et mauvaises: Où puis - je aller pour un chef de station personnel?
新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
Google Earth Engine(GEE)——以MODIS/006/MCD19A2为例批量下载逐天AOD数据逐天的均值、最大值、最小值、标准差、方差统计分析和CSV下载(北京市各区为例)
Attack and Defense World MISC Advanced Area Erik baleog and Olaf
Li Kou 98: verify binary search tree
集群的概述与定义,一看就会
The difference between Max and greatest in SQL
leetcode 72. Edit distance edit distance (medium)
Now MySQL cdc2.1 is parsing the datetime class with a value of 0000-00-00 00:00:00
Redis入门完整教程:有序集合详解
攻防世界 MISC 进阶区 hong
The new version judges the code of PC and mobile terminal, the mobile terminal jumps to the mobile terminal, and the PC jumps to the latest valid code of PC terminal
UML diagram memory skills
Logo special training camp Section V font structure and common design techniques
How to manage 15million employees easily?
串口数据帧
攻防世界 MISC 进阶区 hit-the-core
SPSS installation and activation tutorial (including network disk link)
Advanced area of attack and defense world misc 3-11
NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse