1 redis

​ redis是一个用C语言开发的,基于内存结构进行键值对数据存储、高性能、非关系型NoSQL数据库

​ 官网: https://redis.io/

1.1 特点

  • 基于内存存储,数据读写效率很高
  • 本身支持持久化
  • 虽然基于key-value存储,但是支持多种数据类型
  • 支持集群、支持主从模式

1.2 支持的数据类型

Redis是以键值对形式,进行数据存储,同时value也支持多种数据类型

  • String 字符串
  • hash 映射
  • list 列表
  • set 集合
  • zset 有序集合

image-20231217202241489

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文件

<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
<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)选择依赖,尽量保持一致

    <!-- https://mvnrepository.com/artifact/org.springframework.session/spring-session-data-redis -->
    <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;
     }
    
  • 存储成效果

    image-20231217211247464