SpringBoot2.0 @Cacheable 添加超时策略
SpringBoot2.0 @Cacheable 添加超时策略
逻辑比较简单,废话不多说,直接进入正题:
需求:SpringBoot 利用注解使缓存支持过期时间 (同@Cacheable @CachePut 等注解用法)
第一步:新建注解CacheExpire
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface CacheExpire { /**
* 缓存过期时间
*/
long expire() default 0; /**
* 仅当采用expire字段设置过期时间时生效
*/
TimeUnit timeUnit() default TimeUnit.SECONDS; /**
* 利用cron设置过期时间
*/
String cron() default ""; }
第二步:扩展CacheInterceptor拦截器
public class CacheExpireInterceptor extends CacheInterceptor {
private final StringRedisTemplate template;
public CacheExpireInterceptor(StringRedisTemplate template) {
this.template = template;
}
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Object invoke = super.invoke(invocation);
setExpire(invocation);
return invoke;
}
public void setExpire(MethodInvocation invocation) {
try {
Method method = invocation.getMethod();
CacheExpire expire = method.getAnnotation(CacheExpire.class);
if (expire != null && (expire.expire() > 0 || !StringUtils.isEmpty(expire.cron()))) {
Cacheable cacheable = method.getAnnotation(Cacheable.class);
Object key = getKeyGenerator().generate(invocation.getThis(), method, invocation.getArguments());
Set<String> names = new TreeSet<>();
names.addAll(Arrays.asList(cacheable.cacheNames()));
names.addAll(Arrays.asList(cacheable.value()));
for (String name : names) {
String tag = name + "::" + key;
if (expire.expire() > 0) {
template.expire(tag, expire.expire(), expire.timeUnit());
} else if (!StringUtils.isEmpty(expire.cron())) {
CronTrigger trigger = TriggerBuilder.newTrigger().withSchedule(CronScheduleBuilder.cronSchedule(expire.cron())).build();
Date date = new Date();
long time = trigger.getFireTimeAfter(date).getTime() - date.getTime();
if (time > 0) {
template.expire(tag, time, TimeUnit.MILLISECONDS);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
第三步:扩展ProxyCachingConfiguration配置文件
@Configuration
public class ProxyCachingExpireConfiguration extends ProxyCachingConfiguration { private final StringRedisTemplate template; public ProxyCachingExpireConfiguration(StringRedisTemplate template) {
this.template = template;
} @Bean
@Override
public CacheInterceptor cacheInterceptor() {
CacheInterceptor interceptor = new CacheExpireInterceptor(template);
interceptor.configure(this.errorHandler, this.keyGenerator, this.cacheResolver, this.cacheManager);
interceptor.setCacheOperationSource(this.cacheOperationSource());
return interceptor;
}
}
第四步:新建Service开始使用
@Service
public class TsetService { @CacheExpire(expire = 20)
@Cacheable(value = "test")
public Object get(Long id) {
return System.currentTimeMillis();
} }
第五步:Postman调用

继续惯例:欢迎交流,QQ:1107628852(加备注博客)
SpringBoot2.0 @Cacheable 添加超时策略的更多相关文章
- springBoot2.0+redis+fastJson+自定义注解实现方法上添加过期时间
springBoot2.0集成redis实例 一.首先引入项目依赖的maven jar包,主要包括 spring-boot-starter-data-redis包,这个再springBoot2.0之前 ...
- Springboot2.0整合Redis(注解开发)
一. pom.xm文件引入对应jar包 <dependency> <groupId>org.springframework.boot</groupId> <a ...
- SpringBoot2.0+Mybatis-Plus3.0+Druid1.1.10 一站式整合
SpringBoot2.0+Mybatis-Plus3.0+Druid1.1.10 一站式整合 一.先快速创建一个springboot项目,其中pom.xml加入mybatis-plus 和druid ...
- SpringBoot2.0.3 + SpringSecurity5.0.6 + vue 前后端分离认证授权
新项目引入安全控制 项目中新近添加了Spring Security安全组件,前期没怎么用过,加之新版本少有参考,踩坑四天,终完成初步解决方案.其实很简单,Spring Security5相比之前版本少 ...
- spring-boot-2.0.3应用篇 - shiro集成
前言 上一篇:spring-boot-2.0.3源码篇 - 国际化,讲了如何实现国际化,实际上我工作用的模版引擎是freemaker,而不是thymeleaf,不过原理都是相通的. 接着上一篇,这一篇 ...
- SpringBoot2.0 基础案例(04):定时任务和异步任务的使用方式
一.定时任务 1.基本概念 按照指定时间执行的程序. 2.使用场景 数据分析 数据清理 系统服务监控 二.同步和异步 1.基本概念 同步调用 程序按照代码顺序依次执行,每一行程序都必须等待上一行程序执 ...
- springBoot2.0 配置shiro实现权限管理
一.前言 基于上一篇springBoot2.0 配置 mybatis+mybatisPlus+redis 这一篇加入shiro实现权限管理 二.shiro介绍 2.1 功能特点 Shiro 包含 10 ...
- SpringBoot2.0 基础案例(13):基于Cache注解模式,管理Redis缓存
本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.Cache缓存简介 从Spring3开始定义Cache和Cac ...
- springboot2.0整合logback日志(详细)
<div class="post"> <h1 class="postTitle"> springboot2.0整合logback日志(详 ...
随机推荐
- 1. git 本地给远程仓库创建分支 三步法
命令如下: 1:本地创建分支dev 1 2 Peg@PEG-PC /D/home/myself/Symfony (master) $ git branch dev 2:下面是把本地分支提交到远程仓库 ...
- v&n赛 内存取证题解(已更新)
题目是一个raw的镜像文件 用volatility搜索一下进程 有正常的notepad,msprint,还有dumpit和truecrypt volatility -f mem.raw --profi ...
- [PHP] excel 的导入导出
其实excel导入导出挺简单的,导出最简单! 其原理都是把数据读出来,导出是从数据库中读出数据,导入是从文件读出数据! 导出写入文件,导入写入数据库! 但是在导入表的时候,用的是PHPExcel, 不 ...
- 【题解】P1291 百事世界杯之旅 - 期望dp
P1291 [SHOI2002]百事世界杯之旅 声明:本博客所有题解都参照了网络资料或其他博客,仅为博主想加深理解而写,如有疑问欢迎与博主讨论✧。٩(ˊᗜˋ)و✧*。 题目描述 "--在 \ ...
- 0day学习笔记(3)Windows定位API引起的惨案(原理)
段选择器FS与TEB WinNT内核下内存采用保护模式,段寄存器的意义与实模式汇编下的意义不同.另外,FS存的是段选择子,而不是实模式下的高16位基地址. FS寄存器指向当前活动线程的TEB结构(线程 ...
- python学习笔记(三)---字典
字典 在Python中,字典 字典 是一系列键 键-值对 值对 .每个键 键 都与一个值相关联,你可以使用键来访问与之相关联的值.与键相关联的值可以是数字.字符串.列表乃至字典.事实上,可将 任何Py ...
- Auth认证中的think_auth_rule type字段干嘛用的?
昨晚认真研究了一下这个类,设计的很巧妙,但是你说的这个字段,我认为应该是作者想加功能但还没写,在session判断的地方可以看到,type这个字段实际是对应的 1-实时验证,2登陆验证 ,显然,这个字 ...
- centos 7 安装更新php5.6
epel remi 什么的把我弄晕了 不深研这东西了,直接按步骤操作更新了. # yum install epel-release # rpm -ivh http://rpms.famillecol ...
- CG-CTF(4)
CG-CTF https://cgctf.nuptsast.com/challenges#Web 续上~ 第十六题:bypass again 代码分析: 当a不等于b,且a和b的md5值相同时,才会返 ...
- QT 获取可执行程序的路径
获取到生成.exe目录 QCoreApplication::applicationDirPath(); 获取当前工程目录 QDir::currentPath()