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工具类 ...
随机推荐
- MyBatis mapper.xml中SQL处理小于号与大于号
这种问题在xml处理sql的程序中经常需要我们来进行特殊处理. 其实很简单,我们只需作如下替换即可避免上述的错误: < <= > >= & ' " < ...
- CSS float 父元素高度自适应
<html> <head><title></title><style type="text/css">*{margin: ...
- wp_list_categories如何去掉前面的categories
我们可以通过<?php wp_list_categories(); ?>来调用所有分类,但是在前面会出现一个categories,对强迫症患者来说就是钉子,那就把它去掉吧,顺便让自己更熟悉 ...
- Keil5创建基于RTX的STM32工程(转载+自己的体会)
转载自:https://blog.csdn.net/u011976086/article/details/54342447#commentBox 之前使用过ucos,freertos,但是这个keil ...
- iOS应用代码注入防护
在应用开发过程中,我们不仅仅需要完成正常的业务逻辑,考虑应用性能.代码健壮相关的问题,我们有时还需要考虑到应用安全的问题.那么应用安全的问题涉及到很多方面.比如防止静态分析的,代码混淆.逻辑混淆:防止 ...
- 删除ubuntu旧内核的方法
https://www.jianshu.com/p/75edb9a5fbab 磁盘满了 需要清理系统盘 1,先用uname -a 查看当前内核版本: uname -a Linux 10-9-37-13 ...
- LSTM的神经元个数
小书匠深度学习 目录: 1.LSTM简单介绍 2.简单假设样例 3.神经元分析 3.1忘记门层 3.2细胞状态 3.3输出层 3.4总结 4.测试 1.LSTM简单介绍 LSTM在时间上展开 红框从左 ...
- mac 搭建Java Spring boot 环境(eclipse)
安装 下载安装Springboot 安装完成后,创建项目 1. 2. 3. 完成创建!
- 第08组 Alpha冲刺(5/6)
队名:955 组长博客:https://www.cnblogs.com/cclong/p/11898112.html 作业博客:https://edu.cnblogs.com/campus/fzu/S ...
- 如何在ProXmoX VE 下虚拟机安装 黑群晖 DSM 6.1.6
ProXmoX 官方下载地址: https://www.proxmox.com/en/downloads Proxmox是一个非常优秀的开源虚拟化环境,集诸多优点于一身:开源免费,稳定可靠,架构简洁, ...