弹簧可缓存不使用默认密钥



当我使用默认键,例如 -

时,春季没有缓存我的功能
@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," active":null," latver":1," levelvalue":null":null"," state":nate":null,null," district":district':null}

即使在使用Eclipse自动生成平等和哈希码之后,弹簧也不会缓存该值。以下是自动生成的代码

@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;
}

我没有更改请求有效载荷。

默认情况下,当不提供键时,Spring Cache依赖于Simple KeykeGenerator,该键依赖于参数的哈希码来生成键。您可以检查此链接。

我弄清楚这里出了什么问题。我正在更改函数中某个地方的请求有效载荷的属性之一。

dashboardrequest.setlevel(dashboardrequest.getlevel(( 1(;

,随着弹簧缓存AOP的使用,方法执行使用修改对象后,将值放在缓存中,而不是在参数中提供的值有效地使我的密钥与请求有效载荷生成的密钥不同。希望这对某人有帮助。

最新更新