Redis?key-value亂碼的解決
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);
到此這篇關(guān)于Redis key-value亂碼的解決的文章就介紹到這了,更多相關(guān)Redis key-value亂碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Redis如何統(tǒng)計(jì)用戶(hù)訪(fǎng)問(wèn)量
這篇文章主要介紹了Redis如何統(tǒng)計(jì)用戶(hù)訪(fǎng)問(wèn)量問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Redis超詳細(xì)講解高可用主從復(fù)制基礎(chǔ)與哨兵模式方案
Redis因?yàn)槠涓咝阅芎鸵子眯栽谖覀兒蠖说姆?wù)中發(fā)揮了巨大的作用,并且很多重要功能的實(shí)現(xiàn)都會(huì)依賴(lài)redis,本篇我們來(lái)了解Redis高可用主從復(fù)制與哨兵模式2022-04-04
Redis 實(shí)現(xiàn)隊(duì)列原理的實(shí)例詳解
這篇文章主要介紹了Redis 實(shí)現(xiàn)隊(duì)列原理的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下2017-09-09

