diff --git a/CHANGELOG.md b/CHANGELOG.md index d4d97bc..0f25131 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## version 2.13.2 + +- 增加打开 app 清空题目缓存配置(默认关闭),避免题目缓存数据不同步 + ## version 2.13.1 - 题解显示 katex 数学公式 diff --git a/package.json b/package.json index 485d90e..e7d4124 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" } } } diff --git a/src/controller/MainController.ts b/src/controller/MainController.ts index c61b8a1..9bc5bb8 100644 --- a/src/controller/MainController.ts +++ b/src/controller/MainController.ts @@ -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 diff --git a/src/extension.ts b/src/extension.ts index 1da6a9c..e968a37 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -43,6 +43,7 @@ export async function activate(context: ExtensionContext): Promise { mainContorller.initialize(context); // 检查node环境 await mainContorller.checkNodeEnv(context); + await mainContorller.deleteProblemCache(); // 事件监听 eventController.addEvent(); diff --git a/src/rpc/factory/api/cacheApi.ts b/src/rpc/factory/api/cacheApi.ts index 841e3fc..cf9b45e 100644 --- a/src/rpc/factory/api/cacheApi.ts +++ b/src/rpc/factory/api/cacheApi.ts @@ -10,6 +10,7 @@ import { storageUtils } from "../../utils/storageUtils"; import { sessionUtils } from "../../utils/sessionUtils"; import { ApiBase } from "../apiBase"; +import { reply } from "../../utils/ReplyUtils"; class CacheApi extends ApiBase { constructor() { @@ -17,12 +18,24 @@ class CacheApi extends ApiBase { } 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(); @@ -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 })); } } diff --git a/src/rpc/utils/storageUtils.ts b/src/rpc/utils/storageUtils.ts index 39a24e5..3bef620 100644 --- a/src/rpc/utils/storageUtils.ts +++ b/src/rpc/utils/storageUtils.ts @@ -266,6 +266,7 @@ class StorageUtils { name: k, size: stat.size, mtime: stat.mtime, + mtimeMs: stat.mtimeMs, }; }); } @@ -463,7 +464,6 @@ class StorageUtils { return lineContent.substring(cut_pos); } - public meta(filename) { const m = Object.assign({}, defaultMETA, {}); @@ -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) { diff --git a/src/service/ExecuteService.ts b/src/service/ExecuteService.ts index 1269e27..8b53351 100644 --- a/src/service/ExecuteService.ts +++ b/src/service/ExecuteService.ts @@ -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; @@ -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, [ diff --git a/src/utils/ConfigUtils.ts b/src/utils/ConfigUtils.ts index 38251ac..f6ca700 100644 --- a/src/utils/ConfigUtils.ts +++ b/src/utils/ConfigUtils.ts @@ -383,3 +383,13 @@ export function getIncludeTemplate(lang: string): string { return result; } + +// 获取清除缓存修改时间间隔 +export function getOpenClearProblemCacheTime(): number { + return getVsCodeConfig().get("openClearProblemCacheTime") || 3600; +} + +// 是否打开清除题目缓存 +export function isOpenClearProblemCache(): boolean { + return getVsCodeConfig().get("openClearProblemCache", false); +}