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

com.biz.redis.config.BizRedisConfig Maven / Gradle / Ivy

There is a newer version: 1.5.2
Show newest version
package com.biz.redis.config;

import com.biz.redis.utils.*;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * Redis 配置
 *
 * @author francis
 * @since 2024-04-02 08:28
 **/
@Configuration
public class BizRedisConfig {


    @Bean(name = "redisTemplate")
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);

        // String的序列化
        StringRedisSerializer stringSerializer = new StringRedisSerializer();
        // key采用String的序列化方式
        template.setKeySerializer(stringSerializer);
        // hash的key采用String的序列化方式
        template.setHashKeySerializer(stringSerializer);

        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = jackson2JsonRedisSerializerConfig();
        // value采用jackson序列化方式
        template.setValueSerializer(jackson2JsonRedisSerializer);
        // hash的value也采用jackson序列化方式
        template.setHashValueSerializer(jackson2JsonRedisSerializer);

        template.afterPropertiesSet();


        return template;
    }

    @Bean
    public LettuceConnectionFactory redisConnectionFactory() {
        return new LettuceConnectionFactory();
    }


    private Jackson2JsonRedisSerializer jackson2JsonRedisSerializerConfig() {
        // jackson序列化所有的类
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        // jackson序列化的一些配置
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        return jackson2JsonRedisSerializer;
    }

    @Bean
    public ListOperations listOperations(RedisTemplate redisTemplate) {
        return redisTemplate.opsForList();
    }

    @Bean
    public HashOperations hashOperations(RedisTemplate redisTemplate) {
        return redisTemplate.opsForHash();
    }

    @Bean
    public ValueOperations valueOperations(RedisTemplate redisTemplate) {
        return redisTemplate.opsForValue();
    }

    @Bean
    public SetOperations setOperations(RedisTemplate redisTemplate) {
        return redisTemplate.opsForSet();
    }

    @Bean
    public ZSetOperations zSetOperations(RedisTemplate redisTemplate) {
        return redisTemplate.opsForZSet();
    }

    @Bean
    public GeoOperations geoOperations(RedisTemplate redisTemplate) {
        return redisTemplate.opsForGeo();
    }

    @Bean
    public RedisCommonUtils redisCommonUtils(RedisTemplate redisTemplate) {
        return new RedisCommonUtils(redisTemplate);
    }

    @Bean
    public RedisListUtils redisListUtils(ListOperations listOperations, RedisCommonUtils redisCommonUtils) {
        return new RedisListUtils(listOperations, redisCommonUtils);
    }

    @Bean
    public RedisMapUtils redisMapUtils(HashOperations hashOperations, RedisCommonUtils redisCommonUtils) {
        return new RedisMapUtils(hashOperations, redisCommonUtils);
    }

    @Bean
    public RedisStringUtils redisStringUtils(ValueOperations valueOperations, RedisCommonUtils redisCommonUtils) {
        return new RedisStringUtils(valueOperations, redisCommonUtils);
    }

    @Bean
    public RedisSetUtils redisSetUtils(SetOperations setOperations, RedisCommonUtils redisCommonUtils) {
        return new RedisSetUtils(setOperations, redisCommonUtils);
    }

    @Bean
    public RedisZSetUtils redisZSetUtils(ZSetOperations zSetOperations, RedisCommonUtils redisCommonUtils) {
        return new RedisZSetUtils(zSetOperations, redisCommonUtils);
    }

    @Bean
    public RedisGeoUtils redisGeoUtils(GeoOperations geoOperations, RedisCommonUtils redisCommonUtils) {
        return new RedisGeoUtils(geoOperations, redisCommonUtils);
    }

}