Skip to content

Commit 1215ecb

Browse files
committed
add cache when fetch remote templates.
1 parent e74f7f0 commit 1215ecb

File tree

2 files changed

+94
-60
lines changed

2 files changed

+94
-60
lines changed

src/OperationExplorer.ts

Lines changed: 86 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,55 +1209,71 @@ export class OperationExplorer {
12091209

12101210
try {
12111211

1212+
const resManager = ResManager.GetInstance();
12121213
const pathArr = (remoteUrl).split('/');
12131214
const hostName = pathArr[0];
12141215
const path = '/' + pathArr.slice(1).join('/');
12151216

1216-
const res = await vscode.window.withProgress({
1217-
location: vscode.ProgressLocation.Notification,
1218-
title: `Connect repo '${rawUrl}' ...`,
1219-
cancellable: true
1220-
}, (_, token): Thenable<NetResponse<any>> => {
1221-
return new Promise(async (resolve) => {
1217+
let repoFileList: GitFileInfo[];
12221218

1223-
token.onCancellationRequested(() => {
1224-
netReq.emit('abort');
1225-
});
1219+
const cache = resManager.getCache('eide-template-list.json');
1220+
if (cache && (cache.lastUpdateTime || 0) + (10 * utility.TIME_ONE_MINUTE) > Date.now() &&
1221+
resManager.getCachedFileByName(cache.name).IsFile()) {
1222+
repoFileList = JSON.parse(resManager.getCachedFileByName(cache.name).Read());
1223+
}
1224+
// if no cache, fetch it.
1225+
else {
1226+
const res = await vscode.window.withProgress({
1227+
location: vscode.ProgressLocation.Notification,
1228+
title: `Connect repo '${rawUrl}' ...`,
1229+
cancellable: true
1230+
}, (_, token): Thenable<NetResponse<any>> => {
1231+
return new Promise(async (resolve) => {
1232+
1233+
token.onCancellationRequested(() => {
1234+
netReq.emit('abort');
1235+
});
12261236

1227-
const headers: any = utility.setProxyHeader({
1228-
'User-Agent': 'Mozilla/5.0'
1229-
});
1237+
const headers: any = utility.setProxyHeader({
1238+
'User-Agent': 'Mozilla/5.0'
1239+
});
12301240

1231-
if (acToken) { // if token is enabled, use it
1232-
headers['Authorization'] = `token ${acToken}`;
1233-
}
1241+
if (acToken) { // if token is enabled, use it
1242+
headers['Authorization'] = `token ${acToken}`;
1243+
}
12341244

1235-
const res = await netReq.Request<any, any>({
1236-
host: hostName,
1237-
path: path,
1238-
timeout: 3000,
1239-
headers: headers
1240-
}, 'https');
1245+
const res = await netReq.Request<any, any>({
1246+
host: hostName,
1247+
path: path,
1248+
timeout: 3000,
1249+
headers: headers
1250+
}, 'https');
12411251

1242-
resolve(res);
1252+
resolve(res);
1253+
});
12431254
});
1244-
});
12451255

1246-
if (!res.success) {
1247-
GlobalEvent.emit('msg', newMessage('Warning', `Can't connect to Github repository !, msg: ${res.msg || 'null'}`));
1248-
this.locked = false;
1249-
return;
1250-
} else if (res.content === undefined) {
1251-
GlobalEvent.emit('msg', newMessage('Warning', `Can't get content from Github repository !, msg: ${res.msg || 'null'}`));
1252-
this.locked = false;
1253-
return;
1254-
}
1256+
if (!res.success) {
1257+
GlobalEvent.emit('msg', newMessage('Warning', `Can't connect to Github repository !, msg: ${res.msg || 'null'}`));
1258+
this.locked = false;
1259+
return;
1260+
} else if (res.content === undefined) {
1261+
GlobalEvent.emit('msg', newMessage('Warning', `Can't get content from Github repository !, msg: ${res.msg || 'null'}`));
1262+
this.locked = false;
1263+
return;
1264+
}
12551265

1256-
const resManager = ResManager.GetInstance();
1266+
repoFileList = res.content;
1267+
1268+
resManager.addCache({
1269+
name: 'eide-template-list.json',
1270+
lastUpdateTime: Date.now()
1271+
}, JSON.stringify(repoFileList, undefined, 2));
1272+
}
12571273

12581274
// get file list
12591275
const file_list = new Map<string, GitFileInfo>();
1260-
(<GitFileInfo[]>res.content)
1276+
repoFileList
12611277
.filter((obj) => { return obj.type === 'file'; })
12621278
.forEach((fInfo) => { file_list.set(fInfo.name, fInfo); });
12631279

@@ -1275,32 +1291,46 @@ export class OperationExplorer {
12751291
return;
12761292
}
12771293

1278-
// load index.json
1279-
const indexFileBuf = await vscode.window.withProgress({
1280-
location: vscode.ProgressLocation.Notification,
1281-
title: 'Fetching templates index ...',
1282-
cancellable: false
1283-
}, (_, __): Thenable<Buffer | Error | undefined> => {
1284-
return new Promise(async (resolve) => {
1285-
if (indexFileInfo.download_url) {
1286-
resolve(await utility.downloadFile(indexFileInfo.download_url));
1287-
} else {
1288-
resolve(new Error('download url is null !'));
1289-
}
1290-
});
1291-
});
1292-
12931294
let templateIndexInfo: TemplateIndexDef = <any>null;
12941295
let templateInfoList: TemplateInfo[] = <any>null;
12951296

1296-
if (indexFileBuf instanceof Buffer) {
1297-
templateIndexInfo = JSON.parse(indexFileBuf.toString());
1297+
const idxCache = resManager.getCache('template.index.json');
1298+
if (idxCache && (idxCache.lastUpdateTime || 0) + (15 * utility.TIME_ONE_MINUTE) > Date.now() &&
1299+
resManager.getCachedFileByName(idxCache.name).IsFile()) {
1300+
templateIndexInfo = JSON.parse(resManager.getCachedFileByName(idxCache.name).Read());
12981301
templateInfoList = templateIndexInfo.template_list;
1299-
} else {
1300-
const msg: string = indexFileBuf instanceof Error ? `, msg: ${indexFileBuf.message}` : '';
1301-
GlobalEvent.emit('msg', newMessage('Warning', `Download template 'index.json' failed !${msg}`));
1302-
this.locked = false;
1303-
return;
1302+
}
1303+
// if no cache, fetch it.
1304+
else {
1305+
// load index.json
1306+
const indexFileBuf = await vscode.window.withProgress({
1307+
location: vscode.ProgressLocation.Notification,
1308+
title: 'Fetching templates index ...',
1309+
cancellable: false
1310+
}, (_, __): Thenable<Buffer | Error | undefined> => {
1311+
return new Promise(async (resolve) => {
1312+
if (indexFileInfo.download_url) {
1313+
resolve(await utility.downloadFile(indexFileInfo.download_url));
1314+
} else {
1315+
resolve(new Error('download url is null !'));
1316+
}
1317+
});
1318+
});
1319+
1320+
if (indexFileBuf instanceof Buffer) {
1321+
templateIndexInfo = JSON.parse(indexFileBuf.toString());
1322+
templateInfoList = templateIndexInfo.template_list;
1323+
} else {
1324+
const msg: string = indexFileBuf instanceof Error ? `, msg: ${indexFileBuf.message}` : '';
1325+
GlobalEvent.emit('msg', newMessage('Warning', `Download template 'index.json' failed !${msg}`));
1326+
this.locked = false;
1327+
return;
1328+
}
1329+
1330+
resManager.addCache({
1331+
name: 'template.index.json',
1332+
lastUpdateTime: Date.now()
1333+
}, indexFileBuf.toString());
13041334
}
13051335

13061336
const rootTemplateGroup: TemplateGroup = {

src/ResManager.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,10 @@ const cacheName = 'eide.cache';
7272

7373
export interface FileCacheInfo {
7474
name: string;
75-
size: number;
76-
version: string;
75+
size?: number;
76+
version?: string;
7777
sha?: string;
78-
lastUpdateTime?: string;
78+
lastUpdateTime?: number;
7979
}
8080

8181
export interface HostInfo {
@@ -266,7 +266,7 @@ export class ResManager extends events.EventEmitter {
266266
return new File(this.GetTmpDir().path + File.sep + name);
267267
}
268268

269-
addCache(cacheInfo: FileCacheInfo) {
269+
addCache(cacheInfo: FileCacheInfo, content?: Buffer | string) {
270270
const oldCache = this.getCache(cacheInfo.name);
271271
if (oldCache === undefined) {
272272
this.cacheInfoList.push(cacheInfo);
@@ -275,6 +275,10 @@ export class ResManager extends events.EventEmitter {
275275
(<any>oldCache)[key] = (<any>cacheInfo)[key];
276276
}
277277
}
278+
if (content) {
279+
const f = this.getCachedFileByName(cacheInfo.name);
280+
fs.writeFileSync(f.path, content);
281+
}
278282
}
279283

280284
GetIconByName(name: string): File {

0 commit comments

Comments
 (0)