建网站的程序,企业标准网上备案网站,路线最优1路线2,windows优化大师软件介绍文章目录 1. redis命令行操作bitmap2. RedisTemplate操作bitmap3. Java中的Bitset 1. redis命令行操作bitmap 2. RedisTemplate操作bitmap
bitmap的常见业务场景主要有日活统计#xff08;类似的月考勤#xff09;、点赞、BloomFilter等#xff0c;以用户mj考勤统计为例类似的月考勤、点赞、BloomFilter等以用户mj考勤统计为例一个用户一个月的打卡记录用不了32bit4byte存储空间性能也很好
Resource
private StringRedisTemplate template;template.opsForValue().setBit(mj,1,true);
template.opsForValue().setBit(mj,2,true);
template.opsForValue().setBit(mj,30,true);
template.opsForValue().setBit(mj,31,true);
// 查看mj本月第三天是否打卡
Boolean ifArrive template.opsForValue().getBit(mj, 3);
System.out.println(ifArrive);// false
// 统计本月打卡数
Long count template.execute((RedisCallbackLong) connection - connection.bitCount(mj.getBytes(StandardCharsets.UTF_8), 1, 31));
System.out.println(count); // 43. Java中的Bitset
直接使用Java的bitset实现考勤打卡,这里数据集存储DB需要转化如Bitset#toLongArray(),再转为json进行存储。 public static void main(String[] args) {// Key一般为userIdMapString,BitSet mapnew HashMap(1024);BitSet set map.getOrDefault(mj,new BitSet(32));set.set(1,true);set.set(2,true);set.set(30,true);set.set(31,true);System.out.println(set.get(3)); // 本月第三天打卡falseSystem.out.println(set.cardinality());// 本地打卡数4}