mirror of
https://github.com/crystelf/crystelf-core.git
synced 2025-10-14 05:19:19 +00:00
67 lines
1.9 KiB
TypeScript
67 lines
1.9 KiB
TypeScript
import path from 'path';
|
|
import paths from '../../core/utils/system/path';
|
|
import fs from 'fs/promises';
|
|
import logger from '../../core/utils/system/logger';
|
|
|
|
class WordsService {
|
|
private wordCache: Record<string, string[]> = {}; //缓存
|
|
private readonly clearIntervalMs = 30 * 60 * 1000; //30min
|
|
|
|
constructor() {
|
|
this.startAutoClear();
|
|
}
|
|
private startAutoClear() {
|
|
setInterval(() => {
|
|
logger.info('[WordsService] Clearing wordCache..');
|
|
this.wordCache = {};
|
|
}, this.clearIntervalMs);
|
|
}
|
|
|
|
/**
|
|
* 从本地加载json到内存&返回
|
|
* @param id 文件名
|
|
*/
|
|
public async loadWordById(id: string): Promise<string[] | null> {
|
|
logger.info(`Loading words ${id}..`);
|
|
if (this.wordCache[id]) return this.wordCache[id];
|
|
const filePath = path.join(paths.get('words'), `${id}.json`);
|
|
try {
|
|
const content = await fs.readFile(filePath, 'utf-8');
|
|
const parsed = JSON.parse(content);
|
|
if (Array.isArray(parsed)) {
|
|
const texts = parsed.filter((item) => typeof item === 'string');
|
|
this.wordCache[id] = texts;
|
|
return texts;
|
|
} else {
|
|
return null;
|
|
}
|
|
} catch (error) {
|
|
logger.error(`Failed to loadWordById: ${id}`);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 重载json到内存
|
|
* @param id 文件名
|
|
*/
|
|
public async reloadWord(id: string): Promise<boolean> {
|
|
logger.info(`Reloading word: ${id}..`);
|
|
const filePath = path.join(paths.get('words'), `${id}.json`);
|
|
try {
|
|
const content = await fs.readFile(filePath, 'utf-8');
|
|
const parsed = JSON.parse(content);
|
|
if (Array.isArray(parsed)) {
|
|
this.wordCache[id] = parsed.filter((item) => typeof item === 'string');
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
} catch (e) {
|
|
logger.error(`Failed to reloadWordById: ${id}..`);
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
export default new WordsService();
|