java实现M天展示N次

通过redis实现:

1 通过redis记录M天一共展示的次数

StoreKey storeKey = new StoreKey(Constants.RedisCategory.GLOBAL_FLOATING_RECORD_CATEGORY, userId, resourceId);
//获取当前key的存活剩余时间
Long timesTtl = redisGateway.pttl(storeKey, Boolean.TRUE);
if(timesTtl == null || timesTtl < 0){
     //获取M天转成秒的时间戳
     long totalExpireInSeconds = TimeUtil.getAfterMDayMaxTimestamp(limitDay) / 1000;
     redisGateway.incrBy(storeKey, 1L, (int) (totalExpireInSeconds - System.currentTimeMillis() / 1000), true);
}else{
    redisGateway.incrBy(storeKey, 1L, (int) (timesTtl / 1000), true);
}
/**
     *
     * 获取m天后的时间戳 1天 就是当天的23.59.59
     *
     * @param day
     * @return
     */
public static Long getAfterMDayMaxTimestamp(Integer day){
     if (day == null || day <= 1){
         return getCurrentDayMaxTimestamp();
     }
     return getCurrentDayMaxTimestamp() +  ONE_DAY * (day - 1);
}
 /**
     *
     * 获取当天凌晨 23.59 的时间戳
     *
     * @return
     */
public static Long getCurrentDayMaxTimestamp() {
      LocalDateTime todayStart = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
      return todayStart.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}

2 通过redis获取已展示的次数

StoreKey storeKey = new StoreKey(Constants.RedisCategory.GLOBAL_FLOATING_RECORD_CATEGORY, userId, resourceId);
Long showCount = RedisCategory.get(storeKey, true);
if (Objects.isNull(showCount)) {
      return true;
}
Integer resourceLimit = globalFloatingResource.getResourceLimit();
if (Objects.isNull(resourceLimit)) {
      return true;
}
//已展示次数大于等于配置的展示次数,则过滤当前浮层
return showCount < resourceLimit;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值