SpringBoot + Mybatis Plus 整合 Redis

news2025/3/18 14:18:48

Redis 在用户管理系统中的典型应用场景

结合你的用户增删改查接口,以下是 Redis 的实用场景和具体实现方案:

场景作用实现方案
用户信息缓存减少数据库压力,加速查询响应使用 Spring Cache + Redis 注解缓存
登录 Token 存储分布式 Session 或 JWT Token 管理将 Token 与用户信息绑定,设置过期时间
接口限流防止恶意刷接口基于 Redis 计数器实现滑动窗口限流
重复提交拦截防止用户重复提交表单用 Redis 存储请求唯一标识,设置短期过期
热点数据预加载提前缓存高频访问数据定时任务 + Redis 存储

Mac M1 安装 Redis 详细步骤

1. 通过 Homebrew 安装 Redis
# 安装 Homebrew(如果尚未安装)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# 安装 Redis
brew install redis
2. 启动 Redis 服务
# 前台启动(测试用,Ctrl+C 退出)
redis-server

# 后台启动(推荐)
brew services start redis
3. 验证安装
# 连接 Redis 客户端
redis-cli ping  # 应返回 "PONG"

Spring Boot 3 整合 Redis

1. 添加依赖

pom.xml 中:

		<!-- Spring Cache 核心依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>

        <!-- Redis 驱动 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
2. 配置 Redis 连接

application.yml

spring:
  data:
    redis:
      host: localhost
      port: 6379
      # password: your-password  # 如果设置了密码
      lettuce:
        pool:
          max-active: 8
          max-idle: 8
3. 示例

配置类

package com.example.spring_demo01.config;

import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.*;

import java.time.Duration;

@Configuration
public class RedisConfig {

    // 配置 RedisTemplate
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);

        // Key 序列化
        template.setKeySerializer(new StringRedisSerializer());

        // Value 序列化为 JSON
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        // Hash 结构序列化
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        return template;
    }

    // 配置缓存管理器
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory factory) {
        RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig()
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer()))
                .entryTtl(Duration.ofMinutes(30)); // 设置默认过期时间

        return RedisCacheManager.builder(factory)
                .cacheDefaults(config)
                .build();
    }
}

接口限流工具类

package com.example.spring_demo01.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Component
public class RateLimiter {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public boolean allowRequest(String userId) {
        String key = "rate_limit:" + userId;
        long now = System.currentTimeMillis();
        long windowMs = 60_000; // 1 分钟

        // 移除窗口外的请求记录
        redisTemplate.opsForZSet().removeRangeByScore(key, 0, now - windowMs);

        // 统计当前窗口内请求数
        Long count = redisTemplate.opsForZSet().zCard(key);
        if (count != null && count >= 10) {
            return false; // 超过限制
        }

        // 记录本次请求
        redisTemplate.opsForZSet().add(key, UUID.randomUUID().toString(), now);
        redisTemplate.expire(key, windowMs, TimeUnit.MILLISECONDS);
        return true;
    }
}

实体类

package com.example.spring_demo01.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;

import java.io.Serializable;

@Data
@TableName("user")
@JsonIgnoreProperties(ignoreUnknown = true) // 防止 JSON 反序列化问题
public class User implements Serializable { // 实现 Serializable
    @TableId(type = IdType.AUTO) // 主键自增
    private Long id;
    private String name;
    private Integer age;
    private String email;
}

service层

package com.example.spring_demo01.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.spring_demo01.entity.User;
import com.example.spring_demo01.mapper.UserMapper;
import com.example.spring_demo01.service.UserService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;

@Service
public class UserServiceImpl
        extends ServiceImpl<UserMapper, User>
        implements UserService {
    // 对 MyBatis Plus 的 getById 方法添加缓存
    @Cacheable(value = "user", key = "#id")
    @Override
    public User getById(Serializable id) {
        return super.getById(id);
    }

    // 更新时清除缓存
    @CacheEvict(value = "user", key = "#entity.id")
    @Override
    public boolean updateById(User entity) {
        return super.updateById(entity);
    }

    // 删除时清除缓存
    @CacheEvict(value = "user", key = "#id")
    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }
}

controller层

package com.example.spring_demo01.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.spring_demo01.annotation.AdminOnly;
import com.example.spring_demo01.entity.User;
import com.example.spring_demo01.service.UserService;
import com.example.spring_demo01.utils.RateLimiter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.util.List;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private RateLimiter rateLimiter;


    // ------------------------------ 增 ------------------------------

    @PostMapping
    public String addUser(@RequestBody User user, @RequestHeader String clientId) {
        String key = "SUBMIT_LOCK:" + clientId + ":" + user.hashCode();

        // 10秒内不允许重复提交
        Boolean success = redisTemplate.opsForValue()
                .setIfAbsent(key, "", Duration.ofSeconds(10));

        if (Boolean.FALSE.equals(success)) {
            throw new RuntimeException("请勿重复提交");
        }

        userService.save(user);
        return "新增成功";
    }

    // ------------------------------ 删 ------------------------------
    @DeleteMapping("/{id}")
    public String deleteUser(@PathVariable Long id) {
        userService.removeById(id);
        return "删除成功";
    }

    @DeleteMapping("/batch")
    public String deleteBatch(@RequestBody List<Long> ids) {
        userService.removeByIds(ids);
        return "批量删除成功";
    }

    // ------------------------------ 改 ------------------------------
    @PutMapping
    public String updateUser(@RequestBody User user) {
        userService.updateById(user);
        return "更新成功";
    }

    // ------------------------------ 查 ------------------------------
    @GetMapping("/{id}")
    @AdminOnly
    public User getUserById(@PathVariable Long id) {
        return userService.getById(id);
    }

    @GetMapping("/list")
    public List<User> listUsers(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer age) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (name != null) {
            wrapper.like("name", name); // 模糊查询姓名
        }
        if (age != null) {
            wrapper.eq("age", age);     // 精确查询年龄
        }
        return userService.list(wrapper);
    }
    
    @GetMapping("/page")
    public Page<User> pageUsers(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestHeader(value = "Authorization") String token) {

        // 从 Token 中获取用户ID
        log.info("token:{}", token);
        log.info("User:{}", redisTemplate.opsForValue().get(token.split(" ")[1]));
        User user = (User) redisTemplate.opsForValue().get(token.split(" ")[1]);
        if (user == null) throw new RuntimeException("未登录");

        // 限流校验
        if (!rateLimiter.allowRequest("PAGE_" + user.getId())) {
            throw new RuntimeException("请求过于频繁");
        }

        return userService.page(new Page<>(pageNum, pageSize));
    }

    // ------------------------------ other ------------------------------
    @GetMapping("/error")
    public String getError() {
        throw new RuntimeException();
    }

    @PostMapping("/login")
    public String login(@RequestBody User user) {
        log.info("login user:{}", user);
        // 验证用户逻辑(示例简化)
        User dbUser = userService.getOne(new QueryWrapper<User>()
                .eq("id", user.getId())
                .eq("name", user.getName()));

        if (dbUser == null) {
            throw new RuntimeException("登录失败");
        }

        // 生成 Token 并存储
        String token = "TOKEN_" + UUID.randomUUID();
        redisTemplate.opsForValue().set(
                token,
                dbUser,
                Duration.ofMinutes(30)
        );
        return token;
    }
}

在启动类添加注解:

package com.example.spring_demo01;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cache.annotation.EnableCaching;

@SpringBootApplication
@MapperScan("com.example.spring_demo01.mapper")
@ServletComponentScan // 启用 Servlet 组件扫描(如 Filter、Servlet)
@EnableCaching // 启动缓存,Redis使用
public class SpringDemo01Application {

    public static void main(String[] args) {
        SpringApplication.run(SpringDemo01Application.class, args);
    }

}

常见问题排查

Q1: 连接 Redis 超时
  • 检查服务状态:运行 redis-cli ping 确认 Redis 是否正常运行
  • 查看端口占用lsof -i :6379
  • 关闭防火墙sudo ufw allow 6379
Q2: Spring Boot 无法注入 RedisTemplate
  • 确认配置类:添加 @EnableCaching@Configuration
  • 检查序列化器:显式配置序列化方式避免 ClassCastException
    @Configuration
    public class RedisConfig {
        @Bean
        public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
            RedisTemplate<String, Object> template = new RedisTemplate<>();
            template.setConnectionFactory(factory);
            template.setKeySerializer(new StringRedisSerializer());
            template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
            return template;
        }
    }
    

总结

通过 Redis 你可以为项目快速实现:

  1. 高性能缓存层 - 降低数据库负载
  2. 分布式会话管理 - 支持横向扩展
  3. 精细化流量控制 - 保障系统稳定性

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2317263.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【AI 大模型】RAG 检索增强生成 ⑤ ( 向量数据库 | 向量数据库 索引结构和搜索算法 | 常见 向量数据库 对比 | 安装并使用 向量数据库 chromadb 案例 )

文章目录 一、向量数据库1、向量数据库引入2、向量数据库简介3、向量数据库 索引结构和搜索算法4、向量数据库 应用场景5、传统数据库 与 向量数据库 对比 二、常见 向量数据库 对比三、向量数据库 案例1、安装 向量数据库 chromadb2、核心要点 解析① 创建数据库实例② 创建数…

解决single cell portal点击下载但跳转的是网页

Single cell RNA-seq of Tmem100-lineage cells in a mouse model of osseointegration - Single Cell Portal 想下载个小鼠数据集&#xff1a; 点击下载跳转为网页&#xff1a; 复制bulk download给的链接无法下载 bulk download给的原链接&#xff1a; curl.exe "http…

基于 Prometheus + Grafana 监控微服务和数据库

以下是基于 Prometheus Grafana 监控微服务和数据库的详细指南&#xff0c;包含架构设计、安装配置及验证步骤&#xff1a; 一、整体架构设计 二、监控微服务 1. 微服务指标暴露 Spring Boot 应用&#xff1a; xml <!-- 添加 Micrometer 依赖 --> <dependency>…

CAN总线的CC帧和FD帧之间如何仲裁

为满足CAN总线日益提高的带宽需求&#xff0c;博世公司于2012年推出CAN FD&#xff08;具有灵活数据速率的CAN&#xff09;标准&#xff0c;国际标准化组织&#xff08;ISO&#xff09;2015年通过ISO 11898-1:2015标准&#xff0c;正式将CAN FD纳入国际标准&#xff0c;以示区别…

SpringBoot 第一课(Ⅲ) 配置类注解

目录 一、PropertySource 二、ImportResource ①SpringConfig &#xff08;Spring框架全注解&#xff09; ②ImportResource注解实现 三、Bean 四、多配置文件 多Profile文件的使用 文件命名约定&#xff1a; 激活Profile&#xff1a; YAML文件支持多文档块&#xff…

Excel(函数篇):COUNTIF与CONUTIFS函数、SUMIF与SUMIFS函数、ROUND函数、MATCH与INDEX函数、混合引用与条件格式

目录 COUNTIF和COUNTIFS函数COUNTIF函数COUNTIFS函数SUMIF和SUMIFS函数SUMIF函数SUMIFS函数SUMIFS函数与控件实现动态年月汇总ROUND、ROUNDUP、ROUNDDOWN函数单元格混合引用条件格式与公式,标记整行数据MATCH和INDEX函数COUNTIF和COUNTIFS函数 COUNTIF函数 统计下“苏州”出现…

虚拟定位 1.2.0.2 | 虚拟定位,上班打卡,校园跑步模拟

Fake Location是一款运行于安卓平台上的功能强大、简单实用的虚拟定位软件。它能够帮助用户自定义位置到地图上的任意地方&#xff0c;以ROOT环境运行不易被检测&#xff0c;同时也支持免ROOT运行。提供路线模拟、步频模拟、WIFI模拟等方式&#xff0c;支持反检测。 大小&…

【最大异或和——可持久化Trie】

题目 代码 #include <bits/stdc.h> using namespace std;const int N 6e510; //注意这里起始有3e5&#xff0c;又可能插入3e5 const int M N * 25;int rt[N], tr[M][2]; //根&#xff0c;trie int idx, cnt, br[M]; //根分配器&#xff0c;点分配器&#xff0c;点的相…

C# WPF编程-启动新窗口

C# WPF编程-启动新窗口 新建窗口&#xff1a; 工程》添加》窗口 命名并添加新的窗口 这里窗口名称为Window1.xaml 启动新窗口 Window1 win1 new Window1(); win1.Show(); // 非模态启动窗口win1.ShowDialog(); // 模态启动窗口 模态窗口&#xff1a;当一个模态窗口被打开时&a…

Python 实现大文件的高并发下载

项目背景 基于一个 scrapy-redis 搭建的分布式系统&#xff0c;所有item都通过重写 pipeline 存储到 redis 的 list 中。这里我通过代码演示如何基于线程池 协程实现对 item 的中文件下载。 Item 结构 目的是为了下载 item 中 attachments 保存的附件内容。 {"crawl_tim…

【最新】 ubuntu24安装 1panel 保姆级教程

系统&#xff1a;ubuntu24.04.1 安装软件 &#xff1a;1panel 第一步&#xff1a;更新系统 sudo apt update sudo apt upgrade 如下图 第二步&#xff1a;安装1panel&#xff0c;运行如下命令 curl -sSL https://resource.fit2cloud.com/1panel/package/quick_start.sh -o …

c++图论(二)之图的存储图解

在 C 中实现图的存储时&#xff0c;常用的方法包括 邻接矩阵&#xff08;Adjacency Matrix&#xff09;、邻接表&#xff08;Adjacency List&#xff09; 和 边列表&#xff08;Edge List&#xff09;。以下是具体实现方法、优缺点分析及代码示例&#xff1a; 1. 邻接矩阵&…

c++图论(一)之图论的起源和图的概念

C 图论之图论的起源和图的概念 图论&#xff08;Graph Theory&#xff09;是数学和计算机科学中的一个重要分支&#xff0c;其起源可以追溯到 18 世纪 的经典问题。以下是图论的历史背景、核心起源问题及其与基本概念和用途&#xff1a; 借用一下CSDN的图片哈 一、图论的起源&…

ChatGPT and Claude国内使用站点

RawChat kelaode chatgptplus chatopens&#xff08;4.o mini免费&#xff0c;plus收费&#xff09; 网页&#xff1a; 定价&#xff1a; wildcard 网页&#xff1a; 虚拟卡定价&#xff1a; 2233.ai 网页&#xff1a; 定价&#xff1a; MaynorAPI chatgpt cla…

进行性核上性麻痹:精心护理,点亮希望之光

进行性核上性麻痹是一种罕见的神经退行性疾病&#xff0c;严重影响患者的生活质量。有效的健康护理能够在一定程度上缓解症状、延缓病情发展&#xff0c;给患者带来更好的生活体验。 在日常生活护理方面&#xff0c;由于患者平衡能力逐渐下降&#xff0c;行动不便&#xff0c;居…

ZED X系列双目3D相机的耐用性与创新设计解析

在工业自动化和学术研究领域&#xff0c;高精度的视觉设备正成为提升效率和质量的关键。ZED X系列AI立体相机&#xff0c;凭借其先进的技术和耐用的设计&#xff0c;为这一领域带来了新的可能。 核心技术&#xff1a;深度感知与精准追踪 ZED X系列的核心技术之一是Neural Dept…

HarmonyOS三层架构实战

目录&#xff1a; 1、三层架构项目结构1.0、三层架构简介1.1、 common层&#xff08;主要放一些公共的资源等&#xff09;1.2、 features层&#xff08;主要模块定义的组件以及图片等静态资源&#xff09;1.3、 products层&#xff08;主要放主页面层和一些主要的资源&#xff…

计算机四级 - 数据库原理 - 第4章 「关系数据库标准语言SQL」

4.1 SQL概述 4.1.1 结构化查询语言SQL SQL(Structured Query Language)称为结构化查询语言&#xff0c;它是由1974年由Boyce和Chamberi提出的&#xff0c;1975年至1979年IBM公司的San Jose Research Laboratory研制了关系数据库管理系统的原型系统System R,并实现了这种语198…

基于PMU的14节点、30节点电力系统状态估计MATLAB程序

“电气仔推送”获得资料&#xff08;专享优惠&#xff09; 程序简介&#xff1a; 程序采用三种方法对14节点和30节点电力系统状态进行评估&#xff1a; ①PMU同步向量测量单元结合加权最小二乘法&#xff08;WLS&#xff09;分析电力系统的电压幅值和相角状态&#xff1b; …

Deepseek API+Python测试用例一键生成与导出-V1.0.2【实现需求文档图片识别与用例生成自动化】

在测试工作中&#xff0c;需求文档中的图片&#xff08;如界面设计图、流程图&#xff09;往往是测试用例生成的重要参考。然而&#xff0c;手动提取图片并识别内容不仅耗时&#xff0c;还容易出错。本文将通过一个自研小工具&#xff0c;结合 PaddleOCR 和大模型&#xff0c;自…