【问题标题】:ehcache Map<String,Entry> not work springbootehcache Map<String,Entry> 不起作用 springboot
【发布时间】:2021-01-19 09:07:04
【问题描述】:

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

@Cachable("stocks")
 public Map<String,Entry> getEntries(){
    //getting entry from database then convert to map
  return map;
 }

【问题讨论】:

    标签: java spring-boot caching ehcache


    【解决方案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, "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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-03
      • 2018-12-08
      • 2013-09-25
      • 2016-02-02
      • 2020-07-25
      • 2021-11-27
      • 2016-07-04
      相关资源
      最近更新 更多