java - 在 @CacheEvict 的键中使用非常量属性或字段

标签 java spring spring-cache

是否可以在@CacheEvict 注解的键中使用当前类的非常量字段或属性?例如:

public class Feature {

    private int id;

    @Autowired
    private FeaturesClient featuresClient;

    @CacheEvict(value = CacheConfiguration.FEATURES, key = 
               "T(java.lang.String).valueOf(#userId).concat(T(java.lang.String)" +
               ".valueOf( **#id** ))")
    public boolean isFeatureAvailable(long userId) {
        return featuresClient.isFeatureAvailable(userId, id);
    }

}

最佳答案

是的。

@CacheEvict(key = "#userId + #root.target.id")

Documentation :

#root.method, #root.target, and #root.caches for references to the method, target object, and affected cache(s) respectively.

关于java - 在 @CacheEvict 的键中使用非常量属性或字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45799079/

相关文章:

java - 如何使用 ProcessBuilder 在 Java 代码中运行 NPM 命令

java - XSLT 与 XPAth 2.0 和 Java 表达式

java - 如何在 D 维的单位立方体/球体中生成一组均匀分布的 K 个点

java - 在 Spring Boot 应用程序中更改 application.properties 名称

java - Springcache 不仅仅适用于 XML 配置

Java转换字符串,具有毫秒至今的对象

java - 使用 Mockito 进行依赖注入(inject)的问题

java - Intellij Idea Tomcat 和 Spring MVC 模板

java - 在 Cacheable 方法类中调用 Spring Cacheable 方法抛出异常

java - 在应用程序启动时缓存查找不起作用