All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lodsve.cache.redis.RedisCacheConfiguration Maven / Gradle / Ivy

/*
 * Copyright (C) 2018  Sun.Hao
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */

package lodsve.cache.redis;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import lodsve.cache.properties.CacheProperties;
import lodsve.core.properties.relaxedbind.annotations.EnableConfigurationProperties;
import lodsve.core.utils.StringUtils;
import lodsve.redis.core.annotations.EnableRedis;
import lodsve.redis.core.connection.LodsveRedisConnectionFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

import java.util.Arrays;
import java.util.List;

/**
 * Redis.
 *
 * @author sunhao([email protected])
 * @date 2017/12/23 上午12:23
 */
@Configuration
@EnableCaching
@EnableConfigurationProperties(CacheProperties.class)
public class RedisCacheConfiguration {

    @Configuration
    @Import(RedisCacheBasicConfiguration.class)
    public static class RedisCacheManagerConfiguration {
        private final CacheProperties cacheProperties;
        private final RedisTemplate redisTemplate;

        @Autowired
        public RedisCacheManagerConfiguration(ObjectProvider cacheProperties,
                                              @Qualifier("redisCacheRedisTemplate") ObjectProvider> redisTemplate) {
            this.cacheProperties = cacheProperties.getIfAvailable();
            this.redisTemplate = redisTemplate.getIfAvailable();
        }

        @Bean
        public CacheManager cacheManager() {
            RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
            String cacheNames = cacheProperties.getRedis().getCacheNames();
            List cacheNameList = Arrays.asList(StringUtils.split(cacheNames));

            if (!cacheNameList.isEmpty()) {
                cacheManager.setCacheNames(cacheNameList);
            }

            return cacheManager;
        }
    }

    @Configuration
    @EnableRedis(name = "cache")
    public static class RedisCacheBasicConfiguration {
        private final LodsveRedisConnectionFactory connectionFactory;

        public RedisCacheBasicConfiguration(@Qualifier("cache") ObjectProvider connectionFactory) {
            this.connectionFactory = connectionFactory.getIfAvailable();
        }

        @Bean
        public RedisTemplate redisCacheRedisTemplate() {
            RedisTemplate template = new RedisTemplate<>();

            template.setConnectionFactory(connectionFactory);

            Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
            ObjectMapper om = new ObjectMapper();
            om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
            om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
            jackson2JsonRedisSerializer.setObjectMapper(om);
            template.setKeySerializer(jackson2JsonRedisSerializer);
            template.setValueSerializer(jackson2JsonRedisSerializer);

            return template;
        }
    }
}