mirror of
https://github.com/crystelf/crystelf-core.git
synced 2025-07-04 14:49:19 +00:00
优化数据读取
This commit is contained in:
parent
1efa0eec5b
commit
5e1911a2c3
@ -2,6 +2,7 @@ import logger from '../../utils/core/logger';
|
||||
import paths from '../../utils/core/path';
|
||||
import fs from 'fs/promises';
|
||||
import path from 'path';
|
||||
import redisService from '../../services/redis/redis';
|
||||
|
||||
class BotService {
|
||||
public async getBotId() {
|
||||
@ -16,16 +17,22 @@ class BotService {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
const fileContent: string | undefined = await redisService.fetch('crystelfBots', fileName);
|
||||
if (fileContent) {
|
||||
uins.push(fileContent);
|
||||
}
|
||||
/*
|
||||
const filePath = path.join(botsPath, fileName);
|
||||
const fileContent = await fs.readFile(filePath, 'utf-8');
|
||||
const jsonData = JSON.parse(fileContent);
|
||||
if (jsonData.uin) {
|
||||
uins.push(jsonData.uin);
|
||||
}
|
||||
} catch (e) {
|
||||
logger.error(`读取或解析${fileName}出错`);
|
||||
}*/
|
||||
} catch (err) {
|
||||
logger.error(`读取或解析${fileName}出错: ${err}`);
|
||||
}
|
||||
}
|
||||
logger.debug(uins);
|
||||
return uins;
|
||||
}
|
||||
}
|
||||
|
@ -181,9 +181,6 @@ class RedisService {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试方法:示例性地获取一个用户数据
|
||||
*/
|
||||
public async test(): Promise<void> {
|
||||
const user = await this.fetch<IUser>('Jerry', 'IUser');
|
||||
logger.debug('User:', user);
|
||||
|
@ -5,26 +5,26 @@ import logger from '../core/logger';
|
||||
import fs from 'fs/promises';
|
||||
|
||||
class Persistence {
|
||||
private static getUserDataPath(username: string, fileName: string): string {
|
||||
return path.join(paths.get('userData'), username, `${fileName}.json`);
|
||||
private static getDataPath(dataName: string, fileName: string): string {
|
||||
return path.join(paths.get('userData'), dataName, `${fileName}.json`);
|
||||
}
|
||||
|
||||
private static async ensureUserPath(username: string): Promise<void> {
|
||||
const userPath = path.join(paths.get('userData'), username);
|
||||
private static async ensureUserPath(dataName: string): Promise<void> {
|
||||
const dataPath = path.join(paths.get('userData'), dataName);
|
||||
try {
|
||||
await fc.createDir(userPath, false);
|
||||
await fc.createDir(dataPath, false);
|
||||
} catch (err) {
|
||||
logger.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
public static async writeDataLocal<T>(
|
||||
username: string,
|
||||
dataName: string,
|
||||
data: T,
|
||||
fileName: string
|
||||
): Promise<void> {
|
||||
await this.ensureUserPath(username);
|
||||
const filePath = this.getUserDataPath(username, fileName);
|
||||
await this.ensureUserPath(dataName);
|
||||
const filePath = this.getDataPath(dataName, fileName);
|
||||
|
||||
try {
|
||||
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> {
|
||||
const filePath = this.getUserDataPath(username, fileName);
|
||||
public static async readDataLocal<T>(dataName: string, fileName: string): Promise<T | undefined> {
|
||||
const filePath = this.getDataPath(dataName, fileName);
|
||||
|
||||
try {
|
||||
const data = await fs.readFile(filePath, 'utf-8');
|
||||
|
Loading…
x
Reference in New Issue
Block a user