英文:
ehcache Map<String,Entry> not work springboot
问题
我尝试过缓存Map<String, Entry>,但每次调用getEntries()时,都会在没有缓存的情况下命中数据库,
同时我还对Entry对象进行了序列化,请提供支持。
@Cachable("stocks")
public Map<String, Entry> getEntries() {
// 从数据库获取entry,然后转换为map
return map;
}
英文:
i tried to cache Map<String,Entry> , but every time i found getEntries() hit database without caching ,
also i serialize Entry object , please yours support
@Cachable("stocks")
public Map<String,Entry> getEntries(){
//getting entry from database then convert to map
return map;
}
答案1
得分: 1
这对我有效
@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, "有趣的条目"));
return map;
}
}
这是一个用来证明计数器未被调用的测试
@Test
public void entry() throws Exception {
OrderService.counter = 0;
orderService.getEntries();
assertEquals(1, OrderService.counter);
orderService.getEntries();
assertEquals(1, OrderService.counter);
}
我已将所有内容添加到我的github示例中。
英文:
This works for me
@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;
}
}
Here's a test to prove the counter is not called.
@Test
public void entry() throws Exception {
OrderService.counter = 0;
orderService.getEntries();
assertEquals(1, OrderService.counter);
orderService.getEntries();
assertEquals(1, OrderService.counter);
}
I've added it all to my github example
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论