Skip to content

Commit 35cee96

Browse files
committed
增加打开 app 清空题目缓存配置(默认关闭)
1 parent e8eb76f commit 35cee96

File tree

8 files changed

+86
-11
lines changed

8 files changed

+86
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## version 2.13.2
2+
3+
- 增加打开 app 清空题目缓存配置(默认关闭),避免题目缓存数据不同步
4+
15
## version 2.13.1
26

37
- 题解显示 katex 数学公式

package.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vscode-leetcode-problem-rating",
33
"displayName": "LeetCode",
44
"description": "LeetCode 官方插件增强, 代码开源, 增加 LeetCode 题目难度分, 给个star吧, 球球了",
5-
"version": "2.13.1",
5+
"version": "2.13.2",
66
"author": "ccagml",
77
"publisher": "ccagml",
88
"license": "MIT",
@@ -1101,6 +1101,18 @@
11011101
],
11021102
"scope": "application",
11031103
"description": "Precinct Score Hidden Questions."
1104+
},
1105+
"leetcode-problem-rating.openClearProblemCache": {
1106+
"type": "boolean",
1107+
"default": false,
1108+
"scope": "application",
1109+
"description": "Clear the Problems cache when opening the app to avoid out-of-sync state"
1110+
},
1111+
"leetcode-problem-rating.openClearProblemCacheTime": {
1112+
"type": "integer",
1113+
"default": 3600,
1114+
"scope": "application",
1115+
"description": "Open the app to clear the cache interval by default 3600 seconds"
11041116
}
11051117
}
11061118
}

src/controller/MainController.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ class MainContorller {
3232
}
3333
}
3434

35+
/**
36+
* 检查题目缓存
37+
*/
38+
39+
public async deleteProblemCache() {
40+
await executeService.deleteProblemCache();
41+
}
42+
3543
/**
3644
* It takes the version number from the package.json file and converts it to a number
3745
* @param {ExtensionContext} context - ExtensionContext

src/extension.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export async function activate(context: ExtensionContext): Promise<void> {
4343
mainContorller.initialize(context);
4444
// 检查node环境
4545
await mainContorller.checkNodeEnv(context);
46+
await mainContorller.deleteProblemCache();
4647
// 事件监听
4748
eventController.addEvent();
4849

src/rpc/factory/api/cacheApi.ts

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,32 @@
1010
import { storageUtils } from "../../utils/storageUtils";
1111
import { sessionUtils } from "../../utils/sessionUtils";
1212
import { ApiBase } from "../apiBase";
13+
import { reply } from "../../utils/ReplyUtils";
1314

1415
class CacheApi extends ApiBase {
1516
constructor() {
1617
super();
1718
}
1819

1920
callArg(argv) {
20-
let argv_config = this.api_argv().option("d", {
21-
alias: "delete",
22-
type: "boolean",
23-
describe: "Delete cache by keyword",
24-
default: false,
25-
});
21+
let argv_config = this.api_argv()
22+
.option("d", {
23+
alias: "delete",
24+
type: "boolean",
25+
describe: "Delete cache by keyword",
26+
default: false,
27+
})
28+
.option("t", {
29+
alias: "lastmodify",
30+
type: "string",
31+
default: "",
32+
describe: "",
33+
})
34+
.positional("keyword", {
35+
type: "string",
36+
default: "",
37+
describe: "帮助的参数?",
38+
});
2639
argv_config.parseArgFromCmd(argv);
2740

2841
return argv_config.get_result();
@@ -34,15 +47,24 @@ class CacheApi extends ApiBase {
3447
const name = argv.keyword || "";
3548
const isInteger = Number.isInteger(Number(name));
3649

50+
let option_t = Number(argv.t);
51+
const need_last_modify_time = Number.isInteger(option_t);
52+
if (need_last_modify_time) {
53+
option_t *= 1000;
54+
}
3755
const all_data_file = storageUtils.listCache().filter(function (f) {
3856
return name.length === 0 || (isInteger ? f.name.startsWith(name + ".") : f.name === name);
3957
});
4058

4159
if (argv.delete) {
60+
const cur_time = new Date().getTime();
4261
for (let f of all_data_file) {
43-
storageUtils.delCache(f.name);
62+
if (!need_last_modify_time || (f.mtimeMs || 0) + option_t < cur_time) {
63+
storageUtils.delCache(f.name);
64+
}
4465
}
4566
}
67+
reply.info(JSON.stringify({ code: 100 }));
4668
}
4769
}
4870

src/rpc/utils/storageUtils.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ class StorageUtils {
266266
name: k,
267267
size: stat.size,
268268
mtime: stat.mtime,
269+
mtimeMs: stat.mtimeMs,
269270
};
270271
});
271272
}
@@ -463,7 +464,6 @@ class StorageUtils {
463464
return lineContent.substring(cut_pos);
464465
}
465466

466-
467467
public meta(filename) {
468468
const m = Object.assign({}, defaultMETA, {});
469469

@@ -476,8 +476,7 @@ class StorageUtils {
476476
for (let all_input = 0; all_input < file_info.length; all_input++) {
477477
const lineContent = file_info[all_input];
478478
if (caseFlag && lineContent.indexOf("@lcpr case=end") < 0) {
479-
curCase += this.fix_lineContent(lineContent).replace(/\s+/g, "")
480-
.replace(/\\n/g, "\n");
479+
curCase += this.fix_lineContent(lineContent).replace(/\s+/g, "").replace(/\\n/g, "\n");
481480
}
482481
// 收集所有用例
483482
if (lineContent.indexOf("@lcpr case=start") >= 0) {

src/service/ExecuteService.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { getNodePath } from "../utils/ConfigUtils";
1919
import { openUrl, promptForOpenOutputChannel } from "../utils/OutputUtils";
2020
import * as systemUtils from "../utils/SystemUtils";
2121
import { toWslPath, useWsl } from "../utils/SystemUtils";
22+
import { getOpenClearProblemCacheTime, isOpenClearProblemCache } from "../utils/ConfigUtils";
2223

2324
class ExecuteService implements Disposable {
2425
private leetCodeCliResourcesRootPath: string;
@@ -82,6 +83,24 @@ class ExecuteService implements Disposable {
8283
return true;
8384
}
8485

86+
// 多机同步,可能题目缓存会导致不一致
87+
public async deleteProblemCache() {
88+
if (isOpenClearProblemCache()) {
89+
try {
90+
await this.executeCommandWithProgressEx("正在清除缓存~", this.nodeExecutable, [
91+
await this.getLeetCodeBinaryPath(),
92+
"cache",
93+
"-d",
94+
"problems",
95+
"-t",
96+
getOpenClearProblemCacheTime().toString(),
97+
]);
98+
} catch (error) {
99+
await promptForOpenOutputChannel("Failed to delete cache. 请查看控制台信息~", OutPutType.error);
100+
}
101+
}
102+
}
103+
85104
public async deleteCache() {
86105
try {
87106
await this.executeCommandWithProgressEx("正在清除缓存~", this.nodeExecutable, [

src/utils/ConfigUtils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,3 +383,13 @@ export function getIncludeTemplate(lang: string): string {
383383

384384
return result;
385385
}
386+
387+
// 获取清除缓存修改时间间隔
388+
export function getOpenClearProblemCacheTime(): number {
389+
return getVsCodeConfig().get<number>("openClearProblemCacheTime") || 3600;
390+
}
391+
392+
// 是否打开清除题目缓存
393+
export function isOpenClearProblemCache(): boolean {
394+
return getVsCodeConfig().get<boolean>("openClearProblemCache", false);
395+
}

0 commit comments

Comments
 (0)