首页 > 解决方案 > 如何在 @CacheEvict#key 的 SpEL 中获取#root.args[0] 的字段

问题描述

@Service
public class UserService {
    private final User[] database = new User[]{new User(0, "foo")};
    @Cacheable(cacheNames = "springboot:demo:cache", key = "#root.args[0]")
    public User query(Integer id) {
        return database[0];
    }

    @CacheEvict(cacheNames = "springboot:demo:cache", key = "#root.args[0].id")
    public void update(User user) {
        database[0] = user;
    }
}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {

    private static final long serialVersionUID = -1760350679406802739L;
    Integer id;
    String name;
}

update调用方法后,springboot:demo:cache:{id}从方法生成的query仍然存在于缓存(Redis)中。我应该怎么做才能准确地驱逐它?非常感谢。

标签: javaspringspring-bootspring-el

解决方案


推荐阅读