本文介绍了如何在 spring-data 2.0.x 中创建 RedisCacheManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将我的应用程序从 spring boot 1.5.x 迁移到 2.0.x.我想保留 jedis,但我对 RedisCacheManager 的实例化有问题.

I'm migrating my application from spring boot 1.5.x to 2.0.x. I want to keep jedis but I have a problem with the instantiation of RedisCacheManager.

现在构造函数签名是

RedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration)

但在此之前:

RedisCacheManager(RedisOperations redisOperations)

我定义这个 bean 的作用域只有 RedisTemplate:

I define this bean having only RedisTemplate in scope:

@Bean
public RedisCacheManager redisCacheManager(RedisTemplate redisTemplate) {
    HandleRedisCacheManager redisCacheManager = new HandleRedisCacheManager(redisTemplate);
    redisCacheManager.setUsePrefix(true);
    return redisCacheManager;
}

现在应该如何创建?

推荐答案

它不再接受 RedisTemplate.所以试试这个:

It doesn't accept a RedisTemplate anymore. So try this:

@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory,
                                      ResourceLoader resourceLoader) {
    RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager
            .builder(redisConnectionFactory)
            .cacheDefaults(determineConfiguration(resourceLoader.getClassLoader()));
    List<String> cacheNames = this.cacheProperties.getCacheNames();
    if (!cacheNames.isEmpty()) {
        builder.initialCacheNames(new LinkedHashSet<>(cacheNames));
    }
    return builder.build();
}

private org.springframework.data.redis.cache.RedisCacheConfiguration determineConfiguration(
        ClassLoader classLoader) {
    if (this.redisCacheConfiguration != null) {
        return this.redisCacheConfiguration;
    }
    CacheProperties.Redis redisProperties = this.cacheProperties.getRedis();
    RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();

    ObjectMapper mapper = new Jackson2ObjectMapperBuilder()
            .modulesToInstall( new SimpleModule().addSerializer( new NullValueSerializer(null)) )
            .failOnEmptyBeans( false )
            .build();
    mapper.enableDefaultTyping( ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);

    GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer( mapper );

    //get the mapper b/c they registered some internal modules
    config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(serializer));;

    if (redisProperties.getTimeToLive() != null) {
        config = config.entryTtl(redisProperties.getTimeToLive());
    }
    if (redisProperties.getKeyPrefix() != null) {
        config = config.prefixKeysWith(redisProperties.getKeyPrefix());
        config = config.computePrefixWith(cacheName -> redisProperties.getKeyPrefix() + cacheName + "::");
    }
    if (!redisProperties.isCacheNullValues()) {
        config = config.disableCachingNullValues();
    }
    if (!redisProperties.isUseKeyPrefix()) {
        config = config.disableKeyPrefix();
    }
    return config;
}

这篇关于如何在 spring-data 2.0.x 中创建 RedisCacheManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-28 11:09