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日志(详 ...
随机推荐
- testNG 断言
testNG提供一个Assert类,来判断输出值是否与预期值一致,Assert常用的方法有: Assert.assertEquals():此方法可以有两个参数值,也可以有3个参数值,参数的顺序是 ac ...
- HuggingFace-transformers系列的介绍以及在下游任务中的使用
内容介绍 这篇博客主要面向对Bert系列在Pytorch上应用感兴趣的同学,将涵盖的主要内容是:Bert系列有关的论文,Huggingface的实现,以及如何在不同下游任务中使用预训练模型. 看过这篇 ...
- Java 中正则表达式使用
正则表达式基本用法: 测试代码: @Test public void test01() { String str = "adsfd##4324"; // 创建正则表达式对象 Pat ...
- Hbase详细架构图解
@ 目录 主要组件 数据模型 注意:Hbase是依赖zookeeper和hdfs的,需要启动zk和hdfs. 主要组件 Zookeeper: HBase 通过 Zookeeper 来做 Master ...
- 2019-2020-1 20199328《Linux内核原理与分析》第五周作业
实验要求: 实验步骤: 这里以20号系统调用getpid为例进行实验,该函数的功能为:返回当前进程标识. getpid.c代码: 查看实验结果: 当前进程pid为:31042. 在C语言中编入汇编代码 ...
- 理解分布式一致性:拜占庭容错与PBFT
理解分布式一致性:拜占庭容错与PBFT 拜占庭问题 拜占庭容错BFT PBFT(Practical Byzantine Fault Tolerance) why 3f+1 ? PBFT 的优点 PBF ...
- 什么是最好的在线UML软件工具?
在线UML软件工具允许您创建UML图表,而UML绘图工具可帮助维护您的建模工件并促进不同图表中元素的可重用性.一些UML建模工具还提供复杂的建模功能,例如模型转换,报告,代码工程等. 如果您正在寻找U ...
- 本周ASP.NET英文技术文章推荐[02/03 - 02/16]:MVC、Visual Studio 2008、安全性、性能、LINQ to JavaScript、jQuery...
摘要 继续坚持,继续推荐.本期共有9篇文章: 最新的ASP.NET MVC框架开发计划 Visual Studio 2008 Web开发相关的Hotfix发布 ASP.NET安全性教程系列 ASP.N ...
- USACO Training Section 1.2 [USACO1.2]方块转换 Transformations
题目描述 一块N x N(1<=N<=10)正方形的黑白瓦片的图案要被转换成新的正方形图案.写一个程序来找出将原始图案按照以下列转换方法转换成新图案的最小方式: 1:转90度:图案按顺时针 ...
- 禅道部署(基于 Linux)
1. 查看 Linux 服务器是 32位 还是 64位 的 getconf LONG_BIT 2. 禅道开源版安装包下载 下载站点1:# wget http://sourceforge.net/pro ...