服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Redis - Redis key-value乱码的解决

Redis key-value乱码的解决

2022-10-20 16:04JavaEE_202008 Redis

本文主要介绍了Redis key-value乱码的解决,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

redis 配置类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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 替换默认序列化
        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);
 
        // 设置value的序列化规则和 key的序列化规则
        StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(stringRedisSerializer);
        redisTemplate.setHashKeySerializer(stringRedisSerializer);
        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
 
}

当使用opsForValue() 存取String类型key,value情形

?
1
2
@Autowired
private StringRedisTemplate redisTemplate;

当使用opsForValue() 存取String类型key,自定义对象value情形

?
1
2
@Autowired
private RedisTemplate<String, Object> redisTemplate;

当使用hash结构时

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

到此这篇关于Redis key-value乱码的解决的文章就介绍到这了,更多相关Redis key-value乱码内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

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

延伸 · 阅读

精彩推荐
  • Redis关于redis状态监控和性能调优详解

    关于redis状态监控和性能调优详解

    Redis是一种高级key-value数据库。它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富。有字符串,链表、哈希、集合和有序集合5种。下面这...

    天生帅才4272019-11-08
  • RedisRedis 缓存淘汰策略和事务实现乐观锁详情

    Redis 缓存淘汰策略和事务实现乐观锁详情

    这篇文章主要介绍了Redis缓存淘汰策略和事务实现乐观锁详情,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的朋友可以参考一下...

    Miaoshuowen6502022-07-21
  • RedisRedis设置密码保护的实例讲解

    Redis设置密码保护的实例讲解

    今天小编就为大家分享一篇Redis设置密码保护的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 ...

    翔之天空2472019-11-14
  • RedisRedis安装使用RedisJSON模块的方法

    Redis安装使用RedisJSON模块的方法

    在使用Redis中,我们可以使用大量的Redis模块来扩展Redis的功能,本文主要介绍了Redis安装使用RedisJSON模块的方法,具有一定的参考价值,感兴趣的可以了解...

    Charge88492022-10-12
  • Redisredis 替代php文件存储session的实例

    redis 替代php文件存储session的实例

    这篇文章主要介绍了redis 替代php文件存储session的实例的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下 ...

    项象多5572019-11-10
  • Redisredis使用不当导致应用卡死bug的过程解析

    redis使用不当导致应用卡死bug的过程解析

    本文主要记一次找因redis使用不当导致应用卡死bug的过程,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧...

    小木-_-10962021-08-15
  • Redis详解RedisTemplate下Redis分布式锁引发的系列问题

    详解RedisTemplate下Redis分布式锁引发的系列问题

    这篇文章主要介绍了详解RedisTemplate下Redis分布式锁引发的系列问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,...

    Leonis丶L12472021-07-28
  • Redisredis debug环境搭建过程详解(使用clion)

    redis debug环境搭建过程详解(使用clion)

    这篇文章给大家介绍redis debug环境搭建过程,使用clion,达到和调试java一样的效果,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴...

    低级知识传播者7432021-08-19