JetCache注解配置全局默认CacheType
前言
Jetcache
缓存可以使用的 CacheType
有 REMOTE
, LOCAL
, BOTH
;
注解 @Cached
在使用时,可以 CacheType
,如果不指定,则默认为 REMOTE
。
平台中的缓存注解在各个项目运行时,是无法修改的,并且平台中的缓存注解没有指定 CacheType
。
场景
满足不同项目对平台缓存存储方式的自定义配置。
场景一:没有缓存中间件,使用 caffeine
本地缓存。
场景二:有缓存中间件,比如Redis
,使用 remote.**.type=REDIS
目的
为了满足不同场景的需要。
实现以下功能:
- 在注解
CacheType=REMOTE
的情况下,实现默认缓存可以由REMOTE
改为LOCAL
、BOTH
。
风险
风险一
在用户需要把缓存注解默认为 CacheType=REMOTE
的数据,改为 BOTH
,并且需要主动指定缓存注解 CacheType=REMOTE
的数据,只存到 REMOTE
中。
解决风险
风险一
当前是实现了多个缓存系统(LOCAL
、REMOTE
),所以需要自定义的缓存中,指定缓存的 area
(默认为 default
)。
下面是 area
描述的原话:
1 | /** |
因为主要目的是修改平台默认的缓存方案,并不是项目中默认的缓存方案,所以只需要处理平台缓存 area
下的 CacheType
。
当平台缓存的 area
与 项目中缓存的 area
区分开来时,那么平台的默认缓存就很容易修改了。
实现
实现方案
- 重写
jetcache
下的ConfigMap
类,处理CacheType
。 - 建议修改平台缓存的
area
(为了适配一个项目多种缓存系统)。
具体实现
平台中的缓存注解
1 |
|
systemo_demo
是平台内的系统常量
ConfigMap
处理默认缓存方式
1 | public class CustomCacheConfigMap extends ConfigMap { |