我试图在我的 Spring 应用程序中使用谷歌番石榴缓存,但结果永远不会缓存。
这是我的步骤:
在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");
它每次都进入函数并且不使用缓存:
安慰:
cache not working
cache not working
cache not working
有人知道我缺少什么或者我该如何调试它吗?
你不应该自己管理春豆。让 spring 来管理它。
@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");
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)