springboot使用redis缓存乱码(key或者 value 乱码)一招解决

如果查看redis中的值是这样

创建一个配置类就可以解决

cpp 复制代码
package com.deka.config;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.scripting.support.ResourceScriptSource;


@Configuration
public class RedisConfig {

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;

    @Autowired
    private RedisTemplate redisTemplate;

    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory factory) {
        redisTemplate.setConnectionFactory(factory);
        Jackson2JsonRedisSerializer<Object> serializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
        redisTemplate.setDefaultSerializer(serializer);
        //设置序列化Key的实例化对象
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //设置序列化Value的实例化对象
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        return redisTemplate;
    }

    @Bean
    public RedisMessageListenerContainer redisContainer() {
        final RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(redisConnectionFactory);
        return container;
    }

    @Bean
    public RedisScript<Long> limitRedisScript() {
        DefaultRedisScript<Long> redisScript = new DefaultRedisScript<>();
        redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource("scripts/limit.lua")));
        redisScript.setResultType(Long.class);
        return redisScript;
    }

}
相关推荐
凌寒117 小时前
Linux(Debain)安装Redis、数据迁移
linux·运维·服务器·redis
⑩-7 小时前
基于Redis Lua脚本的秒杀系统
java·redis
踏浪无痕8 小时前
PageHelper 防坑指南:从兜底方案到根治方案
spring boot·后端
三翼鸟数字化技术团队8 小时前
基于redis的多资源分布式公平锁的设计与实践
redis·后端
通往曙光的路上8 小时前
焚决糟糕篇
java·spring boot·tomcat
6***v4178 小时前
spring boot 项目打印sql日志和结果,使用logback或配置文件
spring boot·sql·logback
h***34639 小时前
Redis安装教程(Windows版本)
数据库·windows·redis
3***g2059 小时前
如何使用Spring Boot框架整合Redis:超详细案例教程
spring boot·redis·后端
大猫子的技术日记9 小时前
[百题重刷]前缀和 + Hash 表:缓存思想, 消除重复计算
java·缓存·哈希算法
s***35309 小时前
Spring Boot3.x集成Flowable7.x(一)Spring Boot集成与设计、部署、发起、完成简单流程
java·spring boot·后端