背景要求:对数据做统计分析,时间截止到当天零点

根据要求,每天查询的数据范围都是截止前一天结束,第二天需要查询新数据。那么缓存只保留一天。

使用caffeine简单举个例子,主要依赖有:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
              <version>2.7.18</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
            <version>2.7.18</version>
        </dependency>
        <dependency>
            <groupId>com.github.ben-manes.caffeine</groupId>
            <artifactId>caffeine</artifactId>
            <version>2.9.3</version>
        </dependency>

增加一个到期策略,主要是计算当前时间和明天零点零分零秒的时间差:

import com.github.benmanes.caffeine.cache.Expiry;
import org.checkerframework.checker.index.qual.NonNegative;
import org.checkerframework.checker.nullness.qual.NonNull;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;


public class CurrentDayCacheExpire implements Expiry {

    @Override
    public long expireAfterCreate(@NonNull Object o, @NonNull Object o2, long l) {
        return expireTime();
    }

    @Override
    public long expireAfterUpdate(@NonNull Object o, @NonNull Object o2, long l, @NonNegative long l1) {
        return expireTime();
    }

    @Override
    public long expireAfterRead(@NonNull Object o, @NonNull Object o2, long l, @NonNegative long l1) {
        return expireTime();
    }

    /**
     * 零点过期
     * @return
     */
    private long expireTime() {
        LocalDate currentDate = LocalDate.now(ZoneId.systemDefault());
        return ChronoUnit.NANOS.between(
                LocalDateTime.of(currentDate, LocalTime.now(ZoneId.systemDefault())),
                LocalDateTime.of(currentDate.plusDays(1L), LocalTime.MIN));
    }
}

然后就可以在配置中使用了:

import com.cmcc.ihr.rpa.cache.CurrentDayCacheExpire;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

@Configuration
@EnableCaching
public class CacheConfig {

    @Bean("caffeineCacheManager")
    public CaffeineCacheManager createCaffeineDayCacheManager() {
        CaffeineCacheManager manager = new CaffeineCacheManager();
        // 零点过期
        manager.registerCustomCache("overview", Caffeine.newBuilder().expireAfter(new CurrentDayCacheExpire()).maximumSize(1L).build());
        manager.registerCustomCache("dataTrend", Caffeine.newBuilder().expireAfter(new CurrentDayCacheExpire()).maximumSize(2L).build());
        // 角色5分钟过期
        manager.registerCustomCache("roleCache", Caffeine.newBuilder().expireAfterAccess(Duration.ofMinutes(5L)).maximumSize(64L).build());
        // 其他过期配置
        return manager;
    }

}

最后还要在查询方法上加上缓存注解:

     @Cacheable(value = "overview", cacheManager = "caffeineCacheManager")
    public Object queryOverview() {
        // sql 查询
        return "SQL查询结果集";
    }

    @Cacheable(value = "dataTrend", cacheManager = "caffeineCacheManager")
    public Object queryTrendData() {
        // sql 查询
        return "SQL查询结果集";
    }

虚惊一百场
19 声望7 粉丝

1 + 1 = 2


« 上一篇
Redis的持久化