ehcache Map <String,Entry>不起作用springboot

问题描述

我试图缓存Map ,但是每次我发现getEntries()命中数据库而没有缓存时, 我也序列化Entry对象,请支持

@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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...