实现方案
- 重写
jetcache
下的ConfigMap
类,处理 CacheType
。
- 建议修改平台缓存的
area
(为了适配一个项目多种缓存系统)。
具体实现
平台中的缓存注解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| @Service public class CategoryItemServiceImpl extends ServiceImpl<CategoryItemMapper, CategoryItem> implements ICategoryItemService {
@Override @Cached(name = "categoryItemList:parentId:", key = "#parentId+''", area = "systemo_demo") public List<CategoryItemDTO> loadCategoryItemListByParentId(Long parentId) { LambdaQueryWrapper<CategoryItem> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.select( CategoryItem::getId, CategoryItem::getParentId, CategoryItem::getItemCode, CategoryItem::getName) .eq(CategoryItem::getParentId, parentId) .eq(CategoryItem::getDelStatus, BaseConstant.NO) .orderByAsc(CategoryItem::getSortIndex); List list = this.list(queryWrapper); return categoryConvert.convertToCategoryItemDTOList(list); } }
|
systemo_demo
是平台内的系统常量
ConfigMap
处理默认缓存方式
具体实现请看附录中的 CustomCacheConfigMap
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class CustomCacheConfigMap extends ConfigMap { @Override public void putByMethodInfo(String key, CacheInvokeConfig config) { methodInfoMap.put(key, config); CachedAnnoConfig cac = config.getCachedAnnoConfig(); if (cac != null && !CacheConsts.isUndefined(cac.getName())) { if (CacheConsts.DEFAULT_CACHE_TYPE.equals(cac.getCacheType()) && "system.demo".equals(cac.getArea()) && cacheConfig.getCacheType() != null) {
cac.setCacheType(cacheConfig.getCacheType()); } cacheNameMap.put(cac.getArea() + "_" + cac.getName(), config); } } }
|
附
自定义的 CustomCacheConfigMap
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
public class CustomCacheConfigMap extends ConfigMap {
private final BootCacheProperties bootCacheProperties;
public CustomCacheConfigMap(BootCacheProperties bootCacheProperties) { this.bootCacheProperties = bootCacheProperties; }
private final ConcurrentHashMap<String, CacheInvokeConfig> methodInfoMap = new ConcurrentHashMap<>();
private final ConcurrentHashMap<String, CacheInvokeConfig> cacheNameMap = new ConcurrentHashMap<>();
@Override public void putByMethodInfo(String key, CacheInvokeConfig config) { methodInfoMap.put(key, config); CachedAnnoConfig cac = config.getCachedAnnoConfig(); Map<String, CacheType> autoAreaCacheType = Optional.ofNullable(bootCacheProperties.getAutoAreaCacheType()).orElse(new HashMap<>(0));
if (cac != null && !CacheConsts.isUndefined(cac.getName())) { CacheType cacheType = autoAreaCacheType.get(cac.getArea()); if (config.getCachedAnnoConfig() != null && CacheConsts.DEFAULT_CACHE_TYPE.equals(config.getCachedAnnoConfig().getCacheType()) && cacheType != null) { config.getCachedAnnoConfig().setCacheType(cacheType); } cacheNameMap.put(cac.getArea() + "_" + cac.getName(), config); } }
@Override public CacheInvokeConfig getByMethodInfo(String key) { return methodInfoMap.get(key); }
@Override public CacheInvokeConfig getByCacheName(String area, String cacheName) { return cacheNameMap.get(area + "_" + cacheName); } }
|
BootCacheProperties
配置
配置
1 2 3 4 5 6 7 8 9 10
| gt: boot: cache: auto-area-cache-type: default: REMOTE otherArea: REMOTE autoConfig: { user: 60ms }
|
实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @Data public class BootCacheProperties {
private Map<String, CacheType> autoAreaCacheType = new HashMap<>(0);
private Map<String, Duration> autoConfig = new HashMap<>(0);
}
|
注册 Bean
在配置类中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| @EnableMethodCache(basePackages = "com.gt")
@EnableCreateCacheAnnotation @EnableCaching(mode = AdviceMode.PROXY) @Configuration public class StartCacheConfig {
@Bean @ConfigurationProperties(prefix = "spring.cache") public CacheProperties getCacheProperties() { return new CacheProperties(); }
@Bean @ConfigurationProperties(prefix = "gt.boot.cache") public BootCacheProperties bootCacheProperties() { return new BootCacheProperties(); }
@Bean public RedisCacheConfiguration cacheConfiguration() { return CacheConfigurationUtil.getRedisCacheConfigurationWithTtl(Duration.ZERO); }
@Bean @Role(BeanDefinition.ROLE_INFRASTRUCTURE) public ConfigMap jetcacheConfigMap(BootCacheProperties bootCacheProperties) { return new CustomCacheConfigMap(bootCacheProperties); } }
|
本文地址: https://github.com/maxzhao-it/blog/post/784e58da/