简体   繁体   English

Spring Boot默认不使用自定义密钥生成器

[英]Custom Key generator not being used by default in Spring Boot

I have a custom key generator and it only gets called if I use the "keyGenerator" property in the @Cachehable annotation. 我有一个自定义的密钥生成器,只有在使用@Cachehable批注中的"keyGenerator"属性时才会调用它。

My configuration class that creates it is pulled in as a gradle dependency (and is loaded and my key generator is created.). 创建它我的配置类被拉作为一个依赖的Gradle(和加载并创建了密钥生成器)。

Config: 配置:

@EnableCaching
@Configuration
public class CacheKeyConfig extends CachingConfigurerSupport
{
    private static final Logger LOGGER = LoggerFactory.getLogger( CacheKeyConfig.class );

    @Bean
    @Primary
    public KeyGenerator keyGenerator(@Autowired( required = false ) List<CacheKeyGenerator> keyGenerators)
    {
        return new DynamicCacheKeyGenerator( keyGenerators );
    }
}

The list I pass it is a set of key generators the developer can create (as beans) that can be used for a specific cache resolver/manager. 我通过的列表是开发人员可以创建的一组密钥生成器(作为Bean),可用于特定的缓存解析器/管理器。 So it allows a different key generator for MemCache, or Redis or EhCache (if you were to use multiple in one project). 因此,它允许MemCache,Redis或EhCache使用不同的密钥生成器(如果您要在一个项目中使用多个)。 But this can be an empty list. 但这可以是一个空列表。

The key generator: 密钥生成器:

public class DynamicCacheKeyGenerator implements KeyGenerator
{
    private static Logger LOGGER = LoggerFactory.getLogger( DynamicCacheKeyGenerator.class );

    private Map<Method, CacheKeyGenerator> keyGenerators = new HashMap<>();

    @Autowired
    public DynamicCacheKeyGenerator(List<CacheKeyGenerator> keyGenerators)
    {
        //Map the generators to the resolvers
        mapGeneratorsToResolvers( CacheResolverFinder.getMethodCacheResolvers(), keyGenerators );
    }

    //...elided...

    @Override
    public Object generate(Object target, Method method, Object... params)
    {
        //Try to get the cache key handler for this method
        CacheKeyGenerator generator = keyGenerators.get( method );

        //...elided...

        return generator.generate( target, method, params );
    }
}

The following will never use my key generator: 以下将永远不会使用我的密钥生成器:

@Cacheable( value = "MyCache", cacheResolver = "MyResolver" )

But the following does use it: 但是以下确实使用它:

@Cacheable( value = "MyCache", cacheResolver = "MyResolver", keyGenerator = "keyGenerator" )

How do I get mine to be the default? 如何获得我的默认值?

EDIT: Even changing to the following does not use my key generator: 编辑:即使更改为以下不使用我的密钥生成器:

@EnableCaching
@Configuration
public class CacheKeyConfig extends CachingConfigurerSupport
{
    private static final Logger LOGGER = LoggerFactory.getLogger( CacheKeyConfig.class );

    @Autowired
    private List<CacheKeyGenerator> myKeyGenerators;    

    //This DOES get called
    @Override
    public KeyGenerator keyGenerator()
    {
        return new DynamicCacheKeyGenerator( myKeyGenerators );
    }
}

Something to keep in mind is that if you have defined a key in the Cacheable annotation, that the key generator is not utilised. 要记住的一点是,如果您在Cacheable批注中定义了一个键,则不会使用键生成器。 Ensure these are removed to validate 确保将其删除以验证

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring 引导 - 如何在 @GenericGenerator 中使用的自定义序列生成器中读取 application.properties - Spring Boot - How to read application.properties in custom sequence generator which used in @GenericGenerator Spring 启动 JPA 中的自定义序列生成器 - Custom Sequence Generator in Spring Boot JPA Ehcache Spring-自定义密钥生成器 - Ehcache Spring- custom key generator Spring @Cacheable,除非由自定义密钥生成器创建的属性引用密钥 - Spring @Cacheable unless property reference key created by custom key generator 默认情况下如何在休眠中使用自定义密钥生成器 - how to make use of a custom key generator in hibernate by default 我如何知道是否正在使用Spring Boot中的Mock? - How do I know if Mock in Spring boot is being used or not? 实现AngularJS时未使用Spring-boot RESTful服务 - Spring-boot RESTful services not being used when implementing AngularJS Spring Boot:添加了外部messages.properties但未使用 - Spring Boot: external messages.properties are being added but not used Spring 4 @cacheable注解:如何为自定义密钥生成器添加属性? - Spring 4 @cacheable annotation : how add properties for custom key generator? Spring boot - 使用配置文件时未读取默认属性文件 - Spring boot - Default properties file not being read when using profile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM