优化数据读取

This commit is contained in:
Jerry 2025-04-29 13:12:28 +08:00
parent 1efa0eec5b
commit 5e1911a2c3
3 changed files with 20 additions and 16 deletions

View File

@ -2,6 +2,7 @@ import logger from '../../utils/core/logger';
import paths from '../../utils/core/path'; import paths from '../../utils/core/path';
import fs from 'fs/promises'; import fs from 'fs/promises';
import path from 'path'; import path from 'path';
import redisService from '../../services/redis/redis';
class BotService { class BotService {
public async getBotId() { public async getBotId() {
@ -16,16 +17,22 @@ class BotService {
continue; continue;
} }
try { try {
const fileContent: string | undefined = await redisService.fetch('crystelfBots', fileName);
if (fileContent) {
uins.push(fileContent);
}
/*
const filePath = path.join(botsPath, fileName); const filePath = path.join(botsPath, fileName);
const fileContent = await fs.readFile(filePath, 'utf-8'); const fileContent = await fs.readFile(filePath, 'utf-8');
const jsonData = JSON.parse(fileContent); const jsonData = JSON.parse(fileContent);
if (jsonData.uin) { if (jsonData.uin) {
uins.push(jsonData.uin); uins.push(jsonData.uin);
} }*/
} catch (e) { } catch (err) {
logger.error(`读取或解析${fileName}出错`); logger.error(`读取或解析${fileName}出错: ${err}`);
} }
} }
logger.debug(uins);
return uins; return uins;
} }
} }

View File

@ -181,9 +181,6 @@ class RedisService {
return; return;
} }
/**
*
*/
public async test(): Promise<void> { public async test(): Promise<void> {
const user = await this.fetch<IUser>('Jerry', 'IUser'); const user = await this.fetch<IUser>('Jerry', 'IUser');
logger.debug('User:', user); logger.debug('User:', user);

View File

@ -5,26 +5,26 @@ import logger from '../core/logger';
import fs from 'fs/promises'; import fs from 'fs/promises';
class Persistence { class Persistence {
private static getUserDataPath(username: string, fileName: string): string { private static getDataPath(dataName: string, fileName: string): string {
return path.join(paths.get('userData'), username, `${fileName}.json`); return path.join(paths.get('userData'), dataName, `${fileName}.json`);
} }
private static async ensureUserPath(username: string): Promise<void> { private static async ensureUserPath(dataName: string): Promise<void> {
const userPath = path.join(paths.get('userData'), username); const dataPath = path.join(paths.get('userData'), dataName);
try { try {
await fc.createDir(userPath, false); await fc.createDir(dataPath, false);
} catch (err) { } catch (err) {
logger.error(err); logger.error(err);
} }
} }
public static async writeDataLocal<T>( public static async writeDataLocal<T>(
username: string, dataName: string,
data: T, data: T,
fileName: string fileName: string
): Promise<void> { ): Promise<void> {
await this.ensureUserPath(username); await this.ensureUserPath(dataName);
const filePath = this.getUserDataPath(username, fileName); const filePath = this.getDataPath(dataName, fileName);
try { try {
await fs.writeFile(filePath, JSON.stringify(data, null, 2), 'utf-8'); await fs.writeFile(filePath, JSON.stringify(data, null, 2), 'utf-8');
@ -34,8 +34,8 @@ class Persistence {
} }
} }
public static async readDataLocal<T>(username: string, fileName: string): Promise<T | undefined> { public static async readDataLocal<T>(dataName: string, fileName: string): Promise<T | undefined> {
const filePath = this.getUserDataPath(username, fileName); const filePath = this.getDataPath(dataName, fileName);
try { try {
const data = await fs.readFile(filePath, 'utf-8'); const data = await fs.readFile(filePath, 'utf-8');