前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >redis | 十一、redis之Bitmaps

redis | 十一、redis之Bitmaps

作者头像
雨中散步撒哈拉
发布2022-09-21 11:41:36
3390
发布2022-09-21 11:41:36
举报

redis系列文章: https://liudongdong.top/categories/redis 本篇来源: https://liudongdong.top/archives/redisshi-yi-redis-zhi-bitmaps 公众号:雨中散步撒哈拉 备注:欢迎关注公众号,一起学习,共同进步!

一、Bitmaps(位图)

Bitmaps 并不是实际的数据类型,而是定义在String类型上的一个面向字节操作的集合。因为字符串是二进制安全的块,他们的最大长度是512M,最适合设置成2^32个不同字节。

Bitmaps 的最大优势之一在存储信息时极其节约空间。例如,在一个以增量用户ID来标识不同用户的系统中,记录用户的四十亿的一个单独bit信息(例如,要知道用户是否想要接收最新的来信)仅仅使用512M内存。

1. getbit key offset

获取位图指定索引的值

代码语言:javascript
复制
127.0.0.1:6379> set bitmap big
OK
127.0.0.1:6379> getbit bitmap 0
(integer) 0
127.0.0.1:6379> getbit bitmap 1
(integer) 1
127.0.0.1:6379> getbit bitmap 2
(integer) 1
127.0.0.1:6379>

image.png

2. setbit key offset value

给位图指定索引设置值,返回该索引位置的原始值

代码语言:javascript
复制
127.0.0.1:6379> setbit bitmap 7 1
(integer) 0
127.0.0.1:6379> get bitmap
"cig"
127.0.0.1:6379>

3. bitcount key [start end]

获取位图指定范围(start到end,单位为字节,如果不指定就是获取全部)位值为1的个数。

代码语言:javascript
复制
127.0.0.1:6379> bitcount bitmap
(integer) 13
127.0.0.1:6379> setbit bitmap 8 1
(integer) 0
127.0.0.1:6379> bitcount bitmap
(integer) 14
127.0.0.1:6379>

4. bitop and|or|not|xor destkey key [key…]

做多个bitmap的and(交集)、or(并集)、not(非)、xor(异或)操作并将结果保存到destkey中。

代码语言:javascript
复制
127.0.0.1:6379> set hello good
OK
127.0.0.1:6379> set world good
OK
127.0.0.1:6379> bitop and hello_world hello world
(integer) 4
127.0.0.1:6379> get hello_world
"good"
127.0.0.1:6379> bitop or hello_world hello world
(integer) 4
127.0.0.1:6379> get hello_world
"good"
127.0.0.1:6379> bitop not hello_world hello
(integer) 4
127.0.0.1:6379> get hello_world
"\x98\x90\x90\x9b"
127.0.0.1:6379> bitop xor hello_world hello world
(integer) 4
127.0.0.1:6379> get hello_world
"\x00\x00\x00\x00"
127.0.0.1:6379> 

5. bitpos key targetBit [start] [end] (起始版本:2.8.7)

计算位图指定范围(start到end,单位为字节,如果不指定就是获取全部)第一个偏移量对应的值等于targetBit的位置。

代码语言:javascript
复制
127.0.0.1:6379> set hello big
OK
127.0.0.1:6379> bitpos hello 1
(integer) 1
127.0.0.1:6379> bitpos hello 0
(integer) 0
127.0.0.1:6379> bitpos hello 1 2 2
(integer) 17
127.0.0.1:6379> bitpos hello 1 2 3
(integer) 17
127.0.0.1:6379> bitpos hello 0 2 3
(integer) 16
127.0.0.1:6379> bitpos hello 0 0 3
(integer) 0
127.0.0.1:6379> bitpos hello 1 0 3
(integer) 1

实战应用

独立用户访问统计

  1. 使用 set 和 Bitmap (前提是用户的ID必须是整型)
  2. 1亿用户,五千万独立

数据类型

每个userId占用空间

需要存储的用户量

内存使用总量

set

32位(假设userId用的是integer)

50,000,000

32位*50,000,000=200MB

Bitmap

1位

100,000,000

1位*100,000,000=12.5MB

  1. 若只有10万独立用户

数据类型

每个userId占用空间

需要存储的用户量

内存使用总量

set

32位(假设userId用的是整型)

100,000

32位*100,000=4MB

Bitmap

1位

100,000,000

1位*100,000,000=12.5MB

本文参与?腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-10-22,如有侵权请联系?cloudcommunity@tencent.com 删除

本文分享自 雨中散步撒哈拉 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体同步曝光计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、Bitmaps(位图)
    • 1. getbit key offset
      • 2. setbit key offset value
        • 3. bitcount key [start end]
          • 4. bitop and|or|not|xor destkey key [key…]
            • 5. bitpos key targetBit [start] [end] (起始版本:2.8.7)
              • 实战应用
              相关产品与服务
              云数据库 Redis
              腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
              http://www.vxiaotou.com