使用springboot-starter整合reids

本文详细介绍了如何在Spring Boot项目中整合Redis,包括引入依赖、配置文件设置、编写工具类等关键步骤。同时,探讨了在使用RedisTemplate和StringRedisTemplate进行数据操作时可能遇到的序列化问题。

1.引入相关jar

springboot整合redis相关依赖引入
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
            </dependency>

2.相关配置文件配置

redis:
  database: 0
  # redis 地址这是云的
  host: xxx.xxx.xx.xx
  #连接端口号
  port: 6379
  #连接超时时间
  timeout: 5000
  jedis:
    pool:
      max-idle: 10 #最大连接数
      min-idle: 10 #最小连接数
      max-active: 200 #实例数
      max-wait: 36000 #最大可用连接超时时间

3.写redis 的工具类

@Component
public class RedisBase{

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    public static synchronized Boolean connect(){
        return true;
    }

    /*
    * 检查Key是否存在
    * */
    public Boolean checkKey(String key){
        boolean check = stringRedisTemplate.hasKey(key);
        return check;
    }

    /*
    * 存入string
    * */
    public boolean setString(String key ,String value){
        try{
            stringRedisTemplate.opsForValue().set(key,value);
        }catch (Exception e){
            e.printStackTrace();
        }finally {

        }
        return true;
    }

    /*
    * 存入list
    * */
    public <T> boolean setList(String key , List<T> list){
        try{
            redisTemplate.opsForList().rightPush(key,list);
        }catch (Exception e){
            e.printStackTrace();
        }finally {

        }
        return true;
    }
    /*
    * 存入map
    * */
    public boolean setmap(String key , Map<Object,Object> map){
        try{
            redisTemplate.opsForHash().putAll(key,map);
        }catch (Exception e){
            e.printStackTrace();
        }finally {

        }
        return true;
    }

    /*
    * 根据key取值
    * */
    public Object get(String key){
        String value =  stringRedisTemplate.opsForValue().get(key).toString();
        return  value;
    }

    /*
    * 根据key删除缓存
    * */
    private boolean delete(String key){
       boolean delete =  stringRedisTemplate.delete(key);
       return  delete;
    }

这里我引入了两个redis 的处理类

其实这StringRedisTemplate  是继承 RedisTemplate 的 只是StringRedisTemplate 只处理String 类型的 所以是混合使用的

 

这里其实还有一点点的问题我使用RedisTemplate 存的list  和  has 的被序列化无法识别了 ,这涉及到序列化的格式了

JdkSerializationRedisSerializer 这种的方式 ,具体后面研究好了再来写。

 

spring-boot-starter-data-redisSpring Boot提供的一个Starter,用于简化Spring Data Redis的配置和使用Spring Data RedisSpring提供的数据访问框架,提供了高级的Redis数据操作API,支持对Redis各种数据类型的操作以及事务、Pub/Sub消息等高级特性 [^1]。 ### 使用指南 在Spring Boot应用中使用spring-boot-starter-data-redis可以很方便地集成RedisSpring Boot会自动在容器中生成一个RedisTemplate和一个StringRedisTemplate。RedisTemplate的泛型是<Object,Object>,且未设置数据存于Redis时key及value的序列化方式。这两个Bean使用@ConditionalOnMissingBean注解,若Spring容器中已有RedisTemplate对象,自动配置的RedisTemplate不会实例化,因此可自行编写配置类来配置RedisTemplate [^4]。 以下是一个简单的使用示例: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; @Service public class RedisService { @Autowired private RedisTemplate<String, String> redisTemplate; public void setValue(String key, String value) { redisTemplate.opsForValue().set(key, value); } public String getValue(String key) { return redisTemplate.opsForValue().get(key); } } ``` ### 配置方法 首先,在`pom.xml`中添加依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> ``` 然后,在`application.properties`或`application.yml`中进行基本配置: ```properties spring.redis.host=localhost spring.redis.port=6379 spring.redis.password=yourpassword ``` 若要自定义RedisTemplate的序列化方式,可创建配置类: ```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> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); template.setKeySerializer(new StringRedisSerializer()); template.setValueSerializer(new StringRedisSerializer()); return template; } } ``` ### 常见问题解决方案 - **序列化问题**:RedisTemplate默认未设置key和value的序列化方式,可能导致存储和读取数据时出现问题。可以通过自定义RedisTemplate的序列化方式来解决,如上述配置类中的示例 [^4]。 - **性能问题**:合理配置序列化与连接池参数,结合事务、管道等高级特性,可显著提升应用性能。对于大规模生产环境,建议进一步整合Redis集群与监控工具以实现高可用与稳定性 [^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值