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工具类 ...
随机推荐
- js事件2-事件兼容问题
对于不同的浏览器,事件响应会有一定的不同,所以我们为了更好的用户效果,必须要考虑好事件兼容性问题. 为了兼容不同的浏览器,我们可以自己编写一个事件对象,通过它的事件添加函数和删除函数来给元素添加/删除 ...
- autoRoll_UpDown()|上下滚动函数|无缝|自动(自带demo)
autoRoll_UpDown函数 function autoRoll_UpDown($domObj,speed){ //$domObj 外围容器的jQuery式元素对象 speed 滚动速度,单位毫 ...
- Windows Automation API 3.0 Overview
https://www.codemag.com/article/0810042 While general accessibility requirements (such as font color ...
- modbus-poll和modbus-slave工具的学习使用——modbus协议功能码3的解析(及欧姆龙温控器调试笔记)
最近的项目中使用到了欧姆龙的温控器,里面有很多的通信方式,我们使用的常见的modbus——RTU方式,其他方式我们不使用,其中通信手册上面有很多通信的实例,欧姆龙modbus还区分4字节模式和2字节模 ...
- docker 启动失败 Job for docker.service failed because the control process exited with error code. See "systemctl status docker.service" and "journalctl -xe" for details.
CentOS7安装docker,安装成功后,启动失败 提示: 我们可以看到此处它提示是Failed to start Docker Application Container Engine. 于是在网 ...
- &和&& 每天学一点linux
原文:http://www.cnblogs.com/TianFang/archive/2013/01/23/2872645.html & 放在启动参数后面表示设置此进程为后台进程 默认情况下, ...
- python - django 解决 templates 模板语言语法冲突
# 在使用某个框架时发现语法与Django的模板语法冲突了,于是找到解决方案: {% verbatim %} // 被 verbatim 包裹起来的地方是不会被 django 渲染的 {% endve ...
- round.606.div2
A. Happy Birthday, Polycarp! 这个题意我确实没有看懂. 沃日,我懂了,我感觉我似乎都能切掉这题. B. Make Them Odd 这个我也能看懂.
- UFUN函数 UF_CSYS函数 UF_MTX函数(如何创建坐标系);
// (题目不够长,写在这了) // 函数有 // UF_MTX3_initialize,UF_CSYS_create_matrix,UF_CSYS_create_csys,UF_CSYS_ask_c ...
- smashing 开源方便的dashboard 试用
smashing 一个方便的dashboard 工具,是在Shopify/dashing 上维护的一个版本因为原有的官方团队不在维护了 smashing 使用简单,提供了脚手架同时也有好多人开发了一些 ...