cacheevict on条件注解-spring缓存

dxpyg8gm  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(474)

我有一个奇怪的案例,我的@cacheevict不起作用。代码如下所示:

@Caching(evict = {
        @CacheEvict(value = CacheConsts.C_CACHE1, keyGenerator = CacheConsts.KG_CACHE1, condition="#someModel != null && #someModel.getSomeProperty() != null"),
        @CacheEvict(value = CacheConsts.C_CACHE2, keyGenerator = CacheConsts.KG_CACHE2, condition="#someModel != null && #someModel.getSomeProperty() != null"),
        @CacheEvict(value = CacheConsts.C_CACHE3, keyGenerator = CacheConsts.KG_CACHE3, condition="#someModel != null && #someModel.getSomeProperty() != null")
})
public boolean addModel(ModelDTO someModel, String tenant);

但是,它的工作时,我删除的条件!!!即使我测试的所有数据都是非空的。
例如:当我删除此项时:“#somemodel!=null&&#somemodel.getsomeproperty()!=空”,它工作。
我正在测试一个modeldto,它不为null,“someproperty”也不为null。
在我看来,情况会过去,它会驱逐。。但事实并非如此。
有什么想法吗?
我的口语正确吗?
为什么我的缓存不能在这里收回?
这与@caching注解或cacheevict条件的某些行为有关吗?
谢谢大家的帮助和建议。

rekjcdws

rekjcdws1#

afaict,你的语言表达和 condition 似乎是对的。
您可能需要验证编译器是否 debug 设置为 true ,这是通过“name”引用方法参数所必需的,因为编译器随后将在java字节码中包含变量名。
您也可以尝试在spel表达式中引用方法参数,通常(出于调试目的),例如: #a0 != null && #a0.someProperty() ; 参见文件中的本节。
最后,我编写了一个简单的集成测试来模拟上面的uc。
测试和支持代码(都包含在引用的测试类中)在某种程度上是相似的。但是,我的代码略有不同,因为我没有使用自定义 KeyGenerator (根据 Cache ),因此我不需要 Caching 注解(我只是使用 cacheNames ),但这应该没有什么影响。考试通过了!
希望这能给你更多的想法。
如果需要的话,可以随意玩我的测试来做实验。
干杯!

相关问题