美文网首页
解决bitmap做每日统计,offset偏移量过大,导致内存占用

解决bitmap做每日统计,offset偏移量过大,导致内存占用

作者: xuezhongyu01 | 来源:发表于2020-09-18 18:41 被阅读0次

需求:使用redis的bitmap做每日统计
解决思路:使用每天的第一个id作为被减量,这样每天的offset(偏移量)就从1开始,大幅有效的缩减了内存的占用量!

//bitmap的偏移量offset生产,offset越大,占用内存越多,所以以每日第一个id作为minid,作为被减数
//使用guava cache缓存机制获取最小id,设置过期时间为每一天,每天清空一次
private LoadingCache<String, Integer> minId = CacheBuilder.newBuilder().expireAfterWrite(1L, TimeUnit.DAYS).build(new CacheLoader<String, Integer>() {
    @Override
    public Integer load(String s) throws Exception {
      Date date = LocalDate.parse(StringUtils.substringAfter(s, "@")).toDate();
      if (ACTIVE_COUNTER.startsWith(s)) {
        LoginLog loginLog = loginLogRepository.getTopByLoginTimeBeforeOrderByIdDesc(date);
        if (loginLog != null) {
          return loginLog.getId();
        }
      } else if (PLAYED_COUNTER.startsWith(s)) {
        ViewHistory viewHistory = viewHistoryRepository.getTopByViewtimeBeforeOrderByIdDesc(date);
        if (viewHistory != null) {
          return viewHistory.getId();
        }
      } else if (ADCLICK_COUNTER.startsWith(s)) {
        AdvClickHistory advClickHistory = advClickHistoryRepository.getTopByCreateTimeBeforeOrderByIdDesc(date);
        if (advClickHistory != null) {
          return advClickHistory.getId();
        }
      }
      return 0;
    }
  });

使用guava cache缓存,储存每日第一个id

 //数据库主键id 减去当天第一个id 这样每天的偏移量都是从一开始可以有效减少偏移量对内存的占用。
    int offset = registerEventObject.getId() + 1 - minId.getUnchecked(StringUtils.join(type, "@", date));

相关文章

网友评论

      本文标题:解决bitmap做每日统计,offset偏移量过大,导致内存占用

      本文链接:https://www.haomeiwen.com/subject/zxojyktx.html