用过spring cache的朋友应该会知道,Spring Cache默认是不支持在@Cacheable上添加过期时间的,虽然可以通过配置缓存容器时统一指定。形如
@Bean public CacheManager cacheManager( @SuppressWarnings("rawtypes") RedisTemplate redisTemplate) { RedisCacheManager cacheManager= new RedisCacheManager(redisTemplate); cacheManager.setDefaultExpiration(60); MapexpiresMap = new HashMap<>(); expiresMap.put("customUser",30L); cacheManager.setExpires(expiresMap); return cacheManager; }
复制
但有时候我们会更习惯通过注解指定过期时间。今天我们就来聊一下如何扩展@Cacheable实现缓存自动过期以及缓存即将到期自动刷新
2
实现注解缓存过期前置知识
SpringCache包含两个顶级接口,Cache和CacheManager,通过CacheManager可以去管理一堆Cache。因此我们要扩展@Cacheable,就脱离不了对Cache和CacheManager进行扩展
其次要实现过期时间,首先是引入的缓存产品,他本身就要支持过期时间,比如引入的缓存为ConcurrentHashMap,他原本就是不支持过期时间,如果要扩展,就要非常耗费精力实现
3
01
形如下
@Cacheable(cacheNames = "customUser#30", key = "#id")
复制
通过#分隔,#后面部分代表过期时间(单位为秒)
实现逻辑步骤为:
1、自定义缓存管理器并继承RedisCacheManager,同时重写createRedisCache方法
示例:
public class CustomizedRedisCacheManager extends RedisCacheManager { public CustomizedRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) { super(cacheWriter, defaultCacheConfiguration); } @Override protected RedisCache createRedisCache(String name, RedisCacheConfiguration cacheConfig) { String[] array = StringUtils.delimitedListToStringArray(name, "#"); name = array[0]; if (array.length > 1) { long ttl = Long.parseLong(array[1]); cacheConfig = cacheConfig.entryTtl(Duration.ofSeconds(ttl)); } return super.createRedisCache(name, cacheConfig); } }
复制
2、将默认的缓存管理器改成我们自定义的缓存管理器
示例:
@EnableCaching @Configuration public class CacheConfig { @Bean public CacheManager cacheManager() { RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofDays(1)); CustomizedRedisCacheManager redisCacheManager = new CustomizedRedisCacheManager(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory()), defaultCacheConfig); return redisCacheManager; } }
复制
通过如上2个步骤,即可实现缓存过期
02
第一种方法的实现是简单,但缺点是语义不直观,因此得做好宣导以及wiki,不然对于新人来说,他可能都不知道cacheName用#分割是代表啥意思
方法二的实现逻辑步骤如下
1、自定义注解LybGeekCacheable
@Target({ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented @Cacheable(cacheManager = CacheConstant.CUSTOM_CACHE_MANAGER,keyGenerator = CacheConstant.CUSTOM_CACHE_KEY_GENERATOR) public @interface LybGeekCacheable { @AliasFor(annotation = Cacheable.class,attribute = "value") String[] value() default {}; @AliasFor(annotation = Cacheable.class,attribute = "cacheNames") String[] cacheNames() default {}; @AliasFor(annotation = Cacheable.class,attribute = "key") String key() default ""; @AliasFor(annotation = Cacheable.class,attribute = "keyGenerator") String keyGenerator() default ""; @AliasFor(annotation = Cacheable.class,attribute = "cacheResolver") String cacheResolver() default ""; @AliasFor(annotation = Cacheable.class,attribute = "condition") String condition() default ""; @AliasFor(annotation = Cacheable.class,attribute = "unless") String unless() default ""; @AliasFor(annotation = Cacheable.class,attribute = "sync") boolean sync() default false; long expiredTimeSecond() default 0; long preLoadTimeSecond() default 0; }
复制
大部分注解和@Cacheable保持一致,新增expiredTimeSecond缓存过期时间以及缓存自动刷新时间preLoadTimeSecond
2、自定义缓存管理器并继承RedisCacheManager并重写loadCaches和createRedisCache
public class CustomizedRedisCacheManager extends RedisCacheManager implements BeanFactoryAware { private MapinitialCacheConfigurations; private RedisTemplate cacheRedisTemplate; private RedisCacheWriter cacheWriter; private DefaultListableBeanFactory beanFactory; private RedisCacheConfiguration defaultCacheConfiguration; protected CachedInvocation cachedInvocation; public CustomizedRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, Map initialCacheConfigurations,RedisTemplate cacheRedisTemplate) { super(cacheWriter, defaultCacheConfiguration, initialCacheConfigurations); this.initialCacheConfigurations = initialCacheConfigurations; this.cacheRedisTemplate = cacheRedisTemplate; this.cacheWriter = cacheWriter; this.defaultCacheConfiguration = defaultCacheConfiguration; //采用spring事件驱动亦可 //EventBusHelper.register(this); } public Map getInitialCacheConfigurations() { return initialCacheConfigurations; } @Override protected Collection loadCaches() { List caches = new LinkedList<>(); for (Map.Entry entry : getInitialCacheConfigurations().entrySet()) { caches.add(createRedisCache(entry.getKey(), entry.getValue())); } return caches; } @Override public RedisCache createRedisCache(String name, @Nullable RedisCacheConfiguration cacheConfig) { CustomizedRedisCache customizedRedisCache = new CustomizedRedisCache(name, cacheWriter, cacheConfig != null ? cacheConfig : defaultCacheConfiguration); return customizedRedisCache; } }
复制
3、在spring bean初始化完成后,设置缓存过期时间,并重新初始化缓存。
Component @Slf4j public class CacheExpireTimeInit implements SmartInitializingSingleton, BeanFactoryAware { private DefaultListableBeanFactory beanFactory; @Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { this.beanFactory = (DefaultListableBeanFactory)beanFactory; } @Override public void afterSingletonsInstantiated() { MapbeansWithAnnotation = beanFactory.getBeansWithAnnotation(Component.class); if(MapUtil.isNotEmpty(beansWithAnnotation)){ for (Object cacheValue : beansWithAnnotation.values()) { ReflectionUtils.doWithMethods(cacheValue.getClass(), method -> { ReflectionUtils.makeAccessible(method); boolean cacheAnnotationPresent = method.isAnnotationPresent(LybGeekCacheable.class); if(cacheAnnotationPresent){ LybGeekCacheable lybGeekCacheable = method.getAnnotation(LybGeekCacheable.class); CacheHelper.initExpireTime(lybGeekCacheable); } }); } CacheHelper.initializeCaches(); } }
复制
注: 为啥要重新初始化缓存,主要是为了一开始默认的是没设置缓存过期,重新初始化是为了设置过期时间。为啥调用initializeCaches()这个方法,看下官方描述就知道了
/** * Initialize the static configuration of caches. *Triggered on startup through {@link #afterPropertiesSet()}; * can also be called to re-initialize at runtime. * @since 4.2.2 * @see #loadCaches() */ public void initializeCaches() { Collection extends Cache> caches = loadCaches(); synchronized (this.cacheMap) { this.cacheNames = Collections.emptySet(); this.cacheMap.clear(); Set
cacheNames = new LinkedHashSet<>(caches.size()); for (Cache cache : caches) { String name = cache.getName(); this.cacheMap.put(name, decorateCache(cache)); cacheNames.add(name); } this.cacheNames = Collections.unmodifiableSet(cacheNames); } }
复制
他就是在运行的时候,可以重新初始化缓存
4、将默认的缓存管理器改成我们自定义的缓存管理器
@Bean(CacheConstant.CUSTOM_CACHE_MANAGER) public CacheManager cacheManager(RedisConnectionFactory connectionFactory,RedisTemplate cacheRedisTemplate) { RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory); RedisCacheConfiguration defaultCacheConfig = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofDays(1)); MapinitialCacheConfiguration = new HashMap<>(); return new CustomizedRedisCacheManager(redisCacheWriter,defaultCacheConfig,initialCacheConfiguration,cacheRedisTemplate); }
复制
5、测试
@LybGeekCacheable(cacheNames = "customUser", key = "#id",expiredTimeSecond = 30) public User getUserFromRedisByCustomAnno(String id){ System.out.println("get user with id by custom anno: 【" + id + "】"); Faker faker = Faker.instance(Locale.CHINA); return User.builder().id(id).username(faker.name().username()).build(); }
复制
@Test public void testCacheExpiredAndPreFreshByCustom() throws Exception{ System.out.println(userService.getUserFromRedisByCustomAnno("1")); }
复制
以上就是扩展缓存过期的实现主要方式了,接下来我们来聊一下缓存自动刷新
4
缓存自动刷新
一般来说,当缓存失效时,请求就会打到后端的数据库上,此时可能就会造成缓存击穿现象。因此我们在缓存即将过期时主动刷新缓存,提高缓存的命中率,进而提高性能。
spring4.3的@Cacheable提供了一个sync属性。当缓存失效后,为了避免多个请求打到数据库,系统做了一个并发控制优化,同时只有一个线程会去数据库取数据其它线程会被阻塞
5
缓存即将到期自动刷新
1、封装缓存注解对象CachedInvocation
/** * @description: 标记了缓存注解的方法类信息,用于主动刷新缓存时调用原始方法加载数据 */ @Data @AllArgsConstructor @NoArgsConstructor @Builder public final class CachedInvocation { private CacheMetaData metaData; private Object targetBean; private Method targetMethod; private Object[] arguments; public Object invoke() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException { final MethodInvoker invoker = new MethodInvoker(); invoker.setTargetObject(this.getTargetBean()); invoker.setArguments(this.getArguments()); invoker.setTargetMethod(this.getTargetMethod().getName()); invoker.prepare(); return invoker.invoke(); } }
复制
2、编写一个获取即将到期时间参数切面,并进行事件发布调用对象CachedInvocation
@Component @Aspect @Slf4j @Order(2) public class LybGeekCacheablePreLoadAspect { @Autowired private ApplicationContext applicationContext; @SneakyThrows @Around(value = "@annotation(lybGeekCacheable)") public Object around(ProceedingJoinPoint proceedingJoinPoint,LybGeekCacheable lybGeekCacheable){ buildCachedInvocationAndPushlish(proceedingJoinPoint,lybGeekCacheable); Object result = proceedingJoinPoint.proceed(); return result; } private void buildCachedInvocationAndPushlish(ProceedingJoinPoint proceedingJoinPoint,LybGeekCacheable lybGeekCacheable){ Method method = this.getSpecificmethod(proceedingJoinPoint); String[] cacheNames = getCacheNames(lybGeekCacheable); Object targetBean = proceedingJoinPoint.getTarget(); Object[] arguments = proceedingJoinPoint.getArgs(); KeyGenerator keyGenerator = SpringUtil.getBean(CacheConstant.CUSTOM_CACHE_KEY_GENERATOR,KeyGenerator.class); Object key = keyGenerator.generate(targetBean, method, arguments); CachedInvocation cachedInvocation = CachedInvocation.builder() .arguments(arguments) .targetBean(targetBean) .targetMethod(method) .metaData(CacheMetaData.builder() .cacheNames(cacheNames) .key(key) .expiredTimeSecond(lybGeekCacheable.expiredTimeSecond()) .preLoadTimeSecond(lybGeekCacheable.preLoadTimeSecond()) .build() ) .build(); // EventBusHelper.post(cachedInvocation); applicationContext.publishEvent(cachedInvocation); }
复制
3、自定义缓存管理器,接收CachedInvocation
示例
public class CustomizedRedisCacheManager extends RedisCacheManager implements BeanFactoryAware { //@Subscribe @EventListener private void doWithCachedInvocationEvent(CachedInvocation cachedInvocation){ this.cachedInvocation = cachedInvocation; }
复制
4、自定义cache并重写get方法
@Slf4j public class CustomizedRedisCache extends RedisCache { private ReentrantLock lock = new ReentrantLock(); public CustomizedRedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfiguration cacheConfig) { super(name, cacheWriter,cacheConfig); } @Override @Nullable public ValueWrapper get(Object key) { ValueWrapper valueWrapper = super.get(key); CachedInvocation cachedInvocation = CacheHelper.getCacheManager().getCachedInvocation(); long preLoadTimeSecond = cachedInvocation.getMetaData().getPreLoadTimeSecond(); if(ObjectUtil.isNotEmpty(valueWrapper) && preLoadTimeSecond > 0){ String cacheKey = createCacheKey(key); RedisTemplate cacheRedisTemplate = CacheHelper.getCacheManager().getCacheRedisTemplate(); Long ttl = cacheRedisTemplate.getExpire(cacheKey, TimeUnit.SECONDS); if(ObjectUtil.isNotEmpty(ttl) && ttl <= preLoadTimeSecond){ log.info(">>>>>>>>>>> cacheKey:{}, ttl: {},preLoadTimeSecond: {}",cacheKey,ttl,preLoadTimeSecond); ThreadPoolUtils.execute(()->{ lock.lock(); try{ CacheHelper.refreshCache(super.getName()); }catch (Exception e){ log.error("{}",e.getMessage(),e); }finally { lock.unlock(); } }); } } return valueWrapper; } }
复制
5、缓存即将到期主动刷新缓存方法
public static void refreshCache(String cacheName){ boolean isMatchCacheName = isMatchCacheName(cacheName); if(isMatchCacheName){ CachedInvocation cachedInvocation = getCacheManager().getCachedInvocation(); boolean invocationSuccess; Object computed = null; try { computed = cachedInvocation.invoke(); invocationSuccess = true; } catch (Exception ex) { invocationSuccess = false; log.error(">>>>>>>>>>>>>>>>> refresh cache fail",ex.getMessage(),ex); } if (invocationSuccess) { Cache cache = getCacheManager().getCache(cacheName); if(ObjectUtil.isNotEmpty(cache)){ Object cacheKey = cachedInvocation.getMetaData().getKey(); cache.put(cacheKey, computed); log.info(">>>>>>>>>>>>>>>>>>>> refresh cache with cacheName-->【{}】,key--> 【{}】 finished !",cacheName,cacheKey); } } } }
复制
6、测试
@LybGeekCacheable(cacheNames = "customUserName", key = "#username",expiredTimeSecond = 20,preLoadTimeSecond = 15) public User getUserFromRedisByCustomAnnoWithUserName(String username){ System.out.println("get user with username by custom anno: 【" + username + "】"); Faker faker = Faker.instance(Locale.CHINA); return User.builder().id(faker.idNumber().valid()).username(username).build(); }
复制
@Test public void testCacheExpiredAndPreFreshByCustomWithUserName() throws Exception{ System.out.println(userService.getUserFromRedisByCustomAnnoWithUserName("zhangsan")); TimeUnit.SECONDS.sleep(5); System.out.println("sleep 5 second :" + userService.getUserFromRedisByCustomAnnoWithUserName("zhangsan")); TimeUnit.SECONDS.sleep(10); System.out.println("sleep 10 second :" + userService.getUserFromRedisByCustomAnnoWithUserName("zhangsan")); TimeUnit.SECONDS.sleep(5); System.out.println("sleep 5 second :" + userService.getUserFromRedisByCustomAnnoWithUserName("zhangsan")); }
复制
6
总结
本文主要介绍了如何基于spring @Cacheable扩展实现缓存自动过期时间以及缓存即将到期自动刷新。
不知道有没有朋友会有疑问,为啥@Cacheable不提供一个ttl属性,毕竟也不是很难。在我看来,spring更多提供的是一个通用的规范和标准,如果定义的缓存,本身不支持ttl,你在@Cacheable里面配置ttl就不合适了,有时候实现一个组件或者框架,考虑的是不是能不能实现,而是有没有必要实现,更多是一种权衡和取舍