作者:Eddy 历史版本:1 最后编辑:龚清 更新时间:2024-11-20 15:41
如何使用j2cache进行缓存开发?
适用版本:v3.2.0+;
配置j2cache配置文件
- caffeine.properties
示例:
default = 1000, 30m
ibps.context = 10000, 10m
ibps.sys = 10000, 1h
#
上面的配置定义了三个缓存 Region ,分别是:
默认缓存,大小是 1000 个对象,TTL 是 30 分钟
ibps.context 缓存,大小是 10000 个对象,TTL 是 10 分钟
ibps.sys 缓存,大小是 10000 个对象,TTL 是 1 个小时
default 是当我们调用如下方法时:
public void set(String region, String key, Object value)
如果我们传入的 region 参数(假设为:region1)没有在 caffeine.properties 中定义的话,那 J2Cache 会自动创建一个名为 region1 的缓存 Region,其配置和 default 的配置一致。
- j2cache.properties
示例:
# 缓存广播方法,支持 jgroups/redis/lettuce/rabbitmq/rocketmq
j2cache.broadcast = lettuce
# 第一二级缓存实现方式,一级支持 ehcache/caffeine, 二级支持 redis/lettuce/memcached
j2cache.L1.provider_class = caffeine
j2cache.L2.provider_class = lettuce
# 在redis缓存数据中启用/禁用ttl(如果禁用,redis中的对象将永远不会过期,默认值:true)
j2cache.sync_ttl_to_redis = true
# 默认情况下是否缓存空对象(默认为false)
j2cache.default_cache_null_object = true
# 缓存序列化方式,支持 fst/kryo/fastjson/java/json/fse
j2cache.serialization = kryo
# caffeine配置文件
caffeine.properties = /config/caffeine.properties
# 缓存名称空间可选,默认为[空]
# redis存储模式 支持 generic/hash
# 发布/订阅频道名称
lettuce.namespace =
lettuce.storage = generic
lettuce.channel = j2cache.platform
lettuce.channel = j2cache.platform 需要注意,不同的服务名称需要唯一
重写仓库、领域中cache相关方法
以DefaultPartyUser的仓库和领域类为例子
仓库
- getInternalCacheName()
public String getInternalCacheName() {
return CacheKeyConstants.Region.REGION_IBPS_PARTY; // 返回对应的region名称
}
- getInternalType()
public String getInternalType() {
return "DefaultPartyUser"; // 返回对应的类型名称,一般为类的名称即可
}
- getInternal(P p)
public void getInternal(DefaultPartyUserPo p) {
if(BeanUtils.isEmpty(p)) {
return;
}
// 跳过一级缓存
partyEmployeeRepository.setSkipInternal();
PartyEmployeePo employeePo = partyEmployeeRepository.get(p.getId());
// 恢复一级缓存
partyEmployeeRepository.removeSkipInternal();
if(BeanUtils.isEmpty(employeePo)) {
return;
}
p.setFullname(employeePo.getName());
p.setName(employeePo.getName());
p.setStatus(employeePo.getStatus());
p.setGender(employeePo.getGender());
p.setMobile(employeePo.getMobile());
p.setEmail(employeePo.getEmail());
p.setAddress(employeePo.getAddress());
p.setQq(employeePo.getQq());
p.setPhoto(employeePo.getPhoto());
p.setWcAccount(employeePo.getWcAccount());
}
领域
- getInternalCacheName()
public String getInternalCacheName() {
return CacheKeyConstants.Region.REGION_IBPS_PARTY; // 返回对应的region名称
}
- getInternalType()
public String getInternalType() {
return "DefaultPartyUser"; // 返回对应的类型名称,一般为类的名称即可
}
分区常量位置
- CacheKeyConstants
public class CacheKeyConstants {
public static final String OAUTH = "oauth";
public static final String LOCK_RETRY = "user.lock.retry";
public static final String CURRENT_USER = "current.user";
public static final String CURRENT_ORG = "current.org";
public static final String CURRENT_POSITION = "current.position";
public static final String CURRENT_IP = "current.ip";
public static final String CURRENT_ACCESS_TOKEN = "current.access.token";
public static final String CURRENT_TENANT_ID = "current.tenant.id";
public static final String CURRENT_TENANT_IDS = "current.tenant.ids";
public static final String CACHE_PREFIX = "constants:cache";
public static final String CACHE_KEY_PREFIX = "cache";
private CacheKeyConstants() {
}
public static class Properties {
public static final String PROPERTY_DEFAULT = "cache.default";
public static final String PROPERTY_IBPS_SYS = "cache.ibps.sys";
public static final String PROPERTY_IBPS_RES = "cache.ibps.res";
public static final String PROPERTY_IBPS_CLIENT = "cache.ibps.client";
public static final String PROPERTY_IBPS_CATEGORY = "cache.ibps.category";
public static final String PROPERTY_IBPS_DESKTOP = "cache.ibps.desktop";
public static final String PROPERTY_IBPS_GROUP = "cache.ibps.group";
public static final String PROPERTY_IBPS_ATTR = "cache.ibps.attr";
public static final String PROPERTY_IBPS_LEVEL = "cache.ibps.level";
public static final String PROPERTY_IBPS_PARTY = "cache.ibps.party";
public static final String PROPERTY_IBPS_BO = "cache.ibps.bo";
public static final String PROPERTY_IBPS_FORM = "cache.ibps.form";
public static final String PROPERTY_IBPS_DATA_TPL = "cache.ibps.data.tpl";
public static final String PROPERTY_IBPS_CODE = "cache.ibps.code";
public static final String PROPERTY_IBPS_BPM_DEF = "cache.ibps.bpm.def";
public static final String PROPERTY_IBPS_BPM_AUTH = "cache.ibps.bpm.auth";
public static final String PROPERTY_IBPS_BPM_RUNNING = "cache.ibps.bpm.running";
public static final String PROPERTY_IBPS_BPM_ACTIVITI = "cache.ibps.bpm.activiti";
public static final String PROPERTY_IBPS_AUTHORIZE = "cache.ibps.authorize";
public static final String PROPERTY_IBPS_TOKEN = "cache.ibps.token";
public static final String PROPERTY_IBPS_REFRESH = "cache.ibps.refresh";
public Properties() {
}
}
public static class Region {
public static final String REGION_DEFAULT = "default";
public static final String REGION_IBPS_CONTEXT = "ibps.context";
public static final String REGION_IBPS_SYS = "ibps.sys";
public static final String REGION_IBPS_RES = "ibps.res";
public static final String REGION_IBPS_CLIENT = "ibps.client";
public static final String REGION_IBPS_CATEGORY = "ibps.category";
public static final String REGION_IBPS_DESKTOP = "ibps.desktop";
public static final String REGION_IBPS_GROUP = "ibps.group";
public static final String REGION_IBPS_ATTR = "ibps.attr";
public static final String REGION_IBPS_LEVEL = "ibps.level";
public static final String REGION_IBPS_PARTY = "ibps.party";
public static final String REGION_IBPS_TENANT = "ibps.tenant";
public static final String REGION_IBPS_BO = "ibps.bo";
public static final String REGION_IBPS_FORM = "ibps.form";
public static final String REGION_IBPS_FORM_PRINT = "ibps.form.print";
public static final String REGION_IBPS_DATA_TPL = "ibps.data.tpl";
public static final String REGION_IBPS_CODE = "ibps.code";
public static final String REGION_IBPS_BPM_DEF = "ibps.bpm.def";
public static final String REGION_IBPS_BPM_AUTH = "ibps.bpm.auth";
public static final String REGION_IBPS_BPM_RUNNING = "ibps.bpm.running";
public static final String REGION_IBPS_BPM_ACTIVITI = "ibps.bpm.activiti";
public static final String REGION_IBPS_AUTHORIZE = "ibps.authorize";
public static final String REGION_IBPS_TOKEN = "ibps.token";
public static final String REGION_IBPS_REFRESH = "ibps.refresh";
public Region() {
}
}
}