我有一个奇怪的情况,我的@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);
然而,它工作时,我删除条件!!即使我测试的所有数据都是非空的
示例:当我删除这个时:"# someemodel != null &&#someModel.getSomeProperty() != null",它工作了。
我正在测试一个不为空的ModelDTO和"someProperty"也不为空。
在我看来,这种情况会过去,它会驱逐……但事实并非如此。
任何想法?
我的咒语形成正确吗?
为什么我的缓存在这里没有被清除?
这是否与@Caching注释或我不知道的CacheEvict条件的一些行为有关?
感谢大家的帮助和建议。
AFAICT,您的SpEL表达式和condition
似乎是正确的。
你可能想要验证你的编译器已经将debug
设置为true
,这需要通过"name"引用方法参数。因为编译器会在Java字节码中包含变量名。
您也可以尝试在SpEL表达式中引用方法参数,一般(用于调试目的),例如:#a0 != null && #a0.someProperty()
;参考文档中的这一节。
最后,我写了一个简单的集成测试模拟你的UC上面。
测试和支持代码(都包含在引用的测试类中)在一定程度上是相似的。然而,我的代码略有不同,因为我没有使用自定义的KeyGenerator
(每个Cache
),因此我不需要包含Caching
注释(我只是使用cacheNames
),但这应该无关紧要。考试通过了!
希望这能给你更多的想法。
如果有必要,请随意使用我的测试进行实验。
干杯!