Redis?key-value亂碼的解決
redis 配置類
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類型key,value情形
@Autowired private StringRedisTemplate redisTemplate;
當(dāng)使用opsForValue() 存取String類型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實(shí)現(xiàn)每周熱評(píng)的項(xiàng)目實(shí)踐
實(shí)時(shí)統(tǒng)計(jì)和展示熱門內(nèi)容是一種常見的需求,本文主要介紹了Redis實(shí)現(xiàn)每周熱評(píng)的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-03-03Go語言操作RediSearch進(jìn)行搜索方法示例詳解
這篇文章主要為大家介紹了Go語言操作RediSearch進(jìn)行搜索方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12Redis實(shí)戰(zhàn)之Redis實(shí)現(xiàn)異步秒殺優(yōu)化詳解
這篇文章主要給大家介紹了Redis實(shí)戰(zhàn)之Redis實(shí)現(xiàn)異步秒殺優(yōu)化方法,文章通過圖片和代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,感興趣的同學(xué)可以自己動(dòng)手試一下2023-09-09Redis Cluster Pipeline導(dǎo)致的死鎖問題解決
本文主要介紹了Redis Cluster Pipeline導(dǎo)致的死鎖問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-10-10Redis02 使用Redis數(shù)據(jù)庫(kù)(String類型)全面解析
這篇文章主要介紹了Redis02 使用Redis數(shù)據(jù)庫(kù)(String類型)全面解析的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07