暂存单据数据-使用Redis原创
8人赞赏了该文章
2,023次浏览
编辑于2021年11月01日 16:23:14
开发过程中有遇到单据需要暂时保存的方法,但是目前苍穹并没有找到官方文档,所以自己写了一个运用缓存来进行暂存的方法,话不多说,直接贴代码
import kd.bos.dataentity.entity.DynamicObject; import kd.bos.dataentity.metadata.dynamicobject.DynamicObjectType; import kd.bos.entity.cache.AppCache; import kd.bos.entity.cache.IAppCache; import kd.bos.entity.datamodel.IDataModel; import kd.bos.servicehelper.operation.SaveServiceHelper; import kd.bos.servicehelper.util.DynamicObjectSerializeUtil; /** * @ClassName AutoSaveBillUtil * @Author zb * @Description TODO * @Date 2021/4/20 14:15 **/ public class AutoSaveAndReadBillUtil { /** * ID 关键字-用于构建缓存key **/ private static final String ID = "id"; /** * @Author zb * @Description TODO * @Date 15:25 2021/4/29 * @Param [iDataModel, cacheRegion(缓存空间)] * @return void **/ public static void autoSaveBillDateByPropertyChanged(IDataModel iDataModel,String cacheRegion){ // 当前表单数据包 DynamicObject fromObject = iDataModel.getDataEntity(true); //获取当前单据id long billId = (long) iDataModel.getDataEntity().get(ID); //判断为为生成单据时候,保存到数据库生成 if(billId == 0){ if(billId == 0){ //新建保存表单数据 SaveServiceHelper.save(new DynamicObject[] {fromObject}); } } //构建缓存key String cacheId = buildCacheId(iDataModel,cacheRegion); //当前表单数据类型 DynamicObjectType objectType = fromObject.getDynamicObjectType(); //序列化对象 String objectSerialization = DynamicObjectSerializeUtil.serialize(new Object[]{fromObject},objectType); //缓存 IAppCache appCache = AppCache.get(cacheRegion); //设置缓存内容 appCache.put(cacheId,objectSerialization); } /** * @Author zb * @Description 获取缓存中表单对象 * @Date 17:49 2021/4/26 * @Param [iDataModel] * @return void **/ public static DynamicObject getBillDateByCache(IDataModel iDataModel,String cacheRegion){ // 当前表单数据包 DynamicObject fromObject = iDataModel.getDataEntity(true); //当前表单数据类型 DynamicObjectType objectType = fromObject.getDynamicObjectType(); //缓存 IAppCache appCache = AppCache.get(cacheRegion); //获取缓存key String cacheId = buildCacheId(iDataModel,cacheRegion); //缓存中序列化数据 String cacheString = appCache.get(cacheId,String.class); //构建当前表单对象-缓存数据为空时直接返回当前表单数据 DynamicObject toDyObject = iDataModel.getDataEntity(true); if(cacheString != null && !cacheString.equals("")){ //反序列化 Object[] toObject = DynamicObjectSerializeUtil.deserialize(cacheString,objectType); //对于单个对象数据直接获取设值 toDyObject = (DynamicObject) toObject[0]; } return toDyObject; } /** * @Author zb * @Description 构建缓存Id * @Date 15:33 2021/4/29 * @Param [iDataModel, cacheRegion] * @return java.lang.String **/ private static String buildCacheId(IDataModel iDataModel,String cacheRegion){ //获取当前单据id long billId = (long) iDataModel.getDataEntity().get(ID); //构建缓存key(缓存区域+单据id) String cacheId = cacheRegion+String.valueOf(billId); return cacheId; } /** * @Author zb * @Description 清理缓存 * @Date 16:23 2021/4/29 * @Param [iDataModel, cacheRegion] * @return java.lang.String **/ public static String removeCache(IDataModel iDataModel,String cacheRegion){ // 当前表单数据包 DynamicObject fromObject = iDataModel.getDataEntity(true); //保存表单数据 SaveServiceHelper.save(new DynamicObject[] {fromObject}); //缓存 IAppCache appCache = AppCache.get(cacheRegion); //获取缓存key String cacheId = buildCacheId(iDataModel,cacheRegion); //清空缓存 appCache.remove(cacheId); //缓存中序列化数据 String cacheString = appCache.get(cacheId,String.class); return cacheId; } }
赞 8
8人点赞
还没有人点赞,快来当第一个点赞的人吧!
打赏
0人打赏
还没有人打赏,快来当第一个打赏的人吧!
推荐阅读