use*_*636 3 spring caching ehcache
使用Spring 3.2和EhCache 2.9.我注释了一个零参数方法如下:
@Cacheable(value="myList", key="#result.method.name")
protected List<MyObject> getMyList() {
//db query
//return list of results
}
Run Code Online (Sandbox Code Playgroud)
EhCache配置:
<cache name="myList"
statistics="true"
maxEntriesLocalHeap="1"
timeToLiveSeconds="3600">
<persistence strategy="none" />
</cache>
Run Code Online (Sandbox Code Playgroud)
我想要缓存数据库结果.由于此方法没有参数,因此我选择了方法名称作为缓存键.
当我测试这个时,每个方法调用都会遇到数据库,我不知道为什么.有任何想法吗?
UPDATE
因此在排除故障后我发现了一些有趣 目前,getMyList定义高速缓存的方法与调用它的方法相同.该方法基本上调用DAO来查询列表.如果我getMyList向外移动到另一个只充当代理的类,然后我改变原始调用者来代替调用这个新代理,那么缓存就可以了.我无法解释原因.有什么输入?
Rub*_*ben 13
想象一下你去动物园.你通过入口一次并支付你的入场费.之后你可以参观狮子会,老虎队等等......你不必每次都付钱,因为你在进入时就这么做了.如果你觉得无聊并想去另一个动物园,你必须出去,去下一个,然后再付钱.
您的类是动物园,您的方法是动物,高速缓存代理是入口.当有人打电话给你的班级时,它会通过缓存一次.当她进入并调用同一类的其他方法时,它不会再次通过缓存.只有当你再次出去时,才能通过缓存.
你可以使用一个讨厌的技巧来覆盖这个叫做自己注入:
public class YourClass {
@Autowired
private YourClass instance;
@Cacheable
public String method1() {
// now you go through the cache again
return instance.method2();
}
@Cacheable
public String method2() {
return "2";
}
}
Run Code Online (Sandbox Code Playgroud)
Ste*_*ers 13
@Cacheable 不工作的最有趣的原因是你在“使用”
springfox.documentation.annotations.Cacheable
Run Code Online (Sandbox Code Playgroud)
代替
org.springframework.cache.annotation.Cacheable
Run Code Online (Sandbox Code Playgroud)
请注意,当您没有正确的依赖项(尚未)并且您的 IDE 自动执行导入时,这很容易发生。
| 归档时间: |
|
| 查看次数: |
7709 次 |
| 最近记录: |