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 添加超时策略的更多相关文章

  1. springBoot2.0+redis+fastJson+自定义注解实现方法上添加过期时间

    springBoot2.0集成redis实例 一.首先引入项目依赖的maven jar包,主要包括 spring-boot-starter-data-redis包,这个再springBoot2.0之前 ...

  2. Springboot2.0整合Redis(注解开发)

    一. pom.xm文件引入对应jar包 <dependency> <groupId>org.springframework.boot</groupId> <a ...

  3. SpringBoot2.0+Mybatis-Plus3.0+Druid1.1.10 一站式整合

    SpringBoot2.0+Mybatis-Plus3.0+Druid1.1.10 一站式整合 一.先快速创建一个springboot项目,其中pom.xml加入mybatis-plus 和druid ...

  4. SpringBoot2.0.3 + SpringSecurity5.0.6 + vue 前后端分离认证授权

    新项目引入安全控制 项目中新近添加了Spring Security安全组件,前期没怎么用过,加之新版本少有参考,踩坑四天,终完成初步解决方案.其实很简单,Spring Security5相比之前版本少 ...

  5. spring-boot-2.0.3应用篇 - shiro集成

    前言 上一篇:spring-boot-2.0.3源码篇 - 国际化,讲了如何实现国际化,实际上我工作用的模版引擎是freemaker,而不是thymeleaf,不过原理都是相通的. 接着上一篇,这一篇 ...

  6. SpringBoot2.0 基础案例(04):定时任务和异步任务的使用方式

    一.定时任务 1.基本概念 按照指定时间执行的程序. 2.使用场景 数据分析 数据清理 系统服务监控 二.同步和异步 1.基本概念 同步调用 程序按照代码顺序依次执行,每一行程序都必须等待上一行程序执 ...

  7. springBoot2.0 配置shiro实现权限管理

    一.前言 基于上一篇springBoot2.0 配置 mybatis+mybatisPlus+redis 这一篇加入shiro实现权限管理 二.shiro介绍 2.1 功能特点 Shiro 包含 10 ...

  8. SpringBoot2.0 基础案例(13):基于Cache注解模式,管理Redis缓存

    本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.Cache缓存简介 从Spring3开始定义Cache和Cac ...

  9. springboot2.0整合logback日志(详细)

    <div class="post"> <h1 class="postTitle"> springboot2.0整合logback日志(详 ...

随机推荐

  1. 前端面试的那些事儿(1)~JavaScript 原始数据类型

    前言 自我总结面试常问的一些细节,方便不断回顾与补充.第一次发表文章,如有问题或不足之处望及时指出. JavaScript 原始数据类型 1.1 基础数据类型 7大基础数据类型 boolean nul ...

  2. Jmeter与LoadRunner的比较

    一.与Loadrunner的比较相似点 1.Jmeter的架构跟loadrunner原理一样, 都是通过中间代理,监控&收集并发客户端发现的指令,把他们生成脚本,再发送到应用服务器,再监控服务 ...

  3. 0day学习笔记(2)--函数调用

    函数调用过程 调用函数操作 函数参数入栈(在当前函数栈帧),从左至右或从右至左视情况而定 一般为从右至左 mov 地址,参数 的一个操作并不直接pop而是定位到地址将参数传递进去 call offse ...

  4. php的echo 和 return的区别

    来源:https://blog.csdn.net/ljfphp/article/details/76718635 项目中碰到的问题,本来是想在控制器直接return $xml的($xml是一段xml格 ...

  5. Visual Studio 2015 + Windows 2012 R2, c++/cli Array::Sort() 抛出异常

    在Windows7上编译就是正常. 可见Windows2012 R2缺少了一些东西. 另外,有一个现象一样,但原因不一样的 https://stackoverflow.com/questions/46 ...

  6. Enjoy the game

    这真的只是一道规律题 我找到规律了但是因为数据太大了我超时了 我们现在来看一下这道题 牛牛战队的三个队员在训练之余会自己口胡了一些题当做平时的益智游戏.有一天牛可乐想出了一个小游戏给另外两名队员玩,游 ...

  7. jenkins及Maven介绍

    一.环境介绍 随着软件开发需求及复杂度的不断提高,团队开发成员之间如何更好地协同工作以确保软件开发的质量已经慢慢成为开发过程中不可回避的问题.Jenkins自动化部署可以解决集成.测试.部署等重复性的 ...

  8. C语言实现数组循环左移

    c语言实现数组左移: 例如输入: 8 3 1 2 3 4 5 6 7 8 输出: 4 5 6 7 8 1 2 3 #include <stdio.h> int main(int argc, ...

  9. Crash日志解析

    当应用程序崩溃时,会创建一个崩溃报告,这对于了解导致崩溃的原因非常有用.本文档包含有关如何表示,理解和解释崩溃报告的基本信息. 1.介绍 2.获取崩溃和低内存报告 3.象征性的奔溃报告 1.位码(bi ...

  10. falsk-web 表单

    web 表单 回顾 在上一章节中,我们定义了一个简单的模板,使用占位符来虚拟了暂未实现的部分,比如用户以及文章等. 在本章我们将要讲述应用程序的特性之一–表单,我们将会详细讨论如何使用 web 表单. ...