您的位置:首页 > 房产 > 家装 > 开发小程序定制软件_如何建设网站平台_兰州seo实战优化_互联网营销师证书骗局

开发小程序定制软件_如何建设网站平台_兰州seo实战优化_互联网营销师证书骗局

2025/8/19 15:52:21 来源:https://blog.csdn.net/weixin_51713937/article/details/145482566  浏览:    关键词:开发小程序定制软件_如何建设网站平台_兰州seo实战优化_互联网营销师证书骗局
开发小程序定制软件_如何建设网站平台_兰州seo实战优化_互联网营销师证书骗局

Redis bitmap应用

  • 需求:存储用户今年已签到的天数,如在1月3日签到,则存 3 。。。以此类推

  • 每秒300次请求压力测试

    • image-20250206214539917

1、使用数据库存储

  • image-20250206204346623

查询代码与时间

  • public List<Integer> selectSignRecord(long userId, Integer year) {if (year == null) {year = LocalDate.now().getYear();}int currentYear = LocalDate.now().getYear();if (year != currentYear) {throw new BusinessException(ErrorCode.PARAMS_ERROR, "年份不是当前年份");}//查询签到记录QueryWrapper<UserSign> queryWrapper = new QueryWrapper<>();queryWrapper.eq("userId", userId);queryWrapper.select("signDay");List<Integer> result = userSignMapper.selectObjs(queryWrapper).stream().filter(Objects::nonNull) // 确保过滤掉可能的 null 值.map(item -> Integer.parseInt(item.toString())) // 转换为 Integer.collect(Collectors.toList());return result;}
    }
    
  • 耗时最少时间:879ms

    • image-20250206213921150

2、使用Redis中的bitmap存储

  • image-20250206210505782

查询代码与时间

  •     @Overridepublic List<Integer> selectSignRecord(long userId, Integer year) {if (year == null) {year = LocalDate.now().getYear();}int currentYear = LocalDate.now().getYear();if (year != currentYear) {throw new BusinessException(ErrorCode.PARAMS_ERROR, "年份不是当前年份");}// 拼接签到记录的keyString key = RedisConstant.getUserSignKey(year, userId);// 获取redis中的bitsetRBitSet bitSet = redissonClient.getBitSet(key);// 获取bitset中的所有值BitSet signInBitSet = bitSet.asBitSet();// 构造返回结果,保证返回值有序List<Integer> result = new ArrayList<>();// 获取当前bitset中已签到的天数// 从第一个被设置为1的位置开始遍历,直到没有被设置为1的位置int index = signInBitSet.nextSetBit(0);while (index >= 0) {result.add(index);// 获取下一个被设置为1的位置index = signInBitSet.nextSetBit(index + 1);}return result;}
    
  • 耗时最少时间:292ms,190条数据查询,平均响应时间减少了:61.7%,提升还是蛮大的

    • image-20250206214254268

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com