当前位置:网站首页>Redis-bitmap 位图
Redis-bitmap 位图
2022-06-21 12:02:00 【仅此而已_】
Redis-bitmap 位图
在开发过程中,经常有一些bool型的值需要存储。
比如用户一年的签到记录,签了是1,没签是0。如果每个记录都用一个byte、short、int、boolean等基本类型结构去存储,那么占用的空间是很大的。
为了解决这个问题,Redis提供了位图结构,每个布尔记录只需要占用一个位,也就是一个bit。365天的签到记录只需要365位就可以存的下。
注意,位图并不是特殊的数据结构,它的内容就是普通的字符串内容。
8位bit对应一个字符,我们可以在BITCOUNT操作中,明显地看出来。
基本用法
最常用的几个指令:
# 把当前key下的第offset位设置成value
SETBIT key offset value
# 获取指定key下的第offset位
GETBIT key offset
# 获取key中为1的计数,可加范围
# 注意 start和end的单位是字符
BITCOUNT key [start end]
# 查询在[strat, end]区间内,第一次出现bit值的坐标
BITPOS key bit [start end]
指令示例
示例位数组:{ 0, 1, 0, 1, 1}
SETBIT
127.0.0.1:6379> SETBIT example 0 0
(integer) 0
127.0.0.1:6379> SETBIT example 1 1
(integer) 0
127.0.0.1:6379> SETBIT example 2 0
(integer) 0
127.0.0.1:6379> SETBIT example 3 1
(integer) 0
127.0.0.1:6379> SETBIT example 4 1
(integer) 0

GETBIT
示例位数组:{ 0, 1, 0, 1, 1}
127.0.0.1:6379> GETBIT example 2
(integer) 0
127.0.0.1:6379> GETBIT example 3
(integer) 1
127.0.0.1:6379> GETBIT example 4
(integer) 1
127.0.0.1:6379> GETBIT example 1
(integer) 1

BITCOUNT
示例数据 {0,0,0,0,0,0,0,1, 1,1,1,1,1,1,1,1}
注意(1字符-8位)的对应
# 查看全局的BITCOUNT
127.0.0.1:6379> BITCOUNT example
(integer) 9
# 查看第一个字符的BITCOUNT,即前0位
127.0.0.1:6379> BITCOUNT example 0 0
(integer) 1
# 查看第二个字符的BITCOUNT,即后8位
127.0.0.1:6379> BITCOUNT example 1 1
(integer) 8
# 查看前两个字符的BITCOUNT,即0-15位
127.0.0.1:6379> BITCOUNT example 0 1
(integer) 9

BITPOS
示例数据 {0,0,0,0,0,0,0,1, 1,1,1,1,1,1,1,1}
# 获得第一个出现1的下标
127.0.0.1:6379> BITPOS example 1
(integer) 7
# 获得[1,1](字符)内,第一个出现1的下标
127.0.0.1:6379> BITPOS example 1 1 1
(integer) 8
# 获得[1,1](字符)内,第一个出现0的下标,结果不存在
127.0.0.1:6379> BITPOS example 0 1 1
(integer) -1
# 获得[0,1](字符)内,第一个出现0的下标
127.0.0.1:6379> BITPOS example 0 0 1
(integer) 0

边栏推荐
- 2022年138套数学分析高等代数考研真题参考解答勘误
- 一键打新债到底安不安全呀?是不是靠谱的?
- 嵌入struct和嵌入interface
- RPC(远程过程调用协议)
- Corrigendum to 138 sets of reference solutions to the real problems of Higher Algebra in 2022
- SDCC编译器 + VSCode开发 8位微控制器
- External attention tensorflow (under update)
- Codeworks round 797 (Div. 3) F. shifting string problem solution
- MySQL-DDL
- 『忘了再学』Shell流程控制 — 36、for循环介绍
猜你喜欢

Illustrated with pictures and texts -- wechat applet to obtain the user's geographic location information and call Tencent map API to obtain the user's specific location

巨头局终战:即时零售
Golang implements redis (9): use geohash to search people nearby

Apache shardingsphere 5.1.2 release | new driving API + cloud native deployment to create a high-performance data gateway

STM32笔记之 SWJ(JTAG-DP和 SW-DP)

1108. IP 地址无效化

i.MX - RT1052时钟及锁相环(PLL)分析

马斯克的“好朋友”,冲击2022港股最大IPO

华为是如何从0到1打造以项目为中心运作的项目管理体系的?

STM32开发之 VS Code + GDB下载调试
随机推荐
1108. IP address invalidation
Musk's "good friend" impacts the largest IPO of Hong Kong stocks in 2022
Compilation de l'environnement vs Code + GCC développé par stm32
Illustrated with pictures and texts -- wechat applet to obtain the user's geographic location information and call Tencent map API to obtain the user's specific location
Guangdong issues product testing coupons, and consumers also share
方法的继承和重写
MySQL-DQL
Apache ShardingSphere 5.1.2 发布|全新驱动 API + 云原生部署,打造高性能数据网关
Clear the switch configuration, configure the image port and Wireshark packet capturing (take Huawei s5720 as an example)
Knowledge points: several special wiring methods for PCB
STM32開發之 VS Code + gcc環境編譯
一文搞懂 Flink OperatorChain 对象重用
Huawei cloud releases desktop ide codearts
Typescript variable declaration - type assertion
Second harmonyos training
i.MX - RT1052 脉宽调制(PWM)
Centos7 upgrade MySQL 5.6.40 to enterprise 5.6.49
自动弹出 微信授权登录窗口
Adapter power supply automatic test equipment | introduction to charger ATE test system nsat-8000
旅行不能治愈心灵