【问题标题】:Spring Cacheable not working with default keySpring Cacheable 不能使用默认键
【发布时间】:2019-11-19 00:19:30
【问题描述】:

当我使用默认键时,Spring 没有缓存我的函数 -

@PostMapping("getDashboardDataNew")
@Cacheable(value="myDash")
public DashboardDto getHomeDashboardDataNew(@RequestBody DashboardRequest dashboardRequest) {
    LOGGER.info(" Get All the Dashboard Information : ");
    //code
    return dashboardDto;
}

但是当我使用 sPEL 提供自定义密钥时,它会缓存响应,例如。

@PostMapping("getDashboardDataNew")
@Cacheable(value="myDash", key="#dashboardRequest.level")
public DashboardDto getHomeDashboardDataNew(@RequestBody DashboardRequest dashboardRequest) {
    LOGGER.info(" Get All the Dashboard Information : ");
    //code
    return dashboardDto;
}

请求负载总是-

{"fromDate":null,"toDate":null,"theme":null,"activity":null,"level":1,"levelValue":null,"state":null,"district":空}

即使在使用 eclipse 自动生成 equals 和 hashcode 之后,spring 也不会缓存该值。以下是自动生成的代码

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((activity == null) ? 0 : activity.hashCode());
    result = prime * result + ((fromDate == null) ? 0 : fromDate.hashCode());
    result = prime * result + ((level == null) ? 0 : level.hashCode());
    result = prime * result + ((levelValue == null) ? 0 : levelValue.hashCode());
    result = prime * result + ((organizer == null) ? 0 : organizer.hashCode());
    result = prime * result + ((theme == null) ? 0 : theme.hashCode());
    result = prime * result + ((toDate == null) ? 0 : toDate.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    DashboardRequest other = (DashboardRequest) obj;
    if (activity == null) {
        if (other.activity != null)
            return false;
    } else if (!activity.equals(other.activity))
        return false;
    if (fromDate == null) {
        if (other.fromDate != null)
            return false;
    } else if (!fromDate.equals(other.fromDate))
        return false;
    if (level == null) {
        if (other.level != null)
            return false;
    } else if (!level.equals(other.level))
        return false;
    if (levelValue == null) {
        if (other.levelValue != null)
            return false;
    } else if (!levelValue.equals(other.levelValue))
        return false;
    if (organizer == null) {
        if (other.organizer != null)
            return false;
    } else if (!organizer.equals(other.organizer))
        return false;
    if (theme == null) {
        if (other.theme != null)
            return false;
    } else if (!theme.equals(other.theme))
        return false;
    if (toDate == null) {
        if (other.toDate != null)
            return false;
    } else if (!toDate.equals(other.toDate))
        return false;
    return true;
}

我没有更改请求负载。

【问题讨论】:

  • 为此,您需要实现正确的equalshashCode,否则将不匹配。
  • @M.Deinum 谢谢,但它仍然没有缓存,请参阅更新的问题。
  • 向单个对象添加等号不会有帮助。您还需要在其他引用对象中包含等号和 hashCode,至少如果它们是其他 java 原始/基本类型。并确保为DashboardRequest 生成hashCodeequals
  • @M.Deinum 都是字符串。
  • 不要将其他代码/信息添加为 cmets。那是不可读的。

标签: spring spring-boot spring-cache


【解决方案1】:

默认情况下,当没有提供 key 时,Spring 缓存依赖 SimpleKeyGenerator,它依赖于参数的 hashcode 来生成 key。你可以查看这个link

【讨论】:

    【解决方案2】:

    我知道这里出了什么问题。 我正在函数内部某处更改请求有效负载的属性之一,例如。

    dashboardRequest.setLevel(dashboardRequest.getLevel() + 1);

    并且由于在方法执行使用修改后的对象而不是参数中提供的值之后,spring cache AOP 将值放入缓存中,从而有效地使我的密钥与请求有效负载生成的密钥不同。希望这对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-02
      • 1970-01-01
      • 2016-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多