通过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;