I am trying to use redis bitmap to save online user, use command “bitcount onlineUser” to count the number of online user.I use RedisTemplate to deal with redis. But I can’t find any API in RedisTemplate to execute command “bitcount’.How can I execute command “bitcount” in java? Any help will be appreciated.
@Override
public Boolean saveOnlineUser(Long userId) {
ValueOperations<String,Object> ops = redisTemplate.opsForValue();
return ops.setBit("onlineUser",userId,true);
}
@Override
public Integer getOnlineUser() {
//I want to use Redis command "bitcount 'onlineUser'" here to count the number of online users
return redisTemplate.opsForValue().;
}
2
Answers
You can use the "execute" method of RedisTemplate, for example:
if you use spring, you can use
bitCount
operation like below.