Skip to content

增加打开 app 清空题目缓存配置(默认关闭) #167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## version 2.13.2

- 增加打开 app 清空题目缓存配置(默认关闭),避免题目缓存数据不同步

## version 2.13.1

- 题解显示 katex 数学公式
Expand Down
14 changes: 13 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vscode-leetcode-problem-rating",
"displayName": "LeetCode",
"description": "LeetCode 官方插件增强, 代码开源, 增加 LeetCode 题目难度分, 给个star吧, 球球了",
"version": "2.13.1",
"version": "2.13.2",
"author": "ccagml",
"publisher": "ccagml",
"license": "MIT",
Expand Down Expand Up @@ -1101,6 +1101,18 @@
],
"scope": "application",
"description": "Precinct Score Hidden Questions."
},
"leetcode-problem-rating.openClearProblemCache": {
"type": "boolean",
"default": false,
"scope": "application",
"description": "Clear the Problems cache when opening the app to avoid out-of-sync state"
},
"leetcode-problem-rating.openClearProblemCacheTime": {
"type": "integer",
"default": 3600,
"scope": "application",
"description": "Open the app to clear the cache interval by default 3600 seconds"
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/controller/MainController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ class MainContorller {
}
}

/**
* 检查题目缓存
*/

public async deleteProblemCache() {
await executeService.deleteProblemCache();
}

/**
* It takes the version number from the package.json file and converts it to a number
* @param {ExtensionContext} context - ExtensionContext
Expand Down
1 change: 1 addition & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export async function activate(context: ExtensionContext): Promise<void> {
mainContorller.initialize(context);
// 检查node环境
await mainContorller.checkNodeEnv(context);
await mainContorller.deleteProblemCache();
// 事件监听
eventController.addEvent();

Expand Down
36 changes: 29 additions & 7 deletions src/rpc/factory/api/cacheApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,32 @@
import { storageUtils } from "../../utils/storageUtils";
import { sessionUtils } from "../../utils/sessionUtils";
import { ApiBase } from "../apiBase";
import { reply } from "../../utils/ReplyUtils";

class CacheApi extends ApiBase {
constructor() {
super();
}

callArg(argv) {
let argv_config = this.api_argv().option("d", {
alias: "delete",
type: "boolean",
describe: "Delete cache by keyword",
default: false,
});
let argv_config = this.api_argv()
.option("d", {
alias: "delete",
type: "boolean",
describe: "Delete cache by keyword",
default: false,
})
.option("t", {
alias: "lastmodify",
type: "string",
default: "",
describe: "",
})
.positional("keyword", {
type: "string",
default: "",
describe: "帮助的参数?",
});
argv_config.parseArgFromCmd(argv);

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

let option_t = Number(argv.t);
const need_last_modify_time = Number.isInteger(option_t);
if (need_last_modify_time) {
option_t *= 1000;
}
const all_data_file = storageUtils.listCache().filter(function (f) {
return name.length === 0 || (isInteger ? f.name.startsWith(name + ".") : f.name === name);
});

if (argv.delete) {
const cur_time = new Date().getTime();
for (let f of all_data_file) {
storageUtils.delCache(f.name);
if (!need_last_modify_time || (f.mtimeMs || 0) + option_t < cur_time) {
storageUtils.delCache(f.name);
}
}
}
reply.info(JSON.stringify({ code: 100 }));
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/rpc/utils/storageUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ class StorageUtils {
name: k,
size: stat.size,
mtime: stat.mtime,
mtimeMs: stat.mtimeMs,
};
});
}
Expand Down Expand Up @@ -463,7 +464,6 @@ class StorageUtils {
return lineContent.substring(cut_pos);
}


public meta(filename) {
const m = Object.assign({}, defaultMETA, {});

Expand All @@ -476,8 +476,7 @@ class StorageUtils {
for (let all_input = 0; all_input < file_info.length; all_input++) {
const lineContent = file_info[all_input];
if (caseFlag && lineContent.indexOf("@lcpr case=end") < 0) {
curCase += this.fix_lineContent(lineContent).replace(/\s+/g, "")
.replace(/\\n/g, "\n");
curCase += this.fix_lineContent(lineContent).replace(/\s+/g, "").replace(/\\n/g, "\n");
}
// 收集所有用例
if (lineContent.indexOf("@lcpr case=start") >= 0) {
Expand Down
19 changes: 19 additions & 0 deletions src/service/ExecuteService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { getNodePath } from "../utils/ConfigUtils";
import { openUrl, promptForOpenOutputChannel } from "../utils/OutputUtils";
import * as systemUtils from "../utils/SystemUtils";
import { toWslPath, useWsl } from "../utils/SystemUtils";
import { getOpenClearProblemCacheTime, isOpenClearProblemCache } from "../utils/ConfigUtils";

class ExecuteService implements Disposable {
private leetCodeCliResourcesRootPath: string;
Expand Down Expand Up @@ -82,6 +83,24 @@ class ExecuteService implements Disposable {
return true;
}

// 多机同步,可能题目缓存会导致不一致
public async deleteProblemCache() {
if (isOpenClearProblemCache()) {
try {
await this.executeCommandWithProgressEx("正在清除缓存~", this.nodeExecutable, [
await this.getLeetCodeBinaryPath(),
"cache",
"-d",
"problems",
"-t",
getOpenClearProblemCacheTime().toString(),
]);
} catch (error) {
await promptForOpenOutputChannel("Failed to delete cache. 请查看控制台信息~", OutPutType.error);
}
}
}

public async deleteCache() {
try {
await this.executeCommandWithProgressEx("正在清除缓存~", this.nodeExecutable, [
Expand Down
10 changes: 10 additions & 0 deletions src/utils/ConfigUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,13 @@ export function getIncludeTemplate(lang: string): string {

return result;
}

// 获取清除缓存修改时间间隔
export function getOpenClearProblemCacheTime(): number {
return getVsCodeConfig().get<number>("openClearProblemCacheTime") || 3600;
}

// 是否打开清除题目缓存
export function isOpenClearProblemCache(): boolean {
return getVsCodeConfig().get<boolean>("openClearProblemCache", false);
}