Spring Boot 接入 Redis

Spring Boot 接入 Redis

简介

Redis 是一种访问速度非常快的内存数据结构存储,用作数据库、缓存、消息代理和流引擎。提供 strings、hashes、lists、sets 等数据结构。可以解决会话缓存、消息队列、分布式锁、定期将数据集存储到硬盘等功能。

通过 Redis 设计实现更详细的功能可查阅文章底部连接,特别是接口文档,它涵盖了所有可实现功能。

实现功能

  1. 创建Spring Boot 项目
  2. Windows 环境下启动 Redis 服务;
  3. 实现会话缓存功能;
  4. 测试会话缓存功能;

一、创建 Spring Boot 项目并启动 Redis

1.使用任意方式(idea、eclipse、https://start.spring.io/等)搭建项目后,添加 Redis 以及 FastJSON 依赖:

xml 复制代码
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>2.0.48</version>
</dependency>

二、Windows 环境下启动 Redis 服务

1.下载 Redis Windows 客户端(https://github.com/tporadowski/redis/releases),进入根目录执行命令启动 Redis:

redis-server redis.windows.conf

三、实现会话缓存功能

1.创建并注入连接 Redis 会话工厂配置文件 RedisConfig.java :

java 复制代码
package com.ikgade.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;

@Configuration
public class RedisConfig {

    @Bean
    LettuceConnectionFactory redisConnectionFactory() {
        // 连接 redis 配置
        RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
        configuration.setPassword(""); // 未设置密码也要配置为空 否则抛出 NO AUTH 异常
        return new LettuceConnectionFactory(configuration);
    }
}

2.创建并实现会话缓存控制器 SessionController.java :

java 复制代码
package com.ikgade.demo.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/session")
public class SessionController {

    @Resource
    RedisTemplate<String, String> redisTemplate;

    @PostMapping("/set")
    public JSONObject setMessage(@RequestBody JSONObject jsonObject){
        redisTemplate.opsForValue().set(jsonObject.getString("key"), jsonObject.getString("val"));
        return jsonObject;
    }

    @GetMapping("/get")
    public JSONObject getMessage(@RequestBody JSONObject jsonObject){
        JSONObject res = new JSONObject();
        res.put("val", redisTemplate.opsForValue().get(jsonObject.getString("key")));
        return res;
    }
}

四、测试会话缓存功能

1.存储会话缓存:

2.获取会话缓存:

参考资料

参考手册链接:
https://docs.spring.io/spring-data/redis/docs/current/reference/html/
https://docs.spring.io/spring-data/redis/docs/2.6.10/reference/html/
案例仓库:
https://github.com/spring-projects/spring-data-examples/tree/main/redis
接口文档:
https://docs.spring.io/spring-data/redis/docs

相关推荐
骆晨学长11 分钟前
基于springboot的智慧社区微信小程序
java·数据库·spring boot·后端·微信小程序·小程序
AskHarries16 分钟前
利用反射实现动态代理
java·后端·reflect
bjzhang7517 分钟前
SpringBoot开发——整合SpringDoc实现在线接口文档
spring boot·springdoc
Flying_Fish_roe39 分钟前
Spring Boot-Session管理问题
java·spring boot·后端
赚钱给孩子买茅台喝41 分钟前
智能BI项目第四期
java·spring boot·spring cloud·aigc
hai405871 小时前
Spring Boot中的响应与分层解耦架构
spring boot·后端·架构
码爸2 小时前
flink 批量压缩redis集群 sink
大数据·redis·flink
Adolf_19933 小时前
Flask-JWT-Extended登录验证, 不用自定义
后端·python·flask
叫我:松哥3 小时前
基于Python flask的医院管理学院,医生能够增加/删除/修改/删除病人的数据信息,有可视化分析
javascript·后端·python·mysql·信息可视化·flask·bootstrap
海里真的有鱼3 小时前
Spring Boot 项目中整合 RabbitMQ,使用死信队列(Dead Letter Exchange, DLX)实现延迟队列功能
开发语言·后端·rabbitmq