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

com.qingzhuge.framework.cache.autoconfigure.J2CacheRedisAutoConfiguration Maven / Gradle / Ivy

There is a newer version: 1.0.1.5
Show newest version
package com.qingzhuge.framework.cache.autoconfigure;

import com.qingzhuge.framework.cache.support.CacheSerializer;
import com.qingzhuge.framework.cache.support.LongRedisSerializer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;

/**
 * @author :zero.xiao
 * datetime :2018/1/14 16:46
 **/
@Slf4j
@Configuration
@ConditionalOnProperty(prefix= J2CacheProperties.CACHE_PREFIX,name = "open",havingValue = "true",matchIfMissing = true)
@Import(RedisAutoConfiguration.class)
public class J2CacheRedisAutoConfiguration {

    @Bean
    public RedisSerializer cacheValueSerializer() {
        return new CacheSerializer();
    }

    @Bean
    public RedisSerializer stringRedisSerializer(){
        return RedisSerializer.string();
    }


    @Bean
    public RedisTemplate serializableRedisTemplate(LettuceConnectionFactory factory) {
        RedisTemplate redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(stringRedisSerializer());
        redisTemplate.setHashKeySerializer(stringRedisSerializer());
        redisTemplate.setValueSerializer(cacheValueSerializer());
        redisTemplate.setHashValueSerializer(cacheValueSerializer());
        redisTemplate.setConnectionFactory(factory);
        return redisTemplate;
    }

    @Bean
    public RedisTemplate objectRedisTemplate(LettuceConnectionFactory factory) {
        RedisTemplate template = new RedisTemplate<>();
        template.setKeySerializer(stringRedisSerializer());
        template.setHashKeySerializer(stringRedisSerializer());
        template.setConnectionFactory(factory);
        return template;
    }

    @Bean
    public RedisTemplate stringLongRedisTemplate(LettuceConnectionFactory factory) {
        RedisTemplate redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new LongRedisSerializer());
        redisTemplate.setConnectionFactory(factory);
        return redisTemplate;
    }
}