【springboot】简易模块化开发项目整合Redis

embedded/2024/10/10 9:04:34/

接上一项目,继续拓展项目

1.整合Redis

添加Redis依赖至fast-demo-config模块的pom.xml文件中

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

在application.yml文件中增加Redis配置项

# 数据库连接配置,记得新建一个数据库
spring:datasource:url: jdbc:mysql://localhost:3306/my_demo?useUnicode=true&characterEncoding=UTF-8driver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: 123456mvc:pathmatch:matching-strategy: ant_path_matcher## 注意是在spring下的,默认密码为空redis:host: 127.0.0.1# Redis服务器连接端口post: 6379jedis:pool:# 连接池最大连接数max-active: 100# 连接池中的最新空闲连接max-idel: 10# 连接池最大阻塞等待时间max-wait: 100000# 连接超时时间timeout: 5000# 默认是使用索引为0的数据库database: 0

新建RedisConfig类,封装Redis的一些方法以供后续使用

@Component
public class RedisConfig {@Autowiredprivate RedisTemplate<String,String> redisTemplate;/*** 获取哈希字段的值* @param key* @return*/public String get(final String key){return redisTemplate.opsForValue().get(key);}/*** 新增一个字符串类型的值* @param key* @param value* @return*/public boolean set(final String key,String value){boolean result = false;try {redisTemplate.opsForValue().set(key,value);result = true;}catch (Exception e){e.printStackTrace();}return result;}/*** 获取原来key键对应的值并重新赋新值* @param key* @param value* @return*/public boolean getAndSet(final String key,String value){boolean result = false;try {redisTemplate.opsForValue().getAndSet(key,value);result = true;}catch (Exception e){e.printStackTrace();}return result;}/*** 判断哈希中是否存在指定的字段* @param key* @return*/public boolean hasKey(final String key){boolean result = false;try {return redisTemplate.hasKey(key);}catch (Exception e){e.printStackTrace();}return result;}/*** 删除指定的字段* @param key* @return*/public boolean delete(final String key){boolean result = false;try {redisTemplate.delete(key);result = true;}catch (Exception e){e.printStackTrace();}return result;}/*** 给指定字段设置有效时间* @param key* @param time  一天等于 1*24*60*60* @return*/public boolean expire(final String key, long time){boolean result = false;try {return redisTemplate.expire(key, time, TimeUnit.SECONDS);}catch (Exception e){e.printStackTrace();}return result;}
}

2.测试使用

启动Redis服务器和客户端

在这里插入图片描述

修改getUserList方法,将原本查询到的数据存入redis缓存中

@RestController
@Api("用户信息接口")
public class UserController {@Autowiredprivate UserService userService;@Autowiredprivate RedisConfig redis;@RequestMapping(value = "/user",method = RequestMethod.GET)@ApiOperation(value = "获取所有用户信息", notes = "返回用户信息")public List<User> getUserList(){List<User> userList = userService.getUserList();redis.set("userList",userList.toString());return userList;}
}

运行后,在redis客户端输入keys *命令后,可见新增userList字段

在这里插入图片描述

内容比较少,就不贴完整代码了


http://www.ppmy.cn/embedded/124629.html

相关文章

Simplygon 使用笔记2

本文主收集一些实质开发中的一些问题, 大部分时候只能通过文档, 从个别的描述中找到问题的解决方法, 而这里希望能提供一些关键字, 方便具体的问题的分析 1. 怎么从spMaterial 材质中找到对应贴图的名字 这个可能通过获取该GetShadingNetwork的方式获取但问题是network可能是很…

AtCoder Beginner Contest 373(ABCDEF 题)视频讲解

A - September Problem Statement There are 12 12 12 strings S 1 , S 2 , … , S 12 S_1, S_2, \ldots, S_{12} S1​,S2​,…,S12​ consisting of lowercase English letters. Find how many integers i i i ( 1 ≤ i ≤ 12 ) (1 \leq i \leq 12) (1≤i≤12) satisfy …

达梦数据库索引内容介绍

1、索引概念 索引是为了快速检索和定位数据行而创建的一种数据结构。索引是由表中索引列数据进行排序后的集合和指向这些值的物理标识&#xff08;例如&#xff1a;ROWID 等聚集索引键&#xff09;共同组成。在 DM 中&#xff0c;除了位图索引、位图连接索引、全文索引和空间索…

Redis:string类型

Redis&#xff1a;string类型 string命令设置与读取SETGETMSETMGET 数字操作INCRINCRBYDECRDECRBYINCRBYFLOAT 字符串操作APPENDSTRLENGETRANGESETRANGE 内部编码intembstrraw 在Redis中&#xff0c;字符串string存储的是二进制&#xff0c;以byte为单位&#xff0c;输入的二进…

基于SpringBoot图书馆预约与占座小程序【附源码】

效果如下&#xff1a; 首页界面 用户登录界面 查看座位界面 管理员登录界面 管理员主界面 座位分布信息界面 预约信息界面 研究背景 随着互联网技术的不断进步和智能手机的广泛普及&#xff0c;图书馆作为知识获取和学习的重要场所&#xff0c;其管理方式也在逐步向信息化和智…

PostgreSQL的扩展Citus介绍

PostgreSQL的扩展Citus介绍 Citus 是一个 PostgreSQL 的扩展&#xff0c;用于将 PostgreSQL 转变成一个分布式数据库集群。它使得用户可以利用多台机器来处理更大的数据集和更高的查询吞吐量&#xff0c;从而提升数据库的扩展性和性能。Citus 主要解决的是大规模数据处理和高并…

使用iTextPDF库时,设置文字为中文格式

在使用iTextPDF库时&#xff0c;设置文字为中文格式主要涉及选择合适的中文字体&#xff0c;并确保该字体能够正确渲染中文字符。由于iTextPDF的内置字体通常不支持中文&#xff0c;因此你需要加载一个支持中文的字体文件&#xff08;如TrueType字体&#xff0c;.ttf文件&#…

【AI知识点】残差网络(ResNet,Residual Networks)

残差网络&#xff08;ResNet&#xff0c;Residual Networks&#xff09; 是由微软研究院的何凯明等人在 2015 年提出的一种深度神经网络架构&#xff0c;在深度学习领域取得了巨大的成功。它通过引入残差连接&#xff08;Residual Connection&#xff09; 解决了深层神经网络中…