当前位置:网站首页>[Redis] Bitmap and usage scenarios of bitmap (statistics of online people and user online status)
[Redis] Bitmap and usage scenarios of bitmap (statistics of online people and user online status)
2022-08-08 13:41:00 【InfoQ】
1、位图
512MB
基本使用
get
set
SETBIT key 索引 值0/1
SETBIT

0110 0001
get
a

GETBIT key 索引
127.0.0.1:6379> GETBIT f 1
(integer) 1
127.0.0.1:6379> GETBIT f 0
(integer) 0
通过SET 一次设置单个位图的所有位
SET
127.0.0.1:6379> set g a
OK
127.0.0.1:6379> get g
"a"
127.0.0.1:6379> GETBIT g 1
(integer) 1
127.0.0.1:6379> GETBIT g 0
(integer) 0
BITFIELD 设置多个位
BITFIELD key [GET type offset] [SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP|SAT|FAIL]
127.0.0.1:6379> BITFIELD mykey2 set u1 1 1 set u1 2 1 set u1 7 1
1) (integer) 0
2) (integer) 0
3) (integer) 0
127.0.0.1:6379> get mykey2
"a"
BITCOUNT
#start和end参数指的是字节的索引 不是位的索引
127.0.0.1:6379> set mykey abcde
OK
127.0.0.1:6379> get mykey
"abcde"
127.0.0.1:6379> BITCOUNT mykey 0 0 //是计算第一个字符 a的位数
(integer) 3
127.0.0.1:6379> BITCOUNT mykey 0 1 //是计算前两个个字符 ab的位数
(integer) 6
127.0.0.1:6379> BITCOUNT mykey 0 2
(integer) 10
127.0.0.1:6379> BITCOUNT mykey 1 1 //是计算第2个字符 b的位数
(integer) 3
BITPOS 查找指定值为0或1的第一位.
#start和end参数指的是字节的索引 不是位的索引
a=0110 0001 b=0110 0010
127.0.0.1:6379> BITPOS mykey 1 0 0
(integer) 1
a=0110 0001 b=0110 0010
127.0.0.1:6379> BITPOS mykey 1 1 1
(integer) 9
counter(counterh2)位图的使用场景
记录用户一年的签到情况
- 记录每个用户的一年中每天的签到情况
- 统计某个时间段 用户的签到天数
- 可以查询某个时间段的签到情况
2020-1-1
getbit
管道
lua脚本
BITCOUNT uidkey 0 0
#start和end参数指的是字节的索引 不是位的索引
start和end参数指的是字节的索引 不是位的索引

BITCOUNT key 1 2
getbit
getbit
实时统计在线人数和某个用户的在线状态
incr
getbit key 索引
BITCOUNT
[[email protected]]# /usr/local/bin/redis-cli -r -1 -i 1 INFO |grep rss_human
used_memory_rss_human:7.72M
127.0.0.1:6379> SETBIT bigbit 1 1
(integer) 0

7.72->7.73
127.0.0.1:6379> SETBIT bigbit 100000000 1
(integer) 0

7.73->20.92
BITCOUNT统计大数据量的性能问题
BITCOUNT
BITCOUNT
get
GET
INCR
- 取一个单独的密钥,该密钥在每次修改位图时都会递增.使用小的Redis Lua脚本可以非常高效和原子.
- 使用BITCOUNT 开始和结束 可选参数递增地运行位图,在客户端积累结果,并可选地将结果缓存到密钥中.

边栏推荐
- 复盘:什么是秋招提前批?什么是普通秋招?都是招聘,为啥要设置这两个招聘时间段
- R语言数据类型转换:基本数据类型的转换、将一种数据类型转化为另外一种数据类型
- Knowledge points and written test questions related to shift operations, bit operations, and logical operations
- 剑指 Offer 66. 构建乘积数组
- Experience Sharing | Systematic Design and Development of Business Cache
- 暗恋云匹配匿名交友聊天系统开发
- Doris学习笔记之优化
- Implementation of FIR filter based on FPGA (1) - using fir1 function design
- KMP Media Group South Africa implemented a DMS (Document Management System) to digitize the process, employees can again focus on their actual tasks, providing efficiency
- 【Personal Summary】2022.8.7 Week End
猜你喜欢
Pretraining Weekly Issue 56: Long Text Understanding, Instant Question Answering, Mask Self-Supervision
医药行业转型发展,探索数字化供应链升级之道
In-depth analysis of the soul of C language -- pointer
专访|360高瀚昭:ISC十年,360数字安全大脑能够“看见”什么?
数据解析(XPath、BeautifulSoup、正则表达式、pyquery)
看到这个应用上下线方式,不禁感叹:优雅,太优雅了!
SSTI漏洞介绍认识(flask、Werkzeup)
深析C语言的灵魂 -- 指针
a += 1 += 1为什么是错的?
家电行业趋势:2022从三方面把握家电产品升级方向
随机推荐
SAP数据迁移需要多久?
MySQL的索引和事务
The use of string function, character function, memory function and its analog implementation
TS+Hooks二次封装antd Modal,实现可拖拽
serialize 序列化原生方法
[界面开发]DevExpress WinForms流程图控件——XtraDiagrams组件入门指南
sample函数—R语言
bzoj 3624 [Apio2008]免费道路
poj3744 Scout YYF I
(7) FlinkSQL kafka data written to the mysql way 2
【低代码】1405- 浅谈低代码平台远程组件加载方案
PE文件-手工修改重定位表-WinHex-CFF Explorer
简析LDO静态电流与功耗的关系
Qt操作Sqlite类封装,及命令行导入csv文件到Sqlite数据库
指针和数组笔试题解析
a += 1 += 1为什么是错的?
【Redis】redis安装与客户端redis-cli的使用(批量操作)
金融行业数智化供应链管理系统:多维度评估分析供应商,赋能智能金融变革
删库不易,跑路更难
连锁小酒馆第一股,海伦司能否梦圆大排档?