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·后端
愿你天黑有灯下雨有伞16 分钟前
Spring Boot整合Kafka实战指南:从环境搭建到消息处理全解析
spring boot·kafka·linq
jstart千语38 分钟前
【Redis】分布式锁的实现
数据库·redis·分布式
亚林瓜子42 分钟前
AWS EC2源代码安装valkey命令行客户端
redis·云计算·aws·cli·valkey
Clf丶忆笙1 小时前
SpringBoot异步处理@Async深度解析:从基础到高阶实战
spring boot
java1234_小锋2 小时前
Spring Bean有哪几种配置方式?
java·后端·spring
专注代码七年3 小时前
在Windows 境下,将Redis和Nginx注册为服务。
windows·redis·nginx
智_永无止境3 小时前
Redis 8.0携新功能,重新开源
数据库·redis·开源
fengchengwu20123 小时前
langchain4j集成QWen、Redis聊天记忆持久化
redis·langchain·qwen·聊天记忆持久化
柯南二号3 小时前
【后端】SpringBoot用CORS解决无法跨域访问的问题
java·spring boot·后端