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日志(详 ...
随机推荐
- C# LINQ查询之对象
LINQ是一组查询技术的统称,其主要思想是将各种查询功能直接集成到C#语言中,可以对 对象.XML文档.SQL数据库.外部应用程序等进行操作. 这里面讲的简单的几个子句, 必须以from子句开头,以s ...
- iview使用之怎样给Page组件添加跳转按钮
在项目开发过程中,我们会经常遇到使用分页的表格,然而在ivieiw中,我们通常只能使用Page组件自带的功能,如下图: 切换每页条数这些基本的功能都不说了,有时候我们需要在输入框里输入想要跳转到的页数 ...
- gloo基本知识
Architechture(架构) Gloo通过Envoy XDS gRPC API来动态更新Envoy配置, 更方便的控制Envoy Proxy, 并保留扩展性..本质是一个Envoy xDS配置翻 ...
- Mac安装aws-cli全过程,通过命令行上传文件到aws s3协议服务器
第一次使用aws,首先查询了各种资料,我第一步需要做的是安装aws-cli,而安装aws-cli之前需要安装python3,当然你安装python3之前你还需要安装homebrew,当然我正在安装的过 ...
- jdk1.7和jdk1.8在接口方面的改动
1.JDK7及其之前,接口中都是抽象方法,且不能出现static方法 2.接口的变量都是public final static 全局静态常量,无变化 3.接口中可以添加非抽象方法(static),通过 ...
- centos7在命令行下安装图形界面
yum groupinstall "GNOME Desktop" "Graphical Administration Tools" ln -sf /lib/sy ...
- Apache Rewrite实现URL的跳转和域名跳转
Apache Rewrite实现URL的跳转和域名跳转 Rewirte主要的功能就是实现URL的跳转,它的正则表达式是基于Perl语言.可基 于服务器级的(httpd.conf)和目录级的 (.h ...
- CRS-0184 Cannot communicate with the CRS daemon
事件背景 rman清理脚本异常.导致磁盘空间爆满(一个环境变量没有设置正确) 释放磁盘空间,进行rman清理 之后,领导把实例重启,但是ams实例没有关闭 环境 系统 : AIX 数据库: Oracl ...
- 2019-2020-1 20199325《Linux内核原理与分析》第三周作业
在实验楼当中进行实验3的实践:主要是针对cpu占用分配,使用时间片轮转算法进行分配 在Shell命令当中输入如下代码: $ cd ~/LinuxKernel/linux-3.9.4 $ rm -rf ...
- Windows环境,获取当前线程的ID,GetCurrentThreadId
GetCurrentThreadId 打印格式:0x%08lx 头文件:processthreadsapi.h (include Windows Server 2003, Windows Vista, ...