问题描述
我试图缓存Map
@Cachable("stocks")
public Map<String,Entry> getEntries(){
//getting entry from database then convert to map
return map;
}
解决方法
这对我有用
@Service
public class OrderService {
public static int counter = 0;
@Cacheable("stocks")
public Map<String,Entry> getEntries() {
counter++;
final Map<String,Entry> map = new HashMap<>();
map.put("key",new Entry(123l,"interesting entry"));
return map;
}
}
这是一个证明不调用计数器的测试。
@Test
public void entry() throws Exception {
OrderService.counter = 0;
orderService.getEntries();
assertEquals(1,OrderService.counter);
orderService.getEntries();
assertEquals(1,OrderService.counter);
}
我已将所有内容添加到我的github example