将 spring @Cacheable 与 keyGenerator 一起使用,除非属性

问题描述

使用 spring-boot 2.4.3 org.springframework:spring-context:5.3.4

我有一个使用 keyGenerator 的 @Cacheable 方法 它工作正常。 我想使用静态方法添加除非属性,但它不起作用。

注解的示例代码(不调用静态方法

@Cacheable(value = CURRENCY_CHANNELS_CACHE,keyGenerator = CURRENCY_CHANNEL_CACHE_KEY_GENERATOR,unless = "#{ T(com.cache.MyCacheKeyGenerator).isQueryNotCacheable(#p0) }")
public List<Item> getQueryForCollection(final QueryBuilder query,final String sort)
public static boolean com.cache.MyCacheKeyGenerator.isQueryNotCacheable(final QueryBuilder query)

没有被调用。我收到找不到它的错误

解决方法

您在 unless 参数中使用了错误的 SpEL 语法。正确的语法是:

unless = "T(com.cache.MyCacheKeyGenerator).isQueryNotCacheable(#p0)"

我在这里测试了解决方案:https://github.com/ygor-sk/stackoverflow/tree/master/q67168108-cacheable-unless-static

下次,请直接提供您在问题中得到的错误。一定很明显:

EL1043E:意外的令牌。应为“标识符”但为“lcurly({)”

,

谢谢。它现在有效。 我没有打开弹簧的痕迹。