[英]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.