概述
缓存的使用可以加快平台的运行速度,对于一些经常需要读取的信息,包括结构化的信息需要进行缓存。在流程模块中,因为流程定义其设计也存储是基于XML的,但在流程执行过程中,其定义是会转成一个JAVA的结构化数据,为了兼容其运行效率,我们对其流程定义的解析后的对象进行了缓存管理。当流程定义发生了变更时,还需要重新从缓存池中删除该流程定义的对象的缓存。
实现机制
平台使用redis的缓存机制,即通过流程定义前缀+defId进行缓存。
修改Activiti的缓存机制,在ActivitiConfig.java文件,增加以下配置:
//修改其流程缓存conf.setProcessDefinitionCache(new FlowDefCache());
FlowCache的实现如下:
package com.redxun.bpm.activiti.ext;import com.redxun.bpm.core.service.ConfigCacheUtil;import com.redxun.cache.CacheUtil;import lombok.SneakyThrows;import lombok.extern.slf4j.Slf4j;import org.activiti.engine.impl.persistence.deploy.DeploymentCache;/*** 流程定义缓存。** @author ray*/@Slf4jpublic class FlowDefCache implements DeploymentCache {private static final String REGION="bpm";@SneakyThrows@Overridepublic Object get(String key) {if(!this.contains(key)){return null;}Object o=CacheUtil.get(REGION,key);return o;}@Overridepublic boolean contains(String key) {return CacheUtil.isExist(REGION,key);}@SneakyThrows@Overridepublic void add(String key, Object o) {CacheUtil.set(REGION,key,o);}@Overridepublic void remove(String key) {CacheUtil.get(REGION,key);}@Overridepublic void clear() {CacheUtil.clear(REGION);}/*** 将流程定义从缓存删除。* @param actDefId*/public static void removeByDefId(String actDefId){CacheUtil.remove(REGION,actDefId);ConfigCacheUtil.remove(actDefId);}}
实现以上配置后,在流程很多地方中使用到获取流程节点等信息时,其会优先从缓存池里获取,若没有才会解析并且放置缓存中。
BpmnModel bpmnModel=repositoryService.getBpmnModel(actDefId);
流程扩展配置缓存
参考 BpmDefService类的实现,如获取流程定义的全局节点的配置或某个节点的节点配置:
/*** 根据流程定义ID获取* @param actDefId* @return*/public ProcessConfig getProcessConfig(String actDefId){if(StringUtils.isEmpty(actDefId)){return null;}Map<String,NodeConfig> mapConfig=getConfigByActDefId(actDefId);for (NodeConfig entry:mapConfig.values()){if(entry instanceof ProcessConfig){return (ProcessConfig) entry;}}return null;}/*** 根据流程定义获取节点定义。* @param actDefId* @return*/public Map<String,NodeConfig> getConfigByActDefId(String actDefId){Map<String,NodeConfig> mapConfig=ConfigCacheUtil.getCache(actDefId);if(BeanUtil.isNotEmpty(mapConfig)){return mapConfig;}BpmDef bpmDef=getByActDefId(actDefId);mapConfig=getNodeConfigs(bpmDef.getExtConfs());ConfigCacheUtil.add(actDefId,mapConfig);return mapConfig;}
缓存工具实现
该工具主要实现对redis的缓存读写的处理,如下实现所示:
package com.redxun.bpm.core.service;import com.redxun.bpm.activiti.config.NodeConfig;import com.redxun.cache.CacheUtil;import com.redxun.dto.bpm.BpmConst;import java.util.Map;/*** 流程配置缓存。*/public class ConfigCacheUtil {private static String getKey(String actDefId){return BpmConst.TYPE_CONFIG + actDefId;}/*** 获取缓存。* @param actDefId* @return*/public static Map<String, NodeConfig> getCache(String actDefId){String key=getKey(actDefId);return (Map<String, NodeConfig>)CacheUtil.get(BpmConst.CACHE_REGION,key);}/*** 删除* @param actDefId*/public static void remove(String actDefId){String key=getKey(actDefId);CacheUtil.remove(BpmConst.CACHE_REGION,key);}/*** 添加缓存。* @param actDefId* @param configMap*/public static void add(String actDefId,Map<String, NodeConfig> configMap){String key=getKey(actDefId);CacheUtil.set(BpmConst.CACHE_REGION,key,configMap);}}
package com.redxun.cache;import com.redxun.common.utils.SpringUtil;public class CacheUtil {public CacheUtil() {}public static ICache getCache() {return (ICache)SpringUtil.getBean(ICache.class);}public static void set(String region, String key, Object obj) {ICache cache = getCache();cache.set(region, key, obj);}public static Object get(String region, String key) {ICache cache = getCache();return cache.get(region, key);}public static void remove(String region, String key) {ICache cache = getCache();cache.remove(region, key);}public static void clear(String region) {ICache cache = getCache();cache.clear(region);}public static boolean isExist(String region, String key) {ICache cache = getCache();return cache.isExist(region, key);}}
文档更新时间: 2020-10-12 09:25   作者:csx