分布式缓存配置(SpringCache)

Posted 伍妖捌

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分布式缓存配置(SpringCache)相关的知识,希望对你有一定的参考价值。

@Configuration
@EnableCaching
@EnableConfigurationProperties(CacheProperties.class)
public class RedisCacheConfig {
    @Bean
    RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
        config = config.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new
                StringRedisSerializer()));
        config = config.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new
                GenericJackson2JsonRedisSerializer()));
        CacheProperties.Redis redisProperties = cacheProperties.getRedis();
        if (redisProperties.getTimeToLive() != null) {
            config = config.entryTtl(redisProperties.getTimeToLive());
        }
        if (redisProperties.getKeyPrefix() != null) {
            config = config.prefixKeysWith(redisProperties.getKeyPrefix());
        }
        if (!redisProperties.isCacheNullValues()) {
            config = config.disableCachingNullValues();
        }
        if (!redisProperties.isUseKeyPrefix()) {
            config = config.disableKeyPrefix();
        }
        return config;
    }

    @Bean("CacheKey")
    public KeyGenerator keyGenerator() {
        return (target, method, params) -> target.getClass().getSimpleName() + "_" + method.getName()
                + (params.length == 0 ? "" : "_" + StringUtils.arrayToDelimitedString(params, "_"));
    }
}

以上是关于分布式缓存配置(SpringCache)的主要内容,如果未能解决你的问题,请参考以下文章

分布式缓存(SpringCache)

分布式缓存(SpringCache)

springboot集成springcache

SpringCache实现缓存

SpringCache实现缓存

SpringCache实现缓存