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

org.zodiac.autoconfigure.redis.RedisTemplateAutoConfiguration Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.autoconfigure.redis;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.zodiac.autoconfigure.redis.condition.ConditionalOnRedisEnabled;
import org.zodiac.autoconfigure.security.jwt.SecurityJwtRedisAutoConfiguration;

/**
 * RedisTemplate 配置。
 *
 */
@EnableCaching
@SpringBootConfiguration
@ConditionalOnRedisEnabled
@AutoConfigureBefore(value = {SecurityJwtRedisAutoConfiguration.class, RedisAutoConfiguration.class})
@ConditionalOnClass(value = {org.zodiac.redis.RedisConfigInfo.class, org.springframework.data.redis.core.RedisTemplate.class})
public class RedisTemplateAutoConfiguration implements RedisSerializerConfigable {

    public RedisTemplateAutoConfiguration() {
        super();
    }

    /**
     * value 值 序列化。
     *
     * @return RedisSerializer
     */
    @Override
    @Bean
    @ConditionalOnMissingBean(value = {org.springframework.data.redis.serializer.RedisSerializer.class})
    public org.springframework.data.redis.serializer.RedisSerializer redisSerializer(RedisCacheCloudProperties configProperties) {
        return defaultRedisSerializer(configProperties);
    }

    @Bean(name = "redisTemplate")
    @ConditionalOnMissingBean(name = "redisTemplate")
    protected org.springframework.data.redis.core.RedisTemplate redisTemplate(org.springframework.data.redis.connection.RedisConnectionFactory redisConnectionFactory,
        org.springframework.data.redis.serializer.RedisSerializer redisSerializer) {
        org.springframework.data.redis.core.RedisTemplate redisTemplate = new org.springframework.data.redis.core.RedisTemplate<>();
        /*key 序列化*/
        org.zodiac.redis.serializer.RedisKeySerializer keySerializer = new org.zodiac.redis.serializer.RedisKeySerializer();
        redisTemplate.setKeySerializer(keySerializer);
        redisTemplate.setHashKeySerializer(keySerializer);
        /*value 序列化*/
        redisTemplate.setValueSerializer(redisSerializer);
        redisTemplate.setHashValueSerializer(redisSerializer);
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }

    @Bean
    @ConditionalOnMissingBean(value = {org.springframework.data.redis.core.ValueOperations.class})
    protected org.springframework.data.redis.core.ValueOperations valueOperations(org.springframework.data.redis.core.RedisTemplate redisTemplate) {
        return redisTemplate.opsForValue();
    }

    @Bean
    @ConditionalOnMissingBean
    protected org.zodiac.redis.RedisDataOperation redisDataOperation(org.springframework.data.redis.core.RedisTemplate redisTemplate) {
        return new org.zodiac.redis.RedisDataOperation(redisTemplate);
    }

}