Closed sunmingtao closed 4 years ago
A better version:
@Configuration
@EnableCaching
public class CacheConfig {
private final long batchJobExpireInMinutes;
private final long jobMostUsedToolsExpireInDays;
public CacheConfig(@Value("${banjo.batch.job.cache.expire.minutes}") final long batchJobExpireInMinutes,
@Value("${banjo.job.most.used.tools.expire.days}") final long jobMostUsedToolsExpireInDays) {
this.batchJobExpireInMinutes = batchJobExpireInMinutes;
this.jobMostUsedToolsExpireInDays = jobMostUsedToolsExpireInDays;
}
@Bean
public CacheManager cacheManager() {
final CaffeineCache batchJobCache = buildCache("batchJobCache", batchJobExpireInMinutes, TimeUnit.MINUTES);
final CaffeineCache jobToolsCache = buildCache("jobToolsCache", jobMostUsedToolsExpireInDays, TimeUnit.DAYS);
final SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(List.of(batchJobCache, jobToolsCache));
return cacheManager;
}
private CaffeineCache buildCache(final String name, final long expire, final TimeUnit timeUnit) {
return new CaffeineCache(name, Caffeine.newBuilder()
.expireAfterWrite(expire, timeUnit)
.build());
}
Spring 5+
Spring bean config
Service