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

相关推荐
计算机学姐19 分钟前
基于SpringBoot的动漫推荐系统【协同过滤推荐算法+词云图+排行榜】
java·vue.js·spring boot·后端·mysql·intellij-idea·推荐算法
韩立学长2 小时前
基于Springboot的影视评论网站的设计与实现58py6238(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·后端
小学鸡!3 小时前
Spring Boot通过手机号获取归属地
java·spring boot
whltaoin4 小时前
【JAVA全栈项目】弧图图-智能图床 SpringBoot+Vue3 :[框架开荒:一文全步骤打通前后端项目全流程]
java·spring boot·vue·开源项目·全栈·cos
IDOlaoluo4 小时前
TinyRDM 1.2.3 Windows版安装教程(附Redis客户端下载及详细步骤)
数据库·redis·缓存
Fu1co4 小时前
【Spring Boot】Spring Boot解决循环依赖
java·spring boot·spring
JavaTree20175 小时前
SpringMVC基础入门
后端
国服第二切图仔5 小时前
Rust中泛型函数实现不同类型数据的比较
开发语言·后端·rust
Anlici5 小时前
连载小说大学生课设 需求&架构
前端·javascript·后端
我命由我123455 小时前
Derby - Derby 服务器(Derby 概述、Derby 服务器下载与启动、Derby 连接数据库与创建数据表、Derby 数据库操作)
java·运维·服务器·数据库·后端·java-ee·后端框架