SpringBoot连接redis

川长思鸟来 2024-03-24 22:58 142阅读 0赞

SpringBoot连接redis

简介:本文讲解,如何使用SpringBoot连接redis.
如何下载redis看这篇文章:redis安装教程

项目源文件,在这个地址:https://gitee.com/geek-li-hua/code-in-blog.git

在这里插入图片描述

连接方法

  • 首先启动redis
    在这里插入图片描述
  • 编写配置文件

这里需要修改的就是自己的密码

  1. spring.redis.host=127.0.0.1
  2. spring.redis.port=6379
  3. spring.redis.password=root@123456
  4. spring.redis.jedis.pool.max-active=8
  5. spring.redis.jedis.pool.max-wait=-1
  6. spring.redis.jedis.pool.max-idle=500
  7. spring.redis.jedis.pool.min-idle=0
  8. spring.redis.lettuce.shutdown-timeout=0

在这里插入图片描述

  • 编写测试类进行测试

    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.data.redis.core.StringRedisTemplate;
    @SpringBootTest
    class RedisDemoApplicationTests {

    1. @Autowired
    2. private StringRedisTemplate stringRedisTemplate;
    3. @Test
    4. void contextLoads() {
    5. stringRedisTemplate.opsForValue().set("xiaocai", "888");
    6. String res = stringRedisTemplate.opsForValue().get("xiaocai");
    7. System.out.println(res);
    8. }

    }

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,142人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Springboot连接Redis

    前言 随着数据量的日益增长,很多时候如果直接用SELECT \ FROM XXX这些SQL语句来对上千万的数据进行查询,那耗费的时间可想而知。 作为用户一方,肯定希望系