美食家大橙子 发表于 2024-8-1 21:48:38

【SpringBoot】集成Redis

目录

[*]1 redis

[*]1.1 特点
[*]1.2 支持的数据范例
[*]1.3 应用场景

[*]2 安装redis

[*]2.1 docker

[*]3 可视化软件

[*]3.1 Redis Desktop Manager(RDM)
[*]3.2 QuickRedis (保举)

[*]4 SpringBoot集成

[*]4.1 引入redis
[*]4.2. 操作redis

[*]4.2.1 直接操作

[*]4.3 分布式会话场景(Resis-Session)


1 redis

​        redis是一个用C语言开发的,基于内存结构举行键值对数据存储、高性能、非关系型NoSQL数据库
​        官网: https://redis.io/
1.1 特点


[*]基于内存存储,数据读写效率很高
[*]本身支持持久化
[*]虽然基于key-value存储,但是支持多种数据范例
[*]支持集群、支持主从模式
1.2 支持的数据范例

Redis是以键值对形式,举行数据存储,同时value也支持多种数据范例

[*]String 字符串
[*]hash 映射
[*]list 列表
[*]set 集合
[*]zset 有序集合
https://img2024.cnblogs.com/other/3272173/202408/3272173-20240801231656327-237120933.png
1.3 应用场景


[*]分布式会话
在分布式体系中,可以利用redis实现session(共享缓存)
[*]缓存
提高访问数据、降低数据库压力
[*]分布式锁
基于redis的操作特征,可以实现分布式锁功能
[*]点赞、排行榜、计数器
对数据及时读写要求比较高,但对数据库一致性要求不是很高的功能场景
[*]消息中间件
实现应用之间的通信
2 安装redis

官网上有根据windows、linux、mac 等环境安装redis,这里主要介绍docker安装方式,操作更加方便好用
2.1 docker


[*]安装docker

[*]Windows安装:https://www.runoob.com/docker/windows-docker-install.html
[*]Centos安装:https://www.runoob.com/docker/centos-docker-install.html
[*]Mac安装:https://www.runoob.com/docker/macos-docker-install.html

[*]查看docker版本
网址:https://hub.docker.com/_/redis?tab=tags
[*]拉取镜像
docker pull redis
[*]创建容器
docker run -itd --name redis_1 -p 6379:6379 redis
[*]进入容器
docker exec -it redis_1 /bin/bash
3 可视化软件

3.1 Redis Desktop Manager(RDM)


[*]Github
https://github.com/RedisInsight/RedisDesktopManager
[*]安装文档
​                https://docs.resp.app/en/latest/install/
3.2 QuickRedis (保举)

​        是一款国产开源的 免费 Redis 可视化管理工具,支持直连、哨兵、集群模式,支持亿万数目级的 key,支持 Windows 、 Mac OS X 和 Linux 下运行。

[*]官网: https://quick123.net/
[*]下载地址:
https://gitee.com/quick123official/quick_redis_blog/releases/
https://github.com/quick123official/quick_redis_blog/releases/
4 SpringBoot集成

4.1 引入redis

​        打开pom.xml文件
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    <version>2.7.15</version>
</dependency>4.2. 操作redis

4.2.1 直接操作


[*]界说stringRedisTemplate
@Autowired
private StringRedisTemplate stringRedisTemplate;
[*]写入
// 过期时间,单位:分
public final static int Redis_Key_Login_Expire_Time = 30;

public final static Straing key = 'key_1';
public final static Straing content = '这是存储内容';

stringRedisTemplate.boundValueOps(key).set(content, Constant.Redis_Key_Login_Expire_Time, TimeUnit.MINUTES);
[*]读取
public final static Straing key = 'key_1';
String content = stringRedisTemplate.boundValueOps(key).get();
4.3 分布式会话场景(Resis-Session)

​        引入 spring-session 和 redis 的整合,使得自动将 session 存储到 redis 中
​        PS: 直接配置就好,不必要改动代码

[*]引入依赖包
根据你的SpringBoot版本(我的v2.7.15)选择依赖,尽量保持一致
<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
    <version>2.7.4</version>
</dependency>
[*]修改application.yml配置

[*]spring-session 存储配置
[*]tore-type:

[*]none : 默认值,表示存储在单台服务器上
[*]Redis : 表示存储在redis上

spring:
        # redis 配置
   redis:
      host: localhost
      port: 6379
      password: 123456
      database: 2   # Redis共有16个数据(0-15)
      
session:
      timeout: 86400        # session失效时间,单位秒,1天
      store-type: redis # 指定存储方式
[*]调用
public User doLogin(String userAccount, String userPassword, HttpServletRequest request) {
                // ...
                // 存储session
                String key = "userLoginState";
                String value = "这是存储内容"
                request.getSession().setAttribute(key,value);
               
                return null;
}
[*]存储成结果
https://img2024.cnblogs.com/other/3272173/202408/3272173-20240801231701444-2060292170.png
本文由博客一文多发平台 OpenWrite 发布!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: 【SpringBoot】集成Redis