springboot:自定义缓存注解,实现生存时间需求
需求背景:在使用springbot cache时,发现@cacheabe不能设置缓存时间,导致生成的缓存始终在redis中。
环境:springboot 2.1.5 + redis
解决办法:利用AOP自定义注解,用SPEL来解释key表达式。
1.定义注解
package com.test.entity.util.annotation.cache; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface MyCacheable { /**
* 缓存key
*
* @return
*/
String key(); /**
* 是否缓存空值
*
* @return
*/
boolean cacheNull() default false; /**
* 生存时间,单位是秒,默认为-1(永不过期)
*
* @return
*/
int ttl() default -1; /**
* 生存状态
*
* true:每访问一次,将刷新存活时间
*
* false:不刷新存活时间,时间一到就清除
*
* @return
*/
boolean state() default true;
}
2.实现AOP
package com.test.service.aop; import java.lang.reflect.Method; import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.context.annotation.Lazy;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component; import com.test.entity.util.annotation.cache.MyCacheable;
import com.test.util.redis.RedisUtil; @Aspect
@Component
@Lazy(false)
public class AspectCacheable { private Logger log = LoggerFactory.getLogger(AspectCacheable.class); @Autowired
private RedisUtil redisUtil; /**
* 定义切入点
*/
@Pointcut("@annotation(com.test.entity.util.annotation.cache.MyCacheable)")
private void cut() {
// do nothing
} /**
* 环绕通知
*/
@Around("cut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
// 读取缓存注解
MyCacheable myCacheable = this.getMethodAnnotation(joinPoint);
// 读取类注解
CacheConfig cacheConfig = this.getClassAnnotation(joinPoint);
// 获取方法传入参数
Object[] params = joinPoint.getArgs();
// 获得解释之后的key
String strKey = this.getKey(cacheConfig, myCacheable, params);
log.debug("解释之后的key:{}", strKey);
// 在方法执行前判断是否存在缓存
Object object = this.getCache(strKey, myCacheable.state(), myCacheable.ttl());
if (object == null) {
// 创建缓存
object = this.createCache(joinPoint, strKey, myCacheable);
}
return object;
} /**
* 获取方法中声明的注解
*
* @param joinPoint
* @return
* @throws NoSuchMethodException
*/
private MyCacheable getMethodAnnotation(JoinPoint joinPoint) throws NoSuchMethodException {
// 获取方法名
String methodName = joinPoint.getSignature().getName();
// 反射获取目标类
Class<?> targetClass = joinPoint.getTarget().getClass();
// 拿到方法对应的参数类型
Class<?>[] parameterTypes = ((MethodSignature) joinPoint.getSignature()).getParameterTypes();
// 根据类、方法、参数类型(重载)获取到方法的具体信息
Method objMethod = targetClass.getMethod(methodName, parameterTypes);
// 拿到方法定义的注解信息
return objMethod.getDeclaredAnnotation(MyCacheable.class);
} /**
* 获取类中声明的注解
*
* @param joinPoint
* @return
* @throws NoSuchMethodException
*/
private CacheConfig getClassAnnotation(JoinPoint joinPoint) throws NoSuchMethodException {
// 反射获取目标类
Class<?> targetClass = joinPoint.getTarget().getClass();
return targetClass.getDeclaredAnnotation(CacheConfig.class);
} /**
* 读取现有缓存
*
* @param key
* 实际key,非key表达式
* @param state
* 是否刷新存活时间
* @return
*/
private Object getCache(String key, boolean state, int ttl) {
Object obj = redisUtil.get(key);
if (obj != null && state && ttl != -1) {
// 存在缓存&每次访问重置TTL&非永不过期
// 每次访问后重新刷新TTL,还原为原来值
redisUtil.expire(key, ttl);
}
return obj;
} /**
* 解析key表达式,得到实际的key
*
* @param myCacheable
* @param params
* @return
*/
private String getKey(CacheConfig cacheConfig, MyCacheable myCacheable, Object[] params) {
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext ctx = new StandardEvaluationContext();
// 获得原始key的表达式
String strSourceKey = myCacheable.key();
int intSeq = -1;
String strSearchSeq = null;
int intStartPos = 0;
// 用SPEL解析表达式
while (++intSeq < params.length) {
strSearchSeq = "#p" + intSeq;
intStartPos = StringUtils.indexOf(strSourceKey, strSearchSeq, intStartPos);
if (intStartPos < 0) {
break;
} else {
ctx.setVariable("p" + intSeq, params[intSeq]);
}
}
// 执行表达式
Expression expression = parser.parseExpression(strSourceKey);
String strKey = expression.getValue(ctx).toString();
// 拼接上缓存名称,spring cache会加上前缀,是在CacheConfig中配置的。
if (cacheConfig != null) {
strKey = cacheConfig.cacheNames()[0] + ":" + strKey;
}
return strKey;
} /**
* 创建缓存
*
* @param joinPoint
* @param strKey
* @param myCacheable
* @return
* @throws Throwable
*/
private Object createCache(ProceedingJoinPoint joinPoint, String strKey, MyCacheable myCacheable) throws Throwable {
// 没有缓存则执行目标方法
// 获取目标方法的名称
String methodName = joinPoint.getSignature().getName();
log.debug("目标执行方法:{}", methodName);
// 执行源方法
Object object = joinPoint.proceed();
if (object != null) {
// 设置缓存
redisUtil.set(strKey, object);
redisUtil.expire(strKey, myCacheable.ttl());
} else {
// 判断是否缓存null
if (myCacheable.cacheNull()) {
redisUtil.set(strKey, object);
}
}
return object;
}
}
3.在类上应用注解
@Service
@Transactional
public class ServiceImplCoach implements ServiceCoach { private Logger log = LoggerFactory.getLogger(ServiceImplCoach.class); @Autowired
private DaoCoach daoCoach;
@MyCacheable(key = "'coachnum:'+#p0", ttl = 3600, state = false)
@Override
public EntityCoach select(String coachnum) {
EntityCoach entityCoach = null;
if (StringUtils.isNotBlank(coachnum)) {
try {
entityCoach = daoCoach.selectByPrimaryKey(coachnum);
} catch (Exception e) {
log.error("查询教练员发生错误:{}", e);
}
} else {
log.info("查询教练员,输入不符合要求");
}
return entityCoach;
} @CacheEvict(key = "'coachnum:'+#p0")
@Override
public EntityRes delete(String coachnum) throws Exception {
EntityRes entityRes = new EntityRes();
log.debug("删除教练员,id={}", coachnum);
if (StringUtils.isBlank(coachnum)) {
log.info("删除教练员,输入不符合要求。");
entityRes.setErrorcode(INVALID);
} else {
daoCoach.deleteByPrimaryKey(coachnum);
entityRes.setErrorcode(CODE_SUCC);
}
return entityRes;
} }
RedisUtil 是 redis操作公共类,大家可以用自己的。
springboot:自定义缓存注解,实现生存时间需求的更多相关文章
- SpringBoot自定义Condition注解
最近碰到个这样的需求,需要同一套代码适配个版本数据库(数据库不同,且部分表的字段及关联关系可能会不同),即这套代码配置不同的数据库都能跑.项目采用的框架为SpringBoot+Mybatis. ...
- SpringBoot开启缓存注解
https://blog.csdn.net/sanjay_f/article/details/47372967 https://www.cnblogs.com/lic309/p/4072848.htm ...
- SpringBoot Redis缓存 @Cacheable、@CacheEvict、@CachePut
文章来源 https://blog.csdn.net/u010588262/article/details/81003493 1. pom.xml <dependency> <gro ...
- Spring之缓存注解@Cacheable
https://www.cnblogs.com/fashflying/p/6908028.html https://blog.csdn.net/syani/article/details/522399 ...
- 详解Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用
https://blog.csdn.net/u012240455/article/details/80844361 注释介绍 @Cacheable @Cacheable 的作用 主要针对方法配置,能够 ...
- [技术博客] SPRINGBOOT自定义注解
SPRINGBOOT自定义注解 在springboot中,有各种各样的注解,这些注解能够简化我们的配置,提高开发效率.一般来说,springboot提供的注解已经佷丰富了,但如果我们想针对某个特定情景 ...
- 更加灵活的参数校验,Spring-boot自定义参数校验注解
上文我们讨论了如何使用@Min.@Max等注解进行参数校验,主要是针对基本数据类型和级联对象进行参数校验的演示,但是在实际中我们往往需要更为复杂的校验规则,比如注册用户的密码和确认密码进行校验,这个时 ...
- springboot + 拦截器 + 注解 实现自定义权限验证
springboot + 拦截器 + 注解 实现自定义权限验证最近用到一种前端模板技术:jtwig,在权限控制上没有用springSecurity.因此用拦截器和注解结合实现了权限控制. 1.1 定义 ...
- Springboot中使用自定义参数注解获取 token 中用户数据
使用自定义参数注解获取 token 中User数据 使用背景 在springboot项目开发中需要从token中获取用户信息时通常的方式要经历几个步骤 拦截器中截获token TokenUtil工具类 ...
随机推荐
- 项目Beta冲刺--5/7
项目Beta冲刺--5/7 作业要求 这个作业属于哪个课程 软件工程1916-W(福州大学) 这个作业要求在哪里 项目Beta冲刺 团队名称 基于云的胜利冲锋队 项目名称 云评:高校学生成绩综合评估及 ...
- Template Function
// TemplateFunction.cpp : Defines the entry point for the console application. // #include "std ...
- 客户端本地存储,web存储,localStorage
HTML5 LocalStorage 本地存储 说到本地存储,这玩意真是历尽千辛万苦才走到HTML5这一步,之前的历史大概如下图所示: 最早的Cookies自然是大家都知道,问题主要就是太小,大概也就 ...
- pkusc2019游记
Day0 早上 6:55 的高铁,6 点就起了,好困呜呜呜 去的路上跟 memset0 坐一起,突然发现雀魂还没停服,先雀了一局(居然拿了个 1 位还飞了一个人),与此同时 memset0 切了一道毒 ...
- MongoDB 聚合查询报错
1.Distinct聚合查询报错 db.users.distinct("uname") db.runCommand({"distinct":"user ...
- final和static语句
final关键字 final的概念 关键字final,final的意思为最终,不可变.final是个修饰符,它可以用来修饰类,类的成员,以及局部变量.不能修饰构造方法. final的特点 当final ...
- typedi 强大的javascript以及typescript 依赖注入框架
typedi 是typestack团队提供的依赖注入解决方案,对于typescript 我们可以使用注解的开发方式,官方的文档也比较详细 javascript 使用 基于函数的服务注入 var Ser ...
- 如何优化SpringBoot的项目的启动速度
日常开发SpringBoot项目启动类都用@SpringBootApplication,实际上它是下面三个注解的组合: @EnableAutoConfiguration: enable Spring ...
- 鸿蒙OS还有机会吗?
鸿蒙最初的定位是手机操作系统,那在市场已饱和.生态已成熟,Android地位已牢不可破的当下,能成功是小概率事件,就像PC时代无法撼动Windows地位一样. 好在鸿蒙调整定位定义为面向未来的IOT操 ...
- R 语言输入输出 读取命令函参数
输入数据 使用键盘输入数据 只能处理小样本,很少使用 在创建 data.txt 字符串之后,用函数 read.table() 创建数据框 data.1.这种方法可以让我们把数据嵌入到R代码中,此处切记 ...