【Springboot笔记】Springboot集成redis

2022-08-23 13:35:29

关于springboot集成redis,我做了笔记,分享给有需要的小伙伴们,视频看的动力节点王鹤老师讲的,

动力节点王鹤老师讲解的springboot教程,由浅入深,带你体验Spring Boot的极速开发过程,内容丰富,涵盖了SpringBoot开发的方方面面,并且同步更新到Spring Boot 2.x系列的最新版本。

视频链接:动力节点springboot视频教程-初学springboot必备教程-springboot最新完整版_哔哩哔哩_bilibili你的三连就是录制视频的动力!一定不要忘记收藏、点赞、投币哦~本套视频基于SpringBoot2.4版本讲解。教程从细节入手,每个事例先讲解pom.xml中的重要依赖,其次application配置文件,最后是代码实现。让你知其所以,逐步让掌握SpringBoot框架的自动配置,starter起步依赖等特性。 为什么SpringBoot是创建Spring应用,必须了解spring-boothttps://www.bilibili.com/video/BV1XQ4y1m7ex

redis能帮我们分散掉数据库的压力,有了它能更好的支持并发性能!

可以这样理解redis位于数据库和springboot框架之间,起到数据缓存的作用。

在idea当中已经集成了redis的插件配置

创建完成后会得到idea的驱动以及工具接口。

之后就要在本地启动redis服务,这个过程就好像类似启动mysql服务。

我们可以到redis的官网下载,根据自己的系统选择x64or x32

windows

https://github.com/tporadowski/redis/releases

Linux

https://redis.io/download

之后在本地需要开启redis服务

使用Java进行链接,向redis当中缓存数据

配置 - -application.yml

spring:
  redis:
    host: 127.0.0.1
    port: 6379
    jedis:
      pool:
        max-active: 8
        max-wait: -1ms
        max-idle: 500
        min-idle: 0
    lettuce:
      shutdown-timeout: 0ms

测试类

package com.example.demo;

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.RedisTemplate;

@SpringBootTest
class DemoApplicationTests {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Test
    void contextLoads() {
        redisTemplate.opsForValue().set("myKey3", "4564");
        System.out.println(redisTemplate.opsForValue().get("myKey3"));
    }
}

由于框架已经添加了redis 所以只需要将 redisTemplate 注入到Bean当中就可以调用接口对redis进行数据缓存。

当页面查询数据时首先去缓存当中查找数据, 如果没有数据再向数据库请求资源,因为redis的存储类型,存取速度很快,能在一定程度上减缓数据库的压力。提升并发性能,加固网站的稳定性。

我们可以起线程池对接口进行并发测试,查看是否符合逻辑,必要的加上锁。

  • 作者:老杜铁杆粉丝儿
  • 原文链接:https://blog.csdn.net/A_coding/article/details/123259605
    更新时间:2022-08-23 13:35:29