【问题标题】:@Cacheable conditional caching on request fields@Cacheable 请求字段的条件缓存
【发布时间】:2019-01-31 08:34:20
【问题描述】:

我已经使用spring @Cacheable 进行缓存,如下所示:

@Cacheable
public Response mycall(Request request)

现在我希望仅当 request.getId()!=3 时才缓存此方法调用,其中 getId() 是 Request 类中的公共 getter 方法。

我看到人们在@Cacheable 中指定了unless 条件的代码,但我看到仅在方法的响应上而不是在请求上指定条件。参考:How do I tell Spring cache not to cache null value in @Cacheable annotation

在请求的字段上指定条件时,有什么方法可以达到同样的效果

【问题讨论】:

  • 你不想要像unless="#request.id == 3" 这样的东西吗?看起来参数可以按名称使用,结果是名称“#result”,在除非表达式中...
  • 我试过了,不行

标签: java spring spring-data


【解决方案1】:

你试过条件属性了吗?

//It will cache only if the actor's age is more than 20. Here actor is the input parameter.
@Cacheable(value="movies", key="#actor.name", condition="#actor.age > 20")
public List<movie> getMovies(Actor actor) ...

ttps://www.codelooru.com/2017/03/spring-cache-part-3-conditional-cache.html

【讨论】:

    猜你喜欢
    • 2022-01-20
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    相关资源
    最近更新 更多