Caffeine is a Java 8 rewrite of Guava’s cache and will supersede the Guava support in Spring Boot 2.0.
compile("org.springframework.boot:spring-boot-starter-cache")
compile("com.github.ben-manes.caffeine:caffeine")
@EnableCaching
@Configuration
@Bean
public CacheManager caffeineCacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
List<CaffeineCache> caches = Lists.newArrayList();
caches.add(new CaffeineCache(Caches.getCategories.name()
, Caffeine.newBuilder().recordStats()
.expireAfterWrite(DEFAULT_TTL, TimeUnit.MINUTES)
.maximumSize(DEFAULT_MAXSIZE)
.build())
);
cacheManager.setCaches(caches);
return cacheManager;
}
@Cacheable(cacheNames = "getCategories", cacheManager = "caffeineCacheManager")
public List<Category> getCategories(List<Long> categoryIds) {
// TODO ...
return results.stream()
.map(dto -> new Category(dto.getCategoryId()))
.collect(Collectors.toList());
}
# https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-caching.html#boot-features-caching-provider-caffeine
compile("org.springframework.boot:spring-boot-starter-cache")
compile("com.github.ben-manes.caffeine:caffeine")
@EnableCaching
@Configuration
@Bean
public CacheManager caffeineCacheManager() {
SimpleCacheManager cacheManager = new SimpleCacheManager();
List<CaffeineCache> caches = Lists.newArrayList();
caches.add(new CaffeineCache(Caches.getCategories.name()
, Caffeine.newBuilder().recordStats()
.expireAfterWrite(DEFAULT_TTL, TimeUnit.MINUTES)
.maximumSize(DEFAULT_MAXSIZE)
.build())
);
cacheManager.setCaches(caches);
return cacheManager;
}
@Cacheable(cacheNames = "getCategories", cacheManager = "caffeineCacheManager")
public List<Category> getCategories(List<Long> categoryIds) {
// TODO ...
return results.stream()
.map(dto -> new Category(dto.getCategoryId()))
.collect(Collectors.toList());
}
# https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-caching.html#boot-features-caching-provider-caffeine
댓글
댓글 쓰기