SpringBoot+Redis实现接口级别缓存信息
本文主要讲述如何通过SpringBoot+Redis实现接口级别缓存信息
背景
近期因为一直在处理公司的老项目,恰好碰到产品说页面有一些信息展示慢,简单看了一下页面接口,发现查询的是系统中几张大表(数据量在千万级别),还会关联一些其他的表,导致接口性能极差,但是由于这些信息也不存在"及时性"这么一说,便想着通过接口缓存来控制
相关技术
jdk 1.8
reids 5.0.7
实现思路
通过注解来标识需要缓存的接口,依据注解的内容去找到对应的建造者,通过建造者来找到具体去执行的类,最终达可扩展+缓存的效果
注解相关代码
package com.com.example.springdemo;
import com.com.example.springdemo.aspect.enums.RedisCacheEnums;
import java.lang.annotation.*;
import java.util.concurrent.TimeUnit;
/**
* @CreateAt: 2023-11-3 11:25:37
* @ModifyAt: 2023-11-3 11:25:37
* @Version 1.0
*/
@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RedisCache {
/**
* 缓存的key
**/
String key() default "";
/**
* 参数类型
*
* @return
*/
RedisCacheEnums type();
/**
* 缓存时长,默认-1表示永久有效
**/
int time() default 300;
/**
* 缓存时长单位,默认单位秒
**/
TimeUnit timeType() default TimeUnit.SECONDS;
}
枚举相关代码
package com.example.springdemo.enums;
import java.util.Arrays;
/**
* 缓存类型
*
* @CreateAt: 2023-11-3 11:26:22
* @ModifyAt: 2023-11-3 11:26:22
* @Version 1.0
*/
public enum RedisCacheEnums {
QUERY_MEMBER_INFO(1, "查询会员信息"),
;
private Integer code;
private String type;
RedisCacheEnums(Integer code, String type) {
this.code = code;
this.type = type;
}
public static RedisCacheEnums getByCode(int code) {
return Arrays.stream(RedisCacheEnums.values())
.filter(item -> item.getCode().intValue() == code)
.findFirst()
.orElse(null);
}
public Integer getCode() {
return code;
}
public String getType() {
return type;
}
}
切换相关代码
package com.example.springdemo.aspect;
import com.example.springdemo.RedisCacheProvider;
import com.example.springdemo.handler.RedisCacheHandler;
import com.google.gson.Gson;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
/**
* @CreateAt: 2023-11-3 11:27:20
* @ModifyAt: 2023-11-3 11:27:20
* @Version 1.0
*/
@Aspect
@Slf4j
@Component
@AllArgsConstructor
public class RedisCacheAspect {
@Pointcut("@annotation(com.example.springdemo.RedisCache)")
public void annotationPoint() throws Throwable {
}
private RedisCacheProvider redisCacheProvider;
/**
* 却面切入点
* implements Serializable 对象需要继承
* @param proceedingJoinPoint
* @return
* @throws Throwable
*/
@Around(value = "annotationPoint()")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
try {
MethodSignature method = (MethodSignature) proceedingJoinPoint.getSignature();
RedisCache redisCache = method.getMethod().getAnnotation(RedisCache.class);
Object[] args = proceedingJoinPoint.getArgs();
RedisCacheHandler apply = redisCacheProvider.apply(redisCache.type());
Boolean hasKey = apply.existHandler(args, redisCache.key());
if (hasKey) {
return apply.queryHandler(args, redisCache.key());
} else {
Object result = proceedingJoinPoint.proceed();
apply.handler(redisCache.type(), args, result, redisCache.time(), redisCache.timeType(), redisCache.key());
return result;
}
} catch (Exception e) {
log.info("RedisCacheAspect Error:{}", e.toString());
return proceedingJoinPoint.proceed();
}
}
}
建造者和相关hannder对应代码
package com.example.springdemo;
import com.example.springdemo.enums.RedisCacheEnums;
import com.example.springdemo.handler.RedisCacheHandler;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import java.util.List;
/**
* 缓存提供者
* @CreateAt: 2023-11-3 11:28:42
* @ModifyAt: 2023-11-3 11:28:42
* @Version 1.0
*/
@Component
@AllArgsConstructor
@Slf4j
public class RedisCacheProvider {
private List<RedisCacheHandler> handlers;
public RedisCacheHandler apply(RedisCacheEnums type) {
RedisCacheHandler redisCacheHandler = handlers.stream().filter(x -> x.support(type)).findFirst().orElse(null);
Assert.notNull(redisCacheHandler, "未找到对应的处理器");
return redisCacheHandler;
}
}
package com.example.springdemo.handler;
import com.example.springdemo.enums.RedisCacheEnums;
import java.util.concurrent.TimeUnit;
/**
* @CreateAt: 2023-11-3 11:29:39
* @ModifyAt: 2023-11-3 11:29:39
* @Version 1.0
*/
public interface RedisCacheHandler {
/**
* 是否支持处理
*
* @param type
* @return
*/
boolean support(RedisCacheEnums type);
/**
* 查询缓存信息
*
* @param args
* @param originalKey
* @return
*/
Object queryHandler(Object[] args, String originalKey) throws Exception;
/**
* 缓存信息是否存在
*
* @param args
* @param originalKey
* @return
*/
Boolean existHandler(Object[] args, String originalKey) throws Exception;
/**
* 生成缓存信息
*
* @param type 类型
* @param args 参数
* @param result 结果
* @param time 时间
* @param timeType 时间类型
* @param originalKey
*/
void handler(RedisCacheEnums type, Object[] args, Object result, Integer time, TimeUnit timeType, String originalKey) throws Exception;
}
package com.example.springdemo.handler;
import com.example.springdemo.enums.RedisCacheEnums;
import com.example.springdemo.common.utils.RedisUtil;
import com.google.gson.Gson;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
/**
* @CreateAt: 2023-11-3 11:30:30
* @ModifyAt: 2023-11-3 11:30:30
* @Version 1.0
*/
@Slf4j
@Service
@AllArgsConstructor
public class MemberHandler implements RedisCacheHandler {
private final String redisKey = "test";
// 切换成自己项目使用的redis工具类即可
private RedisUtil resdisUtil;
/**
* 是否支持处理
*
* @param type
* @return
*/
@Override
public boolean support(RedisCacheEnums type) {
return RedisCacheEnums.QUERY_MEMBER_INFO.equals(type);
}
/**
* 查询缓存信息
*
* @param args
* @param originalKey
* @return
*/
@Override
public Object queryHandler(Object[] args, String originalKey) throws Exception {
String key = getKey(args, originalKey);
return resdisUtil.get(key);
}
/**
* 查询缓存信息
*
* @param args
* @param originalKey
* @return
*/
@Override
public Boolean existHandler(Object[] args, String originalKey) throws Exception {
String key = getKey(args, originalKey);
return resdisUtil.hasKey(key);
}
/**
* 生成操作记录对象
*
* @param type
* @param args
* @param result
* @param time
* @param timeType
* @param originalKey
* @return
*/
@Override
public void handler(RedisCacheEnums type, Object[] args, Object result, Integer time, TimeUnit timeType, String originalKey) throws Exception {
String key = getKey(args, originalKey);
resdisUtil.setByTime(key, result, time, timeType);
}
/**
* 获取Key信息
*
* @param args
* @return
*/
private String getKey(Object[] args, String originalKey) throws Exception {
try {
Object omiMemberInquiryVO = (Object ) args[0];
// 拼装缓存key信息
String key = "test";
log.info("RedisCacheAspect key:{}",key);
return key;
} catch (Exception e) {
log.info("RedisCacheAspect 拼装Key参数异常:{},e:{}", new Gson().toJson(args), e.toString());
throw new Exception("拼装Key参数异常");
}
}
}
手动ps:可能很多人都会问为什么不用Spring自带的,而需要自己去写,主要原因还是这是一个老系统,压根找不全对数据进行修改、删除的地方
如有哪里讲得不是很明白或是有错误,欢迎指正
如您喜欢的话不妨点个赞收藏一下吧
SpringBoot+Redis实现接口级别缓存信息的更多相关文章
- 【Redis】SpringBoot+Redis+Ehcache实现二级缓存
一.概述 1.1 一些疑惑? 1.2 场景 1.3 一级缓存.两级缓存的产生 1.4 流程分析 二.项目搭建 一.概述 1.1 一些疑惑? Ehcache本地内存 Redis 分布式缓存可以共享 一级 ...
- SpringBoot + redis + @Cacheable注解实现缓存清除缓存
一.Application启动类添加注解 @EnableCaching 二.注入配置 @Bean public CacheManager cacheManager(RedisTemplate redi ...
- springboot redis 缓存对象
只要加入spring-boot-starter-data-redis , springboot 会自动识别并使用redis作为缓存容器,使用方式如下 gradle加入依赖 compile(" ...
- spring boot 学习(十四)SpringBoot+Redis+SpringSession缓存之实战
SpringBoot + Redis +SpringSession 缓存之实战 前言 前几天,从师兄那儿了解到EhCache是进程内的缓存框架,虽然它已经提供了集群环境下的缓存同步策略,这种同步仍然需 ...
- springboot+redis实现缓存数据
在当前互联网环境下,缓存随处可见,利用缓存可以很好的提升系统性能,特别是对于查询操作,可以有效的减少数据库压力,Redis 是一个开源(BSD许可)的,内存中的数据结构存储系统,它可以用作数据库.缓存 ...
- springboot + redis + 注解 + 拦截器 实现接口幂等性校验
一.概念 幂等性, 通俗的说就是一个接口, 多次发起同一个请求, 必须保证操作只能执行一次 比如: 订单接口, 不能多次创建订单 支付接口, 重复支付同一笔订单只能扣一次钱 支付宝回调接口, 可能会多 ...
- Springboot + redis + 注解 + 拦截器来实现接口幂等性校验
Springboot + redis + 注解 + 拦截器来实现接口幂等性校验 1. SpringBoot 整合篇 2. 手写一套迷你版HTTP服务器 3. 记住:永远不要在MySQL中使用UTF ...
- SpringBoot + Redis:基本配置及使用
注:本篇博客SpringBoot版本为2.1.5.RELEASE,SpringBoot1.0版本有些配置不适用 一.SpringBoot 配置Redis 1.1 pom 引入spring-boot-s ...
- Redis+Caffeine两级缓存,让访问速度纵享丝滑
原创:微信公众号 码农参上,欢迎分享,转载请保留出处. 在高性能的服务架构设计中,缓存是一个不可或缺的环节.在实际的项目中,我们通常会将一些热点数据存储到Redis或MemCache这类缓存中间件中, ...
- Redis基础知识之—— 缓存应用场景
转载原文:http://www.cnblogs.com/jinshengzhi/articles/5225718.html 一.MySql+Memcached架构的问题 Memcached采用客户端- ...
随机推荐
- Windows查询进程和杀死进程
查询进程 查询进程占用的端口(通过端口查询) netstat -ano |findstr "端口号" 杀死进程 通过进程号查看所属进程名称 tasklist |findstr &q ...
- Oracle批量处理SQL
批量更新且更新字段数字需要递增 示例: DECLARE n int; -- 定义变量 BEGIN n:=1000010; --为n 赋值 FOR i IN (SELECT AGENCY.ID FROM ...
- radware 相关(alteon产品)
目录 术语 组网 配置思路 IP 规划 负载均衡器的IP规划 服务器的IP规划 登陆设备 简单命令行配置 Main Menu 管理IP 确认设备版本 telnet/sshd/http enable 网 ...
- 2021-09-27 Core初步实战
中间件Progarm的定义添加Logging public static IHostBuilder CreateHostBuilder(string[] args) => Host.Create ...
- 信创啊,信创。Solon 的 war 包,现在同时支持 jakarta.servlet(及 javax.servlet)容器了!
Solon 是个神奇的项目,不是基于 Servlet 的.但是又很支持 Servlet,尤其是 war 包.打起来还挺方便的. 如果你是做信创的(听说,很多信创项目是用 war 部署到 tomcat ...
- 如何使用iptables防火墙模拟远程服务超时
前言 超时,应该是程序员很不爱处理的一种状态.当我们调用某服务.某个中间件.db时,希望对方能快速回复,正确就正常,错误就错误,而不是一直不回复.目前在后端领域来说,如java领域,调用服务时以同步阻 ...
- P1880 [NOI1995] 石子合并 题解
区间DP. 首先将其复制一遍(因为是环),也就是经典的破环成链. 设 \(f[i][j]\) 表示将 \(i\) 到 \(j\) 段的石子合并需要的次数. 有 \[f[i][j] = 0(i = j) ...
- jsp+servlet实战项目
第一步:新建maven项目,项目中添加dao,entity,service,servlet,util包第二步:导入依赖 第三步:数据库建表 第四步:entity实体包(疯转) 第五步:在util工具包 ...
- 使用 KubeBlocks 为 K8s 提供稳如老狗的数据库服务
原文链接:https://forum.laf.run/d/994 大家好!今天这篇文章主要向大家介绍 Sealos 的数据库服务.在 Sealos 上数据库后端服务由 KubeBlocks 提供,为用 ...
- 文盘Rust -- 生命周期问题引发的 static hashmap 锁
2021年上半年,撸了个rust cli开发的框架,基本上把交互模式,子命令提示这些cli该有的常用功能做进去了.项目地址:https://github.com/jiashiwen/interactc ...