JetCache注解配置全局默认CacheType

实现方案

  1. 重写 jetcache下的ConfigMap类,处理 CacheType
  2. 建议修改平台缓存的 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())) {
/*自定义校验使用的 CacheType,这里需要注意,只需要判断默认情况下的缓存类型,非默认情况下的默认为开发者主动修改*/
if (CacheConsts.DEFAULT_CACHE_TYPE.equals(cac.getCacheType())
&& "system.demo".equals(cac.getArea())
/*当前配置的 cacheType 不为 null*/
&& 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
/**
* 自定义缓存注解存储类<br>
* 替代 jetcache 的默认 ConfigMap<br>
* 实现了全局配置默认缓存方式<br>
*
* @author maxzhao
* @date 2021-09-24 15:30
*/
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 cacheType = autoAreaCacheType.get(cac.getArea());
if (config.getCachedAnnoConfig() != null
&& CacheConsts.DEFAULT_CACHE_TYPE.equals(config.getCachedAnnoConfig().getCacheType())
/*当前配置的 cacheType 不为 null*/
&& 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:
# 不同域的默认缓存方式 JetCache默认方式为 remote
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 {

/**
* 不同域的默认缓存方式<br>
* JetCache默认方式为 remote,
* 这里需要根据 area 划分,统一替换 单个 area 的 CacheType
*/
private Map<String, CacheType> autoAreaCacheType = new HashMap<>(0);
/**
* 自定义配置 cacheName 与 过期时间
*/
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
/*激活 Cached*/
@EnableMethodCache(basePackages = "com.gt")
/*激活 CreateCache注解*/
@EnableCreateCacheAnnotation
@EnableCaching(mode = AdviceMode.PROXY)
@Configuration
public class StartCacheConfig {


/**
* CacheProperties 的 bean 没有默认加载,所有要手动加载一下
*
* @return
*/
@Bean
@ConfigurationProperties(prefix = "spring.cache")
public CacheProperties getCacheProperties() {
return new CacheProperties();
}

/**
* 自定义的缓存配置
*
* @return BootCacheProperties
*/
@Bean
@ConfigurationProperties(prefix = "gt.boot.cache")
public BootCacheProperties bootCacheProperties() {
return new BootCacheProperties();
}

@Bean
public RedisCacheConfiguration cacheConfiguration() {
return CacheConfigurationUtil.getRedisCacheConfigurationWithTtl(Duration.ZERO);
}

/**
* 解决 @Cache 方法上注解的默认 CacheType
* @param bootCacheProperties 自定义参数
* @return ConfigMap 对象
*/
@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
public ConfigMap jetcacheConfigMap(BootCacheProperties bootCacheProperties) {
return new CustomCacheConfigMap(bootCacheProperties);
}
}

本文地址: https://github.com/maxzhao-it/blog/post/784e58da/