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

相关推荐
苹果醋313 分钟前
JAVA设计模式之策略模式
java·运维·spring boot·mysql·nginx
CodeSheep16 分钟前
中国四大软件外包公司
前端·后端·程序员
千寻技术帮17 分钟前
10370_基于Springboot的校园志愿者管理系统
java·spring boot·后端·毕业设计
风象南17 分钟前
Spring Boot 中统一同步与异步执行模型
后端
聆风吟º19 分钟前
【Spring Boot 报错已解决】彻底解决 “Main method not found in class com.xxx.Application” 报错
java·spring boot·后端
乐茵lin27 分钟前
golang中 Context的四大用法
开发语言·后端·学习·golang·编程·大学生·context
爱吃KFC的大肥羊31 分钟前
Redis持久化详解(一):RDB快照机制深度解析
数据库·redis·缓存
黎明破晓.34 分钟前
Redis
数据库·redis·缓存
宋情写1 小时前
单元测试、覆盖率测试-Springboot
spring boot·单元测试·测试覆盖率
步步为营DotNet1 小时前
深度探索ASP.NET Core中间件的错误处理机制:保障应用程序稳健运行
后端·中间件·asp.net