欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > SpringBoot新手快速入门系列教程7:基于Redis的一个简单存取数据的例子

SpringBoot新手快速入门系列教程7:基于Redis的一个简单存取数据的例子

2024/10/26 0:39:06 来源:https://blog.csdn.net/liuyuyefz/article/details/140240179  浏览:    关键词:SpringBoot新手快速入门系列教程7:基于Redis的一个简单存取数据的例子

新手可能有这样的疑问,有了数据库的存取方式,我们为什么还要使用Redis这种缓存数据库读取方式呢?

原因主要有以下几点:

1. 性能提升

数据库查询尤其是复杂查询可能会消耗大量的时间和资源。而Redis是一个内存数据库,读写速度非常快,可以大幅度提高应用的响应速度。通过缓存热点数据,可以减少对数据库的直接访问,从而降低数据库的负载。

2. 减少数据库负载

对于高并发的应用,频繁的数据库查询可能会导致数据库负载过高,甚至出现性能瓶颈。通过使用Redis缓存,可以将部分读请求转移到缓存中,从而减轻数据库的压力,提高整体系统的稳定性。

3. 提高数据读取速度

Redis基于内存的特性使得数据读取速度非常快,远远超过磁盘读取速度。对于一些经常被访问的数据,将其存储在Redis中可以显著提升访问速度,从而改善用户体验。

4. 支持复杂数据结构

Redis支持丰富的数据结构,如字符串、哈希、列表、集合、有序集合等,可以方便地实现各种复杂的缓存需求。比如,你可以使用Redis的哈希来存储用户信息,使用有序集合来实现排行榜等功能。

5. 可靠的持久化选项

尽管Redis是内存数据库,但它提供了多种持久化选项(如RDB快照和AOF日志),可以在一定程度上保证数据的持久性,避免数据丢失。

如果您对IDAE编辑器或者mysql的基础知识不慎熟悉,可以移步来到我之前的教程快速预览一下基础的用法,可以保证你能顺利的看下去下面的教程

SpringBoot新手快速入门系列教程四:创建第一个SringBoot的API-CSDN博客

SpringBoot新手快速入门系列教程二:MySql5.7.44的免安装版本下载和配置,以及简单的Mysql生存指令指南。-CSDN博客

1,下载安装Redis

(1)在选择Redis的版本时,通常建议使用最新的稳定版(stable version),因为它包含了最新的功能和修复,并经过了广泛的测试。

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

(2)运行Redis服务器

双击运行server

(3)测试连接

另开一个命令提示符窗口,进入解压后的目录,运行redis-cli.exe,然后输入PING命令来测试连接:

2,创建项目

我们新建一个‘HelloRedis’项目

你需要添加以下依赖项:

  1. Spring Web(用于创建Web应用程序和RESTful服务)
  2. Spring Data Reactive Redis(用于与Redis进行交互)

 

3,项目结构

我们需要创建config、controller两个package

同时创建RedisConfig、ReactiveController两个类

解释

  • RedisConfig.java

    • 这是一个配置类,用于创建和配置 ReactiveRedisTemplate
    • ReactiveRedisTemplate 是用于与 Redis 进行异步和非阻塞交互的模板类。
    • RedisSerializationContext 配置了键和值的序列化方式,使用 StringRedisSerializer 来将键和值序列化为字符串。
  • ReactiveUserController.java

    • 这是一个 REST 控制器类,包含两个端点 /register/login
    • /register 端点接收用户名参数,将用户名存储到 Redis 中,并返回注册成功的信息。
    • /login 端点接收用户名参数,检查用户名是否存在于 Redis 中,存在则返回登录成功的信息,不存在则返回未注册的信息。

4,代码展示

1,RedisConfig

package com.yuye.www.helloredis.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;@Configuration // 标注这是一个配置类
public class RedisConfig {// 定义一个Bean,用于配置ReactiveRedisTemplate@Beanpublic ReactiveRedisTemplate<String, String> reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {// 创建一个Redis序列化上下文,使用StringRedisSerializer来序列化键和值RedisSerializationContext<String, String> context = RedisSerializationContext.<String, String>newSerializationContext(new StringRedisSerializer()).hashKey(new StringRedisSerializer()) // 设置哈希键的序列化方式.hashValue(new StringRedisSerializer()) // 设置哈希值的序列化方式.build();// 创建并返回ReactiveRedisTemplate实例return new ReactiveRedisTemplate<>(factory, context);}
}

2,ReactiveController

package com.yuye.www.helloredis.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;@RestController // 标注这是一个REST控制器
public class ReactiveUserController {@Autowired // 自动注入ReactiveRedisTemplateprivate ReactiveRedisTemplate<String, String> reactiveRedisTemplate;/*** 处理用户注册请求* @param name 用户名* @return 注册结果*/@GetMapping("/register")public Mono<String> register(@RequestParam String name) {// 使用ReactiveRedisTemplate将用户名存入Redisreturn reactiveRedisTemplate.opsForValue().set(name, "registered").thenReturn("User " + name + " registered successfully"); // 注册成功后返回结果}/*** 处理用户登录请求* @param name 用户名* @return 登录结果*/@GetMapping("/login")public Mono<String> login(@RequestParam String name) {// 使用ReactiveRedisTemplate检查用户名是否存在return reactiveRedisTemplate.hasKey(name).flatMap(isRegistered -> {if (isRegistered) {return Mono.just("User " + name + " is logged in"); // 用户存在,返回登录成功信息} else {return Mono.just("User " + name + " is not registered"); // 用户不存在,返回未注册信息}});}
}

3,application.properties

spring.application.name=HelloRedis
spring.data.redis.host=localhost
spring.data.redis.port=6379server.port=8081

5, 启动测试项目

(1)保存

http://localhost:8081/register?name=yourName

(2)读取

http://localhost:8081/login?name=yourName

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com