redis bitmap 入门

1.简介

bigmap是通过类似map结构存放0或1(bit 位)作为值,一般用来统计状态.如:日活,是否浏览过某个东西


2.setbit

redis 127.0.0.1:6379> help setbit


  SETBIT key offset value

  summary: Sets or clears the bit at offset in the string value stored at key

  since: 2.2.0

  group: string


例子:

redis 127.0.0.1:6379> setbit aaa:001 10001 1 #返回操作之前的数值

(integer) 0

redis 127.0.0.1:6379> setbit aaa:001 10002 2 #如果值不是0或1就报错

(error) ERR bit is not an integer or out of range

redis 127.0.0.1:6379> setbit aaa:001 10002 0

(integer) 0

redis 127.0.0.1:6379> setbit aaa:001 10003 1

(integer) 0



3.getbit

redis 127.0.0.1:6379> help getbit


  GETBIT key offset

  summary: Returns the bit value at offset in the string value stored at key

  since: 2.2.0

  group: string


例子:

redis 127.0.0.1:6379> getbit aaa:001 10001 

(integer) 1



4.bitcount

redis 127.0.0.1:6379> help bitcount


  BITCOUNT key [start] [end]

  summary: Count set bits in a string

  since: 2.6.0

  group: string



redis 127.0.0.1:6379> bitcount aaa:001 0 -1

(integer) 2

redis 127.0.0.1:6379> bitcount aaa:001

(integer) 2





你可能感兴趣的:(redis)