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. Jmeter--Plugins Manager安装及常用的插件介绍

    jmeter 客户端 内置的插件管理工具Plugins Manager 1.下载地址:https://jmeter-plugins.org/install/Install/ 2.将下载的文件拷贝的你的 ...

  2. python 中自带的堆模块heapq

    import heapq my_heap = [] #使用列表保存数据 #网列表中插入数据,优先级使用插入的内容来表示,就是一个比较大小的操作,越大优先级越高 heapq.heappush(my_he ...

  3. 微信小程序 POST传值跳坑

    来源:https://www.cnblogs.com/ordinaryk/p/8430462.html 加这个就行了: header : { 'content-type': 'application/ ...

  4. java零基础自学网站分享

    俗话说工欲善其事,必先利其器,今天给大家分享一个java自学网站:how2j,这是一个有十年淘宝工作经验大牛的制作的网站,距离现在已经有三四年了,这个网站包含的知识非常的多,从基础的环境变量配置一直到 ...

  5. Scala教程之:scala的参数

    文章目录 默认参数值 命名参数 scala的参数有两大特点: 默认参数值 命名参数 默认参数值 在Scala中,可以给参数提供默认值,这样在调用的时候可以忽略这些具有默认值的参数. def log(m ...

  6. 5.Python是怎么解释的?

    Python是怎么解释的? Python language is an interpreted language. Python program runs directly from the sour ...

  7. [bzoj5329] P4606 [SDOI2018]战略游戏

    P4606 [SDOI2018]战略游戏:广义圆方树 其实会了圆方树就不难,达不到黑,最多算个紫 那个转换到圆方树上以后的处理方法,画画图就能看出来,所以做图论题一定要多画图,并把图画清楚点啊!! 但 ...

  8. muduo网络库源码学习————线程安全

    线程安全使用单例模式,保证了每次只创建单个对象,代码如下: Singleton.h // Use of this source code is governed by a BSD-style lice ...

  9. 最小点覆盖(König定理)

    König定理是一个二分图中很重要的定理,它的意思是,一个二分图中的最大匹配数等于这个图中的最小点覆盖数.如果你还不知道什么是最小点覆盖,我也在这里说一下:假如选了一个点就相当于覆盖了以它为端点的所有 ...

  10. 区间dp 例题

    D - 石子合并问题--直线版 HRBUST - 1818 这个题目是一个区间dp的入门,写完这个题目对于区间dp有那么一点点的感觉,不过还是不太会. 注意这个区间dp的定义 dp[i][j] 表示的 ...