首頁 > 軟體

SpringBoot整合Redis之編寫RedisConfig

2022-06-23 18:01:18

編寫RedisConfig首先我們要明白RedisConfig中需要包含什麼,首先看看我們直接使用RedisTemplate的問題,我們就知道RedisConfig要包含什麼了,我們在RedisConfig需要規定好根據不同的查詢生成的key,key和value的序列化和反序列化

我們在RedisConfig中設定的自定義方法,最終通過註解除參照的就是我們自定義的方法

RedisConfig.java   :     

對redis提供的兩個Template的序列化設定

/**
 * @Author: JCccc
 * @CreateTime: 2018-09-11
 * @Description:
 */
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

    /*
    * 用於redisApi
    * */
    @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內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com