提交 a9e3ea4a 编写于 作者: tony001's avatar tony001

代码表服务优化

上级 8f2846b9
...@@ -149,10 +149,11 @@ export default class CodeListService { ...@@ -149,10 +149,11 @@ export default class CodeListService {
// 启用缓存 // 启用缓存
if(isEnableCache){ if(isEnableCache){
const callback:Function = (context:any ={},data:any ={},tag:string,promise:Promise<any>) =>{ const callback:Function = (context:any ={},data:any ={},tag:string,promise:Promise<any>) =>{
const callbackKey:string = `${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`;
promise.then((result:any) =>{ promise.then((result:any) =>{
if(result.length > 0){ if(result.length > 0){
CodeListService.codelistCached.set(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`,{items:result}); CodeListService.codelistCached.set(callbackKey,{items:result});
CodeListService.codelistCache.delete(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`); CodeListService.codelistCache.delete(callbackKey);
return resolve(result); return resolve(result);
}else{ }else{
return resolve([]); return resolve([]);
...@@ -162,8 +163,9 @@ export default class CodeListService { ...@@ -162,8 +163,9 @@ export default class CodeListService {
}) })
} }
// 加载完成,从本地缓存获取 // 加载完成,从本地缓存获取
if(CodeListService.codelistCached.get(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`)){ const key:string = `${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`;
let items:any = CodeListService.codelistCached.get(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`).items; if(CodeListService.codelistCached.get(key)){
let items:any = CodeListService.codelistCached.get(key).items;
if(items.length >0){ if(items.length >0){
if(new Date().getTime() <= codelist.getExpirationTime()){ if(new Date().getTime() <= codelist.getExpirationTime()){
return resolve(items); return resolve(items);
...@@ -172,11 +174,11 @@ export default class CodeListService { ...@@ -172,11 +174,11 @@ export default class CodeListService {
} }
if (codelist) { if (codelist) {
// 加载中,UI又需要数据,解决连续加载同一代码表问题 // 加载中,UI又需要数据,解决连续加载同一代码表问题
if(CodeListService.codelistCache.get(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`)){ if(CodeListService.codelistCache.get(key)){
callback(context,data,tag,CodeListService.codelistCache.get(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`)); callback(context,data,tag,CodeListService.codelistCache.get(key));
}else{ }else{
let result:Promise<any> = codelist.getItems(context,data,isloading); let result:Promise<any> = codelist.getItems(context,data,isloading);
CodeListService.codelistCache.set(`${JSON.stringify(context)}-${JSON.stringify(data)}-${tag}`,result); CodeListService.codelistCache.set(key,result);
codelist.setExpirationTime(new Date().getTime() + cacheTimeout); codelist.setExpirationTime(new Date().getTime() + cacheTimeout);
callback(context,data,tag,result); callback(context,data,tag,result);
} }
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册