Skip to content

Instantly share code, notes, and snippets.

@linux-china
Last active July 23, 2017 02:15
Show Gist options
  • Save linux-china/550594f2ff1be84f7a62daa86ceef8d5 to your computer and use it in GitHub Desktop.
Save linux-china/550594f2ff1be84f7a62daa86ceef8d5 to your computer and use it in GitHub Desktop.
bitmap with Redis in Java
@Test
public void testBitGet() {
//redisTemplate.opsForValue().setBit("user2.flags",0,true);
BitSet value = redisTemplate.execute(new RedisCallback<BitSet>() {
public BitSet doInRedis(RedisConnection redis) throws DataAccessException {
return fromByteArrayReverse(redis.get("user.1.flags".getBytes()));
}
});
System.out.println(value);
}
private BitSet fromByteArrayReverse(final byte[] bytes) {
final BitSet bits = new BitSet();
for (int i = 0; i < bytes.length * 8; i++) {
if ((bytes[i / 8] & (1 << (7 - (i % 8)))) != 0) {
bits.set(i);
}
}
return bits;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment