【问题标题】:How to cache with condition on result object in spring boot如何在spring boot中缓存结果对象的条件
【发布时间】:2021-05-03 14:58:40
【问题描述】:

我想将结果缓存在 Spring Boot 中,并带有结果。

@Cacheable(value = "saveCache", key = "{#a, #b, #c}")
public Response save(String a, String b, String c) {
  // block of code
  List<Map<String, Object>> result = new ArrayList();

  new Response(result)
}

在上面的代码中,我只想在 response.result 不为空时缓存。我试过下面的方法,但它对我不起作用

@Cacheable(value = "saveCache", key = "{#a, #b, #c}", unless="#result.result == null")

@Cacheable(value = "saveCache", key = "{#a, #b, #c}", unless="#result.result.size > 0")

[错误] EL1008E: 在对象上找不到属性或字段“大小” 输入“java.util.ArrayList” - 可能不公开或无效?

@Cacheable(value = "saveCache", key = "{#a, #b, #c}", unless="#result.result.length > 0")

[错误] EL1008E:在对象上找不到属性或字段“长度” 'java.util.ArrayList' 类型的 - 可能不公开或无效?

【问题讨论】:

    标签: java spring-boot caching


    【解决方案1】:

    因为 size 是 ArrayList 的一种方法。请这样使用。

    @Cacheable(value = "saveCache", key = "{#a, #b, #c}", unless="#result.result.size() > 0")
    

    参考: SpringBoot Cacheable unless result with List

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-31
      • 1970-01-01
      • 2020-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多