日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

Redis?key-value亂碼的解決_Redis

作者:JavaEE_202008 ? 更新時(shí)間: 2022-07-29 編程語(yǔ)言

redis 配置類(lèi)

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
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.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@SpringBootConfiguration
public class RedisConfig extends CachingConfigurerSupport {

? ? /**
? ? ?* 注入 RedisConnectionFactory
? ? ?*/
? ? @Autowired
? ? private RedisConnectionFactory redisConnectionFactory;

? ? @Bean
? ? public CacheManager cacheManager(RedisConnectionFactory factory) {
? ? ? ? return RedisCacheManager.builder(factory).build();
? ? }

? ? @Bean
? ? public RedisTemplate<String, Object> functionDomainRedisTemplate() {
? ? ? ? RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
? ? ? ? redisTemplate.setConnectionFactory(redisConnectionFactory);

? ? ? ? // 使用Jackson2JsonRedisSerialize 替換默認(rèn)序列化
? ? ? ? Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);

? ? ? ? ObjectMapper objectMapper = new ObjectMapper();
? ? ? ? objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
? ? ? ? objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL);
? ? ? ? jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

? ? ? ? // 設(shè)置value的序列化規(guī)則和 key的序列化規(guī)則
? ? ? ? StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
? ? ? ? redisTemplate.setKeySerializer(stringRedisSerializer);
? ? ? ? redisTemplate.setHashKeySerializer(stringRedisSerializer);
? ? ? ? redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
? ? ? ? redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
? ? ? ? redisTemplate.afterPropertiesSet();
? ? ? ? return redisTemplate;
? ? }

}

當(dāng)使用opsForValue() 存取String類(lèi)型key,value情形

    @Autowired
    private StringRedisTemplate redisTemplate;

當(dāng)使用opsForValue() 存取String類(lèi)型key,自定義對(duì)象value情形

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

當(dāng)使用hash結(jié)構(gòu)時(shí)

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
BoundHashOperations<String, Object, Object> ops = redisTemplate.boundHashOps("key1");
        ops.put("key2",obj);

原文鏈接:https://blog.csdn.net/weixin_51681634/article/details/125078072

欄目分類(lèi)
最近更新