org.springframework.cache.interceptor.CacheAspectSupport Java Examples
The following examples show how to use
org.springframework.cache.interceptor.CacheAspectSupport.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RedissonSpringAutoConfiguration.java From redisson-spring-boot with Apache License 2.0 | 5 votes |
/** * 声明 RedissonSpringCacheManager * * 由于 #{@link CacheAutoConfiguration} 的加载顺序在本类之前,并且其默认会注册一个 #{@link org.springframework.boot.autoconfigure.cache.SimpleCacheConfiguration}, * 所以这里将 beanName 设置为 'cacheManager',目的是为了覆盖掉默认的 cacheManager(Spring 有一种机制来保证 bean 的优先级,详情请查看 * #{@link DefaultListableBeanFactory#registerBeanDefinition(String, BeanDefinition)}) * * 为什么不先加载本类呢?因为 CacheAutoConfiguration 中有一些功能是我们需要的,如果先加载本类,那么 RedissonSpringCacheManager注册成功后, * CacheAutoConfiguration 将不会加载,因为其加载条件是不存在 CacheManager * * @param redisson redisson 客户端 * @return RedissonSpringCacheManager cacheManager */ @Bean @ConditionalOnClass(CacheManager.class) @ConditionalOnBean(CacheAspectSupport.class) @ConditionalOnMissingBean(RedissonSpringCacheManager.class) @ConditionalOnProperty(prefix = "spring.redisson.cache-manager", name = "enabled", havingValue = "true", matchIfMissing = true) public RedissonSpringCacheManager cacheManager(RedissonClient redisson) { log.info("redisson cache-manager init..."); RedissonCacheManagerProperties redissonCacheManagerProperties = redissonSpringProperties.getCacheManager(); // 获取 ConfigMap // CacheConfig: // ttl 过期时间,key 写入一定时间后删除,相当于 GuavaCache 的 expireAfterWrite // maxIdleTime 最大空闲时间,key 一定时间内没有被访问后删除,相当于 GuavaCache 的 expireAfterAccess // maxIdleTime 最大数量,达到一定数量后删除一部分 key,基于 LRU 算法 Map<String, CacheConfig> config = redissonCacheManagerProperties.getConfigs(); // 创建 CacheManager,ConfigMap 会转换为 Cache RedissonSpringCacheManager redissonSpringCacheManager = new RedissonSpringCacheManager(redisson, config); // RedissonSpringCacheManager 中的 dynamic 属性默认为 true,即获取不存在的 Cache 时,Redisson 创建一个永不过期的 Cache 以供使用 // 个人认为这样不合理,会导致滥用缓存,所以 starter 中 dynamic 的默认值为 false,当获取不存在的 Cache 时会抛出异常 // 当然,你也可以手动开启 dynamic 功能 if (!redissonCacheManagerProperties.isDynamic()) { redissonSpringCacheManager.setCacheNames(redissonCacheManagerProperties.getConfigs().keySet()); } if (redissonCacheManagerProperties.getCodec() != null) { redissonSpringCacheManager.setCodec(redissonCacheManagerProperties.getCodec().getInstance()); } if (redissonCacheManagerProperties.getConfigLocation() != null && !redissonCacheManagerProperties.getConfigLocation().isEmpty()) { redissonSpringCacheManager.setConfigLocation(redissonCacheManagerProperties.getConfigLocation()); } redissonSpringCacheManager.setAllowNullValues(redissonCacheManagerProperties.isAllowNullValues()); // 用户自定义配置,拥有最高优先级 redissonSpringCacheManagerCustomizers.forEach(customizer -> customizer.customize(redissonSpringCacheManager)); return redissonSpringCacheManager; }