当前位置: 首页 > news >正文

Spring Boot 3.X 下Redis缓存的尝试(一):初步尝试

背景

        想像一下有这么一个场景,一个系统有超多角色、角色下有多个菜单、菜单下有多个按钮权限,这种子父级关系查询每次向数据库查询相当耗时,那么我们是否可以将这种更新频次不高,而查询耗时的数据且不直接影响业务的数据放进缓存中去,相关查询直接去缓存里去查,减少对数据库的压力,那么Redis是一个不错的选择。

        当然Spring Boot 不采用Redis也可以实现缓存,但是作为开发要想项目中的解耦性,所以针对Redis做个尝试教程供大家讨论。

准备

        安装Redis,看我之前的教程《Ubuntu 系统、Docker配置、Docker的常用软件配置(下)》

基本测试

1.创建Spring Boot 项目

        pom 文件

        

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.5.0</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>demo</name><description>demo</description><url/><licenses><license/></licenses><developers><developer/></developers><scm><connection/><developerConnection/><tag/><url/></scm><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><annotationProcessorPaths><path><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></path></annotationProcessorPaths></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build></project>

        项目配置文件

        

spring:data:redis:host: 127.0.0.1port: 6379password: 24568096@qq.comconnect-timeout: 5stimeout: 5smvc:pathmatch:matching-strategy: ant_path_matcher
server:port: 9999

2.Redis配置类

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.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(factory);// 设置键(key)的序列化器为 StringRedisSerializer,确保键以直观的字符串形式存储template.setKeySerializer(new StringRedisSerializer());// 设置值(value)的序列化器为 StringRedisSerializer,确保值也以字符串形式存储template.setValueSerializer(new StringRedisSerializer());// 初始化模板配置template.afterPropertiesSet();return template;}
}

3.测试


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;
import org.springframework.data.redis.core.types.RedisClientInfo;import java.util.Collections;
import java.util.List;@SpringBootTest
class DemoApplicationTests {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;@Autowiredprivate RedisService redisService;@Testvoid contextLoads() {redisTemplate.opsForValue().set("name", "张三");String  name = (String) redisTemplate.opsForValue().get("name");System.out.println(name);//----------写入ListredisTemplate.opsForList().leftPush("list", "王王");redisTemplate.opsForList().leftPush("list", "李四");redisTemplate.opsForList().leftPush("list", "赵六");//  获取listList<Object> list = redisTemplate.opsForList().range("list", 0, -1);System.out.println(list);//-----------写入集合redisTemplate.opsForSet().add("set", "张三");redisTemplate.opsForSet().add("set", "李四");redisTemplate.opsForSet().add("set", "王王");redisTemplate.opsForSet().add("set", "赵六");//-----------写入集合List<Object> set = Collections.singletonList(redisTemplate.opsForSet().members("set"));System.out.println(set);//-----------写入MapredisTemplate.opsForHash().put("map", "name", "张三");redisTemplate.opsForHash().put("map", "age", "18");//-----------获取MapList<Object> map = Collections.singletonList(redisTemplate.opsForHash().entries("map"));System.out.println(map);}@Testvoid testRedisService() {redisService.getStuById(1);}}

效果

下一篇:《Spring Boot 3.X 下Redis缓存的尝试(二):自动注解实现自动化缓存操作》

http://www.xdnf.cn/news/767791.html

相关文章:

  • CSS 3D 变换中z-index失效问题
  • Ubuntu上进行VS Code的配置
  • 简单工厂模式
  • Spring Boot 3.X 下Redis缓存的尝试(二):自动注解实现自动化缓存操作
  • DeepSeek模型性能优化:从推理加速到资源调度的全栈实践
  • spring-boot接入websocket教程以及常见问题解决
  • 优化WP外贸建站提升用户体验
  • React 核心概念与生态系统
  • React 组件异常捕获机制详解
  • React---day6、7
  • Google机器学习实践指南(TensorFlow六大优化器)
  • 2025GDCPC广东省赛游记(附赛时代码)
  • 【Spring】RAG 知识库基础
  • Docker 镜像制作
  • 【Redis】Zset 有序集合
  • .net consul服务注册与发现
  • 描述性统计——让数据说话
  • Figma 与 Cursor 深度集成的完整解决方案
  • 【数据分析】第四章 pandas简介(1)
  • Caliper压力测试
  • mac安装brew时macos无法信任ruby的解决方法
  • 智慧零工平台后端开发实战:Spring Boot 3 + MyBatis-Flex 现代化架构
  • Linux中的mysql备份与恢复
  • Unity + HybirdCLR热更新 入门篇
  • GStreamer开发笔记(六):gstreamer基本概念:组件、箱柜、管道、衬垫、链接组件
  • 【端午安康】龙舟争渡Plug-In
  • 计算机组成原理核心剖析:CPU、存储、I/O 与总线系统全解
  • 空调清洗教程
  • Python----目标检测(《YOLOv3:AnIncrementalImprovement》和YOLO-V3的原理与网络结构)
  • 20250602在Ubuntu20.04.6下修改压缩包的日期和时间