Redis位运算操作与redisTemplate结合
By:Roy.LiuLast updated:2021-08-26
redis的位运算,里面记录的是二进制0和1。利用它可以轻松的实现与,或非等运算,在平时开发过程中,在得到连续几天签到,活跃率统计,包括在线数统计上都有一定的实践意义。今天主要记录利用redisTemplate怎么简单的操作redis 位运算.
public Boolean setBit(String key, Long id, boolean flag) {
ValueOperations<String,Object> ops = redisTemplate.opsForValue();
return ops.setBit(key, id,flag);
}
/**
* 获取BIT 的数量.
* @param key
* @param start
* @param end
* @return
*/
public Long bitcount(final String key, final long start, final long end) {
return (Long) redisTemplate.execute(new RedisCallback<Long>() {
@Override
public Long doInRedis(RedisConnection redisConnection) throws DataAccessException {
if (start == 0 && end == 0) {
return redisConnection.bitCount(key.getBytes());
}
return redisConnection.bitCount(key.getBytes(), start, end);
}
});
}
如果还需要其他操作,可以参考上面图形所示的得到bit总数量的方式写方法,可以做其他操作。
From:一号门
Previous:利用Python脚本调用ssh scp命令批量拷贝文件

COMMENTS