注解实现SpringCache自定义失效时间

SpringCache是一个很方便的缓存框架,但是官方提供的缓存的配置只有全局的缓存失效时间,没有针对某个命名空间做配置,因为工作上业务的关系需要针对某一个缓存做单独的控制,所有想了个办法来实现。大概分为以下步骤:

1)自定义注解

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.concurrent.TimeUnit;
/**
* 缓存失效的注解,目前只支持在类级别上有效
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface CacheExpire {
/**
* 失效时间,默认是60
* @return
*/
public long ttl() default 60L; /**
* 单位,默认是秒
* @return
*/
public TimeUnit unit() default TimeUnit.SECONDS;
}

2)CacheManagerHelper获得注解的值

import com.spboot.utils.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component; import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit; /**
* CacheManager的辅助类
*/
@Component
public class CacheManagerHelper implements ApplicationContextAware { private static ApplicationContext applicationContext;
private static Map<String , Duration> CACHE_DURATION = new HashMap<>(); @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
CacheManagerHelper.applicationContext = applicationContext;
} /**
* 根据cacheName获得对应的duration值
* @param name
* @return
*/
public static Duration getByKey(String name) {
return findAllCacheBean().get(name);
} /**
* 找到所有的被 @CacheConfig 和 @CacheExpire 修饰的类对象
*/
public static Map<String , Duration> findAllCacheBean() {
if(CACHE_DURATION.size() == 0) {
Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(CacheConfig.class);
if(beansWithAnnotation != null && beansWithAnnotation.size() > 0) {
for (Map.Entry<String, Object> entry : beansWithAnnotation.entrySet()) {
Object proxyObject = entry.getValue(); // 代理类
Object realObject = BeanUtils.getTarget(proxyObject); //获得真实的对象
CacheExpire cacheExpire = realObject.getClass().getAnnotation(CacheExpire.class);
if(null != cacheExpire) {
CacheConfig cacheConfig = realObject.getClass().getAnnotation(CacheConfig.class);
String[] cacheNames = cacheConfig.cacheNames();
long convert = TimeUnit.SECONDS.convert(cacheExpire.ttl(), cacheExpire.unit());
Duration duration = Duration.ofSeconds(convert);
for (String cacheName : cacheNames) {
CACHE_DURATION.put(cacheName, duration);
}
}
}
}
}
return CACHE_DURATION;
}
}

3)修改源码org.springframework.data.redis.cache.RedisCache

修改这里是为了改变每次存储之前redis的key的ttl值,通过上面自定义的CacheManagerHelper来获得。

修改源码位置:

  • org.springframework.data.redis.cache.RedisCache#put
  • org.springframework.data.redis.cache.RedisCache#putIfAbsent
  • 添加的方法:
    • getDuration(java.lang.String, org.springframework.data.redis.cache.RedisCacheConfiguration)

因为代码太长,只放出了被修改过的代码,其余的保持不变:

/**
* 如果该命名空间使用了@CacheExpire注解就是用自定义的失效时间,否则使用默认的
* @param name
* @param cacheConfiguration
* @return
*/
private Duration getDuration(String name, RedisCacheConfiguration cacheConfiguration) {
Duration duration = CacheManagerHelper.getByKey(name);
if(null != duration) { // 如果当前命名空间配置了自定义失效时间,使用配置值
return duration;
}
return cacheConfig.getTtl(); // 否则使用全局的配置值
} /*
* (non-Javadoc)
* @see org.springframework.cache.Cache#put(java.lang.Object, java.lang.Object)
*/
@Override
public void put(Object key, @Nullable Object value) { Object cacheValue = preProcessCacheValue(value); if (!isAllowNullValues() && cacheValue == null) { throw new IllegalArgumentException(String.format(
"Cache '%s' does not allow 'null' values. Avoid storing null via '@Cacheable(unless=\"#result == null\")' or configure RedisCache to allow 'null' via RedisCacheConfiguration.",
name));
} // 修改的
cacheWriter.put(name, createAndConvertCacheKey(key), serializeCacheValue(cacheValue), getDuration(name, cacheConfig));
// 默认的
// cacheWriter.put(name, createAndConvertCacheKey(key), serializeCacheValue(cacheValue), cacheConfig.getTtl());
} /*
* (non-Javadoc)
* @see org.springframework.cache.Cache#putIfAbsent(java.lang.Object, java.lang.Object)
*/
@Override
public ValueWrapper putIfAbsent(Object key, @Nullable Object value) { Object cacheValue = preProcessCacheValue(value); if (!isAllowNullValues() && cacheValue == null) {
return get(key);
} // 修改后的
byte[] result = cacheWriter.putIfAbsent(name, createAndConvertCacheKey(key), serializeCacheValue(cacheValue), getDuration(name, cacheConfig));
// 默认的
// byte[] result = cacheWriter.putIfAbsent(name, createAndConvertCacheKey(key), serializeCacheValue(cacheValue),
// cacheConfig.getTtl()); if (result == null) {
return null;
} return new SimpleValueWrapper(fromStoreValue(deserializeCacheValue(result)));
}

4)全局redis cache config

import java.time.Duration;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer; import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper; @Configuration
@EnableCaching // 开启spring的缓存
public class CacheConfig { /**
* 自定义得缓存管理器
* @param redisConnectionFactory
* @return
*/
@Primary
@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { //初始化一个RedisCacheWriter
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory); // key 序列化方式
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// value的序列化机制
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = jackson2JsonRedisSerializer(); // 配置
RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofHours(1)) // 默认1个小时失效时间
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(stringRedisSerializer)) // 设置 k v 序列化机制
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)); //初始化RedisCacheManager
RedisCacheManager cacheManager = new RedisCacheManager(redisCacheWriter, defaultCacheConfig); return cacheManager;
} Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer() {
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
return jackson2JsonRedisSerializer;
} }

5)使用注解

假设在一个controller使用注解,例如:

@CacheExpire(ttl = 10, unit = TimeUnit.SECONDS) // 自定义注解,10秒钟就过期
@CacheConfig(
cacheNames = "testApiService")
@RestController
public class TestApi {
@Cacheable
@GetMapping("/api/redis")
public Map<String,String> data() {
Map<String,String> map = new HashMap<String, String>();
map.put("k1", "v1");
map.put("k2", "v2");
map.put("k3", "v3");
return map;
}
}

具体的代码地址

如此一来就实现了使用注解控制缓存失效时间,这里还有优化的空间,比如注解精细到方法粒度的控制,使用aop来替代等,后面有时间再优化实践吧。

注解实现SpringCache自定义失效时间的更多相关文章

  1. java 日志脱敏框架 sensitive-v0.0.4 系统内置常见注解,支持自定义注解

    项目介绍 日志脱敏是常见的安全需求.普通的基于工具类方法的方式,对代码的入侵性太强.编写起来又特别麻烦. 本项目提供基于注解的方式,并且内置了常见的脱敏方式,便于开发. 特性 基于注解的日志脱敏. 可 ...

  2. 深入JAVA注解(Annotation):自定义注解 (转)

    原文出自:http://blog.csdn.net/yjclsx/article/details/52101922 一.基础知识:元注解 要深入学习注解,我们就必须能定义自己的注解,并使用注解,在定义 ...

  3. Java日志脱敏框架 sensitive-v0.0.4 系统内置常见注解,支持自定义注解

    项目介绍 日志脱敏是常见的安全需求.普通的基于工具类方法的方式,对代码的入侵性太强.编写起来又特别麻烦. 本项目提供基于注解的方式,并且内置了常见的脱敏方式,便于开发. 特性 基于注解的日志脱敏. 可 ...

  4. Java注解Annotation与自定义注解详解

    Java注解简介 开发中经常使用到注解,在项目中也偶尔会见到过自定义注解,今天就来探讨一下这个注解是什么鬼,以及注解的应用场景和如何自定义注解. 下面列举开发中常见的注解 @Override:用于标识 ...

  5. SpringCache自定义过期时间及自动刷新

    背景前提 阅读说明(十分重要) 对于Cache和SpringCache原理不太清楚的朋友,可以看我之前写的文章:Springboot中的缓存Cache和CacheManager原理介绍 能关注Spri ...

  6. Java注解教程:自定义注解示例,利用反射进行解析

    Java注解能够提供代码的相关信息,同时对于所注解的代码结构又没有直接影响.在这篇教程中,我们将学习Java注解,如何编写自定义注解,注解的使用,以及如何使用反射解析注解. 注解是Java 1.5引入 ...

  7. Java注解教程及自定义注解

    Java注解提供了关于代码的一些信息,但并不直接作用于它所注解的代码内容.在这个教程当中,我们将学习Java的注解,如何定制注解,注解的使用以及如何通过反射解析注解. Java1.5引入了注解,当前许 ...

  8. 160621、Java注解教程及自定义注解

    Java注解提供了关于代码的一些信息,但并不直接作用于它所注解的代码内容.在这个教程当中,我们将学习Java的注解,如何定制注解,注解的使用以及如何通过反射解析注解. Java1.5引入了注解,当前许 ...

  9. 注解:@interface 自定义注解的语法

      自定义注解: 使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口,由编译程序自动完成其他细节.在定义注解时,不能继承其他的注解或接口 ...

随机推荐

  1. Apache Hudi典型应用场景知多少?

    1.近实时摄取 将数据从外部源如事件日志.数据库提取到Hadoop数据湖 中是一个很常见的问题.在大多数Hadoop部署中,一般使用混合提取工具并以零散的方式解决该问题,尽管这些数据对组织是非常有价值 ...

  2. 感觉shopex现在的升级方式太慢了

    我是说产品的更新,484,485是一个经典的版本,那时候免费,shopex 系统市场占用率很高.但是485以后呢,只有小版本的更新,fxw ,ekd 都是改进版本吧,没用特别大幅度的更新.5年前,10 ...

  3. babel转码时generator的regeneratorRuntime

    今天写generator函数时发现出错:regeneratorRuntime. 在stackoverflow网友说需是本地babel软件包没有安装完全. package.json: "dev ...

  4. 判断对象oStringObject是否为String

    1.操作符 (1)typeof操作符 格式:result=typeof variable 返回值: undefined 值未定义 boolean 布尔值 string 字符串 number 数值 ob ...

  5. 我的excel是2003版本的,里边有sheet1、sheet2两个工作表,当使用GetOleDbSchemaTable获取表Schema时,结果是4个,分别为: sheet1 sheet1$ sheet2 sheet2$

    原帖地址:http://bbs.csdn.net/topics/310230098 ---------------------------------------------------------- ...

  6. 新概念英语三 新东方主讲Lesson1

    新概念二 Lesson95 词汇 ①get a shock 吓了一跳,得到一个惊喜 例:his wife got a shock get into a such mess 这么不幸搞得一片狼籍弄得这样 ...

  7. 常用的code snipper

    Xcode 10.2.1 版本,自动代码提示功能延迟很大,很多情况下代码提示甚至会无效,但是code snippet 唤起的响应很快,从提高效率的情况考虑,即使系统提供的代码提示制作成code sni ...

  8. 【MySQL】详细说下MySQL删除数据的过程是什么样的?

    drop table 这里先介绍一下[InnoDB]存储表空间概念: Innodb存储引擎,可将所有的数据库数据存放于[ibdata1]的共享表空间:也可以将每张表存放于独立的.idb文件的独立表空间 ...

  9. SpringMVC笔记总结

    文章所有代码见:gitee 1.回顾MVC 1.1.什么是MVC MVC是模型(Model).视图(View).控制器(Controller)的简写,是一种软件设计规范. 是将业务逻辑.数据.显示分离 ...

  10. Python 绘制全球疫情地图

    国内疫情得到控制后,我就没怎么再关心过疫情,最近看到一条新闻,全球疫情累计确诊人数已经突破 500w 大关,看到这个数字我还是有点吃惊的. 思来想去,还是写一篇全球疫情的分析的文章,本文包括网络爬虫. ...