Spring ehCache根据Bean值跳过缓存

3
我想知道是否有可能通过一些外部条件来跳过缓存,例如bean字段值。
以下是我的缓存方法:
@Cacheable(value = "MY_CACHE", keyGenerator = "myKeyGenerator")
public List<CloudEntity> getTestMetaRecords(final String someParam) {
    ...
    return aRetrievedList;
}

我希望的是只跳过与缓存方法无关的值。我知道在@Cacheable注释中有unless参数,但我没有成功使用它。我添加了以下内容:
unless = "#myBean.getSomeValue().equals(${value.from.properties})"

任何帮助都非常感激!
更新:
好的,我已经发现unlesscondition参数可以用于条件缓存,但它们在 spel 中使用有限的元数据集: methodName、method、target...... 而且我只能通过root.target表达式引用被调用对象字段来实现它。
我的结果代码如下:
@Cacheable(value = "MY_CACHE", keyGenerator = "myKeyGenerator", condition = "#root.target.myBean.getSomeValue().equals(#root.target.valueFromProperties)")

我不喜欢这种解决方案的原因是目标类与缓存方法必须更改以包含我的依赖项myBean。是否有更灵活的解决方案?

能否使用unless评估返回为字符串对象的JSON?我有以下JSON值:{"status":"FAILED","errorcode":"0001","error":"..."}。我想检查是否为“ unless='#result.status==FAILED' ”。这个表达式能够被评估吗? - nijogeorgep
2个回答

0

我遇到了差不多相同的问题。

事实上,对于@Cachable注释的conditionunless属性的SpEL表达式的上下文不允许从ApplicationContext中检索bean。

但是,我能够使用T()运算符通过静态方法检索应用程序上下文,然后使用它来检索我的bean并调用我需要的条件方法。

因此,在我的情况下,最终的SpEL表达式是这样的:

condition="T(my.utils.package.Registry).getApplicationContext().getBean('myBean').cachingCondition()"

0
尝试使用目标直接作为,对我来说很有效。
@Cacheable(value = "MY_CACHE", keyGenerator = "myKeyGenerator", condition = "target.myBean.getSomeValue().equals(target.valueFromProperties)")

或者

@Cacheable(value = "MY_CACHE", keyGenerator = "myKeyGenerator", condition = "target.myBean.cachingCondition()")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接