SpringBoot整合Redis之编写RedisConfig

2022-11-13 10:11:22 整合 编写 springboot

编写RedisConfig首先我们要明白RedisConfig中需要包含什么,首先看看我们直接使用RedisTemplate的问题,我们就知道RedisConfig要包含什么了,我们在RedisConfig需要规定好根据不同的查询生成的key,key和value的序列化和反序列化

我们在RedisConfig中配置的自定义方法,最终通过注解引用的就是我们自定义的方法

RedisConfig.java   :     

对redis提供的两个Template的序列化配置


import com.fasterxml.jackson.annotation.JSONAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
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.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.Jackson2jsonRedisSerializer;
 
@Configuration
@EnableCaching
public class RedisConfig {
           @Bean
        public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
            RedisCacheConfiguration cacheConfiguration =
                    defaultCacheConfig()
                            .disableCachingNullValues()
                            .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new Jackson2JsonRedisSerializer(Object.class)));
            return RedisCacheManager.builder(connectionFactory).cacheDefaults(cacheConfiguration).build();
 
//            RedisCacheManager rcm=RedisCacheManager.create(connectionFactory);
//
//            return rcm;
 
        }
 
 
        @Bean
        public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
            RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
            redisTemplate.setConnectionFactory(factory);
            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);
            //序列化设置 ,这样为了存储操作对象时正常显示的数据,也能正常存储和获取
            redisTemplate.seTKEySerializer(new StringRedisSerializer());
            redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
            redisTemplate.setHashKeySerializer(new StringRedisSerializer());
            redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
            return redisTemplate;
        }
    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(factory);
        return stringRedisTemplate;
    }
 
 
 
}

补充

redisconfig配置与使用

配置类:

@Configuration   // 定义一个配置类
public class RedisConfig {
    //object object

    
    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> template = new RedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        // 使用JSON格式序列化对象,对缓存数据key和value进行转换
        Jackson2JsonRedisSerializer jacksonSeial = new Jackson2JsonRedisSerializer(Object.class);
        // 解决查询缓存转换异常的问题
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jacksonSeial.setObjectMapper(om);

        // 设置RedisTemplate模板api的序列化方式为JSON
        template.setDefaultSerializer(jacksonSeial);
        return template;
    }
 }

使用方式:

1. 业务类中进行注入:

@Autowired
RedisTemplate redisTemplate;

2. 使用redisTemplate的内置方法:

2.1 获取redis中的数据

Object object = redisTemplate.opsForValue().get("course_" + id);

2.2 插入数据

redisTemplate.opsForValue().set("course_" + id, course, 1, TimeUnit.DAYS);

到此这篇关于SpringBoot整合Redis之编写RedisConfig的文章就介绍到这了,更多相关SpringBoot编写RedisConfig内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关文章