当前位置:网站首页>Redis入门完整教程:Bitmaps
Redis入门完整教程:Bitmaps
2022-07-04 22:29:00 【谷哥学术】
3.5.1 数据结构模型
现代计算机用二进制(位)作为信息的基础单位,1个字节等于8位,例
如“big”字符串是由3个字节组成,但实际在计算机存储时将其用二进制表
示,“big”分别对应的ASCII码分别是98、105、103,对应的二进制分别是
01100010、01101001和01100111,如图3-9所示。
许多开发语言都提供了操作位的功能,合理地使用位能够有效地提高内
存使用率和开发效率。Redis提供了Bitmaps这个“数据结构”可以实现对位的
操作。把数据结构加上引号主要因为:
·Bitmaps本身不是一种数据结构,实际上它就是字符串(如图3-10所
示),但是它可以对字符串的位进行操作。
·Bitmaps单独提供了一套命令,所以在Redis中使用Bitmaps和使用字符
串的方法不太相同。可以把Bitmaps想象成一个以位为单位的数组,数组的
每个单元只能存储0和1,数组的下标在Bitmaps中叫做偏移量。
3.5.2 命令
本节将每个独立用户是否访问过网站存放在Bitmaps中,将访问的用户
记做1,没有访问的用户记做0,用偏移量作为用户的id。
1.设置值
setbit key offset value
设置键的第offset个位的值(从0算起),假设现在有20个用户,
userid=0,5,11,15,19的用户对网站进行了访问,那么当前Bitmaps初始
化结果如图3-11所示。
具体操作过程如下,unique:users:2016-04-05代表2016-04-05这天的
独立访问用户的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
如果此时有一个userid=50的用户访问了网站,那么Bitmaps的结构变成
了图3-12,第20位~49位都是0。
很多应用的用户id以一个指定数字(例如10000)开头,直接将用户id
和Bitmaps的偏移量对应势必会造成一定的浪费,通常的做法是每次做setbit
操作时将用户id减去这个指定数字。在第一次初始化Bitmaps时,假如偏移
量非常大,那么整个初始化过程执行会比较慢,可能会造成Redis的阻塞。
2.获取值
gitbit key offset
获取键的第offset位的值(从0开始算),下面操作获取id=8的用户是否
在2016-04-05这天访问过,返回0说明没有访问过:
127.0.0.1:6379> getbit unique:users:2016-04-05 8
(integer) 0
由于offset=1000000根本就不存在,所以返回结果也是0:
127.0.0.1:6379> getbit unique:users:2016-04-05 1000000
(integer) 0
3.获取Bitmaps指定范围值为1的个数
bitcount [start][end]
下面操作计算2016-04-05这天的独立访问用户数量:
127.0.0.1:6379> bitcount unique:users:2016-04-05
(integer) 5
[start]和[end]代表起始和结束字节数,下面操作计算用户id在第1个字节
到第3个字节之间的独立访问用户数,对应的用户id是11,15,19。
127.0.0.1:6379> bitcount unique:users:2016-04-05 1 3
(integer) 3
4.Bitmaps间的运算
bitop op destkey key[key....]
bitop是一个复合操作,它可以做多个Bitmaps的and(交集)、or(并
集)、not(非)、xor(异或)操作并将结果保存在destkey中。假设2016-
04-04访问网站的userid=1,2,5,9,如图3-13所示。
下面操作计算出2016-04-04和2016-04-03两天都访问过网站的用户数
量,如图3-14所示。
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
如果想算出2016-04-04和2016-04-03任意一天都访问过网站的用户数量
(例如月活跃就是类似这种),可以使用or求并集,具体命令如下:
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.计算Bitmaps中第一个值为targetBit的偏移量
bitpos key targetBit [start] [end]
下面操作计算2016-04-04当前访问网站的最小用户id:
127.0.0.1:6379> bitpos unique:users:2016-04-04 1
(integer) 1
除此之外,bitops有两个选项[start]和[end],分别代表起始字节和结束字
节,例如计算第0个字节到第1个字节之间,第一个值为0的偏移量,从图3-
13可以得知结果是id=0的用户。
127.0.0.1:6379> bitpos unique:users:2016-04-04 0 0 1
(integer) 0
3.5.3 Bitmaps分析
假设网站有1亿用户,每天独立访问的用户有5千万,如果每天用集合类
型和Bitmaps分别存储活跃用户可以得到表3-3。
很明显,这种情况下使用Bitmaps能节省很多的内存空间,尤其是随着
时间推移节省的内存还是非常可观的,见表3-4。
但Bitmaps并不是万金油,假如该网站每天的独立访问用户很少,例如
只有10万(大量的僵尸用户),那么两者的对比如表3-5所示,很显然,这
时候使用Bitmaps就不太合适了,因为基本上大部分位都是0。
边栏推荐
- 9 - class
- Postgresqlql advanced skills pivot table
- High school physics: linear motion
- The Sandbox 和数字好莱坞达成合作,通过人力资源开发加速创作者经济的发展
- leetcode 72. Edit distance edit distance (medium)
- Naacl-22 | introduce the setting of migration learning on the prompt based text generation task
- Detailed explanation of flask context
- LOGO特训营 第二节 文字与图形的搭配关系
- Detailed explanation of heap sort code
- 常用技术指标之一文读懂BOLL布林线指标
猜你喜欢
UML diagram memory skills
Redis入门完整教程:发布订阅
SPSS installation and activation tutorial (including network disk link)
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
Logo special training camp Section V font structure and common design techniques
Introduction and application of bigfilter global transaction anti duplication component
攻防世界 misc 高手进阶区 a_good_idea
Close system call analysis - Performance Optimization
LOGO特训营 第四节 字体设计的重要性
攻防世界 misc 进阶区 2017_Dating_in_Singapore
随机推荐
常用技术指标之一文读懂BOLL布林线指标
PHP short video source code, thumb animation will float when you like it
High school physics: linear motion
Deployment of JVM sandbox repeater
10 schemes to ensure interface data security
Recommendation of mobile app for making barcode
共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
堆排序代码详解
The difference between Max and greatest in SQL
Attack and defense world misc master advanced zone 001 normal_ png
环境加密技术解析
LOGO特训营 第五节 字体结构与设计常用技法
leetcode 72. Edit distance edit distance (medium)
Unity修仙手游 | lua动态滑动功能(3种源码具体实现)
Hit the core in the advanced area of misc in the attack and defense world
Redis的持久化机制
Locust performance test - environment construction and use
LOGO特训营 第一节 鉴别Logo与Logo设计思路
通过Go语言创建CA与签发证书
Redis入门完整教程:客户端通信协议