简介

  • Bitmap 位存储,通过操作二进制位进行记录,0/1两个状态
  • 使用场景:统计用户信息,活跃度,登录打卡

    命令

    | 命令 | 解释 | | —- | —- | | GETBIT key offset | 返回位的值存储在关键的字符串值的偏移量 | | SETBIT key offset value | 设置或清除键处存储的字符串值中偏移量的位 | | BITCOUNT key [start end] | 统计字符串指定起始位置的字节数 | | BITFIELD key [GET type offset] [SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP|SAT|FAIL] | 对字符串执行任意位域整数运算 | | BITOP operation destkey key [key …] | 在字符串之间执行按位运算 | | BITPOS key bit [start] [end] | 查找第一位设置或清除字符串 |

例子: 统计一周打卡天数

设置打卡数据

  1. hello-chen.cn:6379> SETBIT week 0 1
  2. (integer) 0
  3. hello-chen.cn:6379> SETBIT week 1 0
  4. (integer) 0
  5. hello-chen.cn:6379> SETBIT week 2 1
  6. (integer) 0
  7. hello-chen.cn:6379> SETBIT week 3 1
  8. (integer) 0
  9. hello-chen.cn:6379> SETBIT week 4 0
  10. (integer) 0
  11. hello-chen.cn:6379> SETBIT week 5 0
  12. (integer) 0
  13. hello-chen.cn:6379> SETBIT week 6 1
  14. (integer) 0

获取打卡数据

  • GETBIT week 6 // (integer) 1

统计打卡天数

  • BITCOUNT week // (integer) 4