Last active
July 23, 2017 02:15
-
-
Save linux-china/550594f2ff1be84f7a62daa86ceef8d5 to your computer and use it in GitHub Desktop.
bitmap with Redis in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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