A sample code using Caffeine Cache through Annotation in Spring
- Available asynchronously refresh cache setting in annotation.
@Service
public class TimeService {
@SimpleCaffeineCache(name = "getNowCache", expireAfterWrite = 20, refreshAfterWrite = 5)
public String getNow(String name) throws InterruptedException {
Thread.sleep(3000);
return name + " / " + DateTime.now().toString();
}
}
build.gradle
repositories {
maven {
url "https://github.com/kimwz/caffeine-cache-annotation/raw/master/release/"
}
}
compile('com.github.kimwz.caffeinecache:caffeinecache-annotation:1.0')
// default dependencies
compile('org.aspectj:aspectjweaver:1.8.6')
compile("com.github.ben-manes.caffeine:caffeine:2.3.1")
CacheConfig.java
@Configuration
@EnableAspectJAutoProxy
public class CacheConfig {
@Bean
public SimpleCaffeineCacheAspect simpleCaffeineCache() {
return new SimpleCaffeineCacheAspect();
}
}