fix: fixed function clearCache and added 2 fields cacheClearFailed/cachePath

This commit is contained in:
gdjdkid committed 2026-03-26 22:52:42 +08:00
1 parent 20873e7512
commit f37db4b22b
3 files changed
+26 -7

No files matched your search

+1 -1
View File
@@ -56,7 +56,7 @@ program
}
if (options.clearCache) {
clearCache();
clearCache(options.lang || 'zh'); // 支持 -l zh/en/jp 等
process.exit(0);
}
+15 -6
View File
@@ -1,6 +1,7 @@
import { readFileSync, writeFileSync, existsSync, mkdirSync } from 'fs';
import { homedir } from 'os';
import { join } from 'path';
import { getLang } from './i18n.js';
// 缓存文件存放在用户主目录下,和 yddict 的做法一致
const CACHE_DIR = join(homedir(), '.config', 'javinfo');
@@ -14,7 +15,6 @@ function loadCache() {
try {
return JSON.parse(readFileSync(CACHE_FILE, 'utf-8'));
} catch {
// 文件损坏就当缓存不存在
return {};
}
}
@@ -31,7 +31,6 @@ export function getCache(id) {
const entry = cache[id];
if (!entry) return null;
// 检查是否过期
const isExpired = Date.now() - entry.cachedAt > TTL;
if (isExpired) return null;
@@ -47,10 +46,20 @@ export function setCache(id, data) {
saveCache(cache);
}
export function clearCache() {
if (existsSync(CACHE_FILE)) {
writeFileSync(CACHE_FILE, '{}', 'utf-8');
console.log('缓存已清空');
// 清空缓存(已支持多语言)
export function clearCache(lang = 'zh') {
const t = getLang(lang);
try {
if (existsSync(CACHE_FILE)) {
writeFileSync(CACHE_FILE, '{}', 'utf-8');
console.log(t.cacheCleared);
} else {
console.log(t.cacheCleared);
}
} catch (err) {
console.error(`${t.cacheClearFailed}: ${err.message}`);
console.error(`${t.cachePath}: ${CACHE_FILE}`);
}
}
+10
View File
@@ -17,6 +17,8 @@ export const messages = {
notFound: '未找到番号',
queryFailed: '查询失败',
cacheCleared: '缓存已清空',
cacheClearFailed: '清空缓存失败',
cachePath: '缓存路径',
fc2Detected: 'FC2番号已转换为',
windowsHint: '温馨提示: 此番号需要JAVDB数据源,Windows暂不支持~',
},
@@ -38,6 +40,8 @@ export const messages = {
notFound: 'Title not found',
queryFailed: 'Query failed',
cacheCleared: 'Cache cleared',
cacheClearFailed: 'Failed to clear cache',
cachePath: 'Cache path',
fc2Detected: 'FC2 format detected',
windowsHint: 'Note: This title requires JAVDB, which is unavailable on Windows.',
},
@@ -59,6 +63,8 @@ export const messages = {
notFound: '番号が見つかりません',
queryFailed: '検索失敗',
cacheCleared: 'キャッシュを削除しました',
cacheClearFailed: 'キャッシュの削除に失敗しました',
cachePath: 'キャッシュパス',
fc2Detected: 'FC2番号を変換しました',
windowsHint: '注意: この番号はJAVDBが必要ですが、WindowsではJAVDBが利用できません。',
},
@@ -80,6 +86,8 @@ export const messages = {
notFound: '번호를 찾을 수 없습니다',
queryFailed: '검색 실패',
cacheCleared: '캐시가 삭제되었습니다',
cacheClearFailed: '캐시 삭제 실패',
cachePath: '캐시 경로',
fc2Detected: 'FC2 번호 변환됨',
windowsHint: '알림: 이 번호는 JAVDB가 필요하지만 Windows에서는 사용할 수 없습니다.',
},
@@ -101,6 +109,8 @@ export const messages = {
notFound: 'Titel nicht gefunden',
queryFailed: 'Suche fehlgeschlagen',
cacheCleared: 'Cache geleert',
cacheClearFailed: 'Cache konnte nicht geleert werden',
cachePath: 'Cache-Pfad',
fc2Detected: 'FC2-Format erkannt',
windowsHint: 'Hinweis: Dieser Titel benötigt JAVDB, das unter Windows nicht verfügbar ist.',
},