本文介绍了带有自定义 cacheResolver 的 spring 缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要动态缓存名称,和 spring 4.1 允许

I wand to have dynamic cache names, and spring 4.1 allows that

从 Spring 4.1 开始,缓存注解的 value 属性不再是强制性的,因为无论注解的内容如何,​​CacheResolver 都可以提供此特定信息.

请注意我如何在所有可能的级别上偏执地设置 cacheResolver:

Notice how I paranoidly set cacheResolver on all possible levels:

@Cacheable(cacheResolver = "defaultCacheResolver")
@CacheConfig(cacheResolver = "defaultCacheResolver")
public interface GatewayRepository extends CrudRepository<Gateway, Integer> {
    @Cacheable(cacheResolver = "defaultCacheResolver")
    Gateway findByBulkId(int bulkId);
}

Spring 4.1.5 仍然无法验证配置并出现错误:Caused by: java.lang.IllegalStateException: No cache names could be detected on 'public abstract skunkworks.data.Gateway skunkworks.repos.GatewayRepository.findByBulkId(int)'.确保在注释上设置 value 参数或在类级别声明 @CacheConfig 并使用要使用的默认缓存名称.在 org.springframework.cache.annotation.SpringCacheAnnotationParser.validateCacheOperation(SpringCacheAnnotationParser.java:240)

Spring 4.1.5 still fails to validate the config with error: Caused by: java.lang.IllegalStateException: No cache names could be detected on 'public abstract skunkworks.data.Gateway skunkworks.repos.GatewayRepository.findByBulkId(int)'. Make sure to set the value parameter on the annotation or declare a @CacheConfig at the class-level with the default cache name(s) to use. at org.springframework.cache.annotation.SpringCacheAnnotationParser.validateCacheOperation(SpringCacheAnnotationParser.java:240)

推荐答案

我认为您应该在代码中的某处指定缓存名称.

I think you should specify cache name somewhere in your code.

在基本用法中,缓存名称在@Cacheable、@CachePut 或@CacheEvict 注释中给出.

In the basic usage, cache name is given in @Cacheable, @CachePut or @CacheEvict annotations.

@Cacheable(cacheNames = "myCache")

您也可以在类级注解@CacheConfig中指定.

You can also specify it in the @CacheConfig which is a class-level annotation.

@CacheConfig(cacheNames = "myCache")

如果需要更灵活的缓存机制,可以使用CacheResolver.在这种情况下,您应该创建自己的 CacheResolver.一些类似的东西:

If you need more flexible caching mechanism, you can use CacheResolver. In this case you should create your own CacheResolver. Something along these lines:

public class CustomCacheResolver implements CacheResolver {

    private final CacheManager cacheManager;

    public CustomCacheResolver(CacheManager cacheManager){
        this.cacheManager = cacheManager;
    }

    @Override
    public Collection<? extends Cache> resolveCaches(CacheOperationInvocationContext<?> context) {
        Collection<Cache> caches = new ArrayList<>();
        if(context.getTarget().getClass() == GatewayRepository.class){
            if(context.getMethod().getName().equals("findByBulkId")){
                caches.add(cacheManager.getCache("gatewayCache"));
            }
        }

        return caches;
    }
}

在这一步中,缓存名称是

In this step, cache name is

网关缓存

在cacheresolver中单独定义,注解端可以省略.

which is defined solely in the cacheresolver and it can be omitted in the annotation side.

在这一步之后,你应该注册CacheResolver:

After this step, you should register CacheResolver:

@Configuration
@EnableCaching
public class CacheConfiguration extends CachingConfigurerSupport {

    @Bean
    @Override
    public CacheManager cacheManager() {
         // Desired CacheManager
    }

    @Bean
    @Override
    public CacheResolver cacheResolver() {
        return new CustomCacheResolver(cacheManager());
    }
}

作为最后一步,您应该在@Cacheable、@CachePut、@CacheConfig 等注释之一中指定 CustomCacheResolver.

And as the last step, you should specify CustomCacheResolver in one of @Cacheable, @CachePut, @CacheConfig etc. annotations.

@Cacheable(cacheResolver="cacheResolver")

您可以在此处查看代码示例

这篇关于带有自定义 cacheResolver 的 spring 缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 18:14