[英]spring - using google guava cache
我試圖在我的春季應用程序中使用谷歌番石榴緩存,但結果永遠不會緩存。
這是我的步驟:
在conf文件中:
@EnableCaching
@Configuration
public class myConfiguration {
@Bean(name = "CacheManager")
public CacheManager cacheManager() {
return new GuavaCacheManager("MyCache");
}
}
在課堂上我想使用緩存:
public class MyClass extends MyBaseClass {
@Cacheable(value = "MyCache")
public Integer get(String key) {
System.out.println("cache not working");
return 1;
}
}
然后當我打電話時:
MyClass m = new MyClass();
m.get("testKey");
m.get("testKey");
m.get("testKey");
它每次進入功能而不使用緩存:console:
cache not working
cache not working
cache not working
有人知道我錯過了什么或者我該如何調試?
你不應該自己管理一個春豆。 讓春天來管理它。
@EnableCaching
@Configuration
public class myConfiguration {
@Bean(name = "CacheManager")
public CacheManager cacheManager() {
return new GuavaCacheManager("MyCache");
}
@Bean
public MyClass myClass(){
return new MyClass();
}
}
之后,您應該以托管方式使用MyClass。
public static void main(String[] args) throws Exception {
final ApplicationContext applicationContext = new AnnotationConfigApplicationContext(myConfiguration.class);
final MyClass myclass = applicationContext.getBean("myClass");
myclass.get("testKey");
myclass.get("testKey");
myclass.get("testKey");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.