SpringBoot中RedisConfig配置类


前言

本文主要记录SpringBoot中集成的Redis的配置类


一、引入起步依赖

代码示例

        <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

二、RedisConfig配置类

1.RedisConfig

代码如下(示例):

package com.lp.framework.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author LuoPing
 * @project IntelliJ IDEA
 * 
 */
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        FastJson2JsonRedisSerializer<Object> json2JsonRedisSerializer = new FastJson2JsonRedisSerializer<>(Object.class);
        // 使用StringRedisSerializer来序列化和反序列化redis的key值
        StringRedisSerializer serializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(serializer);
        redisTemplate.setValueSerializer(json2JsonRedisSerializer);
        //hash的value也采用jackson序列化方式
        redisTemplate.setHashKeySerializer(serializer);
        redisTemplate.setHashValueSerializer(json2JsonRedisSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

2.FastJson2JsonRedisSerializer

代码如下(示例):

package com.lp.framework.config;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

import java.nio.charset.Charset;

/**
 * Redis使用FastJson序列化
 * 
 */
public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T>
{
    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

    private Class<T> clazz;

    public FastJson2JsonRedisSerializer(Class<T> clazz)
    {
        super();
        this.clazz = clazz;
    }

    @Override
    public byte[] serialize(T t) throws SerializationException
    {
        if (t == null)
        {
            return new byte[0];
        }
        return JSON.toJSONString(t, JSONWriter.Feature.WriteClassName).getBytes(DEFAULT_CHARSET);
    }

    @Override
    public T deserialize(byte[] bytes) throws SerializationException
    {
        if (bytes == null || bytes.length <= 0)
        {
            return null;
        }
        String str = new String(bytes, DEFAULT_CHARSET);

        return JSON.parseObject(str, clazz, JSONReader.Feature.SupportAutoType);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot 配置 Redis配置需要配置以下内容: 1. 引入 Redis 相关的依赖:在 `pom.xml` 文件添加 Redis 相关的依赖,例如 `spring-boot-starter-data-redis`。 2. 创建 Redis 连接工厂的 Bean:在配置创建 `RedisConnectionFactory` 型的 Bean,用于创建 Redis 连接工厂。可以使用 `LettuceConnectionFactory` 或 `JedisConnectionFactory`,具体选择取决于你使用的 Redis 客户端。 3. 创建 RedisTemplate 的 Bean:在配置创建 `RedisTemplate` 型的 Bean,用于操作 Redis 数据。可以使用 `StringRedisTemplate` 或自定义的 `RedisTemplate`,具体选择取决于你的需求。 4. 配置 Redis 缓存管理器(可选):如果你想使用 Spring Cache 来缓存数据到 Redis ,可以配置一个 `CacheManager` 的 Bean,并将其设置为 Redis 的缓存管理器。 以下是一个简单的 Redis 配置示例: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new StringRedisSerializer()); return redisTemplate; } // 配置其他 Redis 相关的 Bean // 配置 Redis 缓存管理器(可选) } ``` 在上述示例,我们创建了一个 `RedisConfig` 配置,并在其定义了一个名为 `redisTemplate` 的 `RedisTemplate` Bean。我们设置了 Redis 连接工厂、键值序列化器等相关配置。 根据你的需求,你可以继续配置其他的 Redis 相关的 Bean,例如 `RedisConnectionFactory`、`CacheManager` 等。 请确保将该配置正确地放置在 Spring Boot 应用程序的扫描路径,以便自动加载该配置

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十一*

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值