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

com.spring.boxes.redis.BoxesRedisConfiguration Maven / Gradle / Ivy

The newest version!
package com.spring.boxes.redis;

import com.spring.boxes.dollar.support.rule.RuleExecutor;
import com.spring.boxes.dollar.support.rule.key.FrequencyTemplate;
import com.spring.boxes.dollar.support.rule.key.GrayScaleTemplate;
import com.spring.boxes.redis.lock.LockTemplate;
import com.spring.boxes.redis.lock.redisson.RedissonLockTemplate;
import com.spring.boxes.redis.lock.redisson.RedissonProperties;
import com.spring.boxes.redis.queue.RedisQueueTemplate;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
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.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.annotation.Resource;

@Configuration
@AutoConfigureAfter({RedisAutoConfiguration.class})
@ConditionalOnClass({RedisOperations.class, Config.class})
@EnableConfigurationProperties(RedissonProperties.class)
public class BoxesRedisConfiguration {

    @Resource
    private ApplicationContext applicationContext;

    @Resource
    private RedisConnectionFactory redisConnectionFactory;

    @Bean(name = {"redisKeySerializer"})
    @ConditionalOnMissingBean(name = {"redisKeySerializer"})
    public RedisSerializer redisKeySerializer() {
        return StringRedisSerializer.UTF_8;
    }

    @Bean(name = {"redisValueSerializer"})
    @ConditionalOnMissingBean(name = {"redisValueSerializer"})
    public RedisSerializer redisValueSerializer() {
        return new GenericJackson2JsonRedisSerializer();
    }

    @Bean(name = {"redisHashKeySerializer"})
    @ConditionalOnMissingBean(name = {"redisHashKeySerializer"})
    public RedisSerializer redisHashKeySerializer() {
        return StringRedisSerializer.UTF_8;
    }

    @Bean(name = {"redisHashValueSerializer"})
    @ConditionalOnMissingBean(name = {"redisHashValueSerializer"})
    public RedisSerializer redisHashValueSerializer() {
        return new GenericJackson2JsonRedisSerializer();
    }

    @Bean(name = {"redisTemplate"})
    @ConditionalOnMissingBean
    public RedisTemplate redisTemplate() {
        RedisTemplate template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        template.setKeySerializer(this.redisKeySerializer());
        template.setValueSerializer(this.redisValueSerializer());
        template.setHashKeySerializer(this.redisHashKeySerializer());
        template.setHashValueSerializer(this.redisHashValueSerializer());
        return template;
    }

    @Bean
    @ConditionalOnMissingBean
    public StringRedisTemplate stringRedisTemplate() {
        StringRedisTemplate template = new StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        template.setKeySerializer(this.redisKeySerializer());
        return template;
    }

    @Bean
    @ConditionalOnMissingBean
    public LockTemplate redissonLockTemplate(RedissonClient redissonClient) {
        return new RedissonLockTemplate(redissonClient);
    }

    @Bean
    @ConditionalOnMissingBean
    public RedisQueueTemplate redisQueueTemplate(RedissonClient redissonClient) {
        return new RedisQueueTemplate(redissonClient);
    }

    @ConditionalOnMissingBean
    @Bean(destroyMethod = "shutdown")
    public RedissonClient redissonSingle(RedissonProperties redissonProperties) {
        Config config = new Config();
        config.useSingleServer()
                .setAddress(redissonProperties.getAddress())
                .setTimeout(redissonProperties.getTimeout())
                .setConnectionPoolSize(redissonProperties.getConnectionPoolSize())
                .setConnectionMinimumIdleSize(redissonProperties.getConnectionMinimumIdleSize())
                .setPassword(redissonProperties.getPassword());
        return Redisson.create(config);
    }

    @Bean
    @ConditionalOnMissingBean
    public GrayScaleTemplate grayScaleTemplate(){
        return new GrayScaleTemplate();
    }

    @Bean
    @ConditionalOnMissingBean
    public FrequencyTemplate frequencyTemplate(){
        return new FrequencyTemplate(stringRedisTemplate());
    }

    @Bean
    @ConditionalOnMissingBean
    public RuleExecutor ruleExecutor() {
        return new RuleExecutor(applicationContext);
    }
}