mirror of
https://github.com/crystelf/crystelf-core.git
synced 2025-07-04 14:49:19 +00:00
redis数据静态保存
This commit is contained in:
parent
e16378e75b
commit
a2d1a9a4f2
1
.gitignore
vendored
1
.gitignore
vendored
@ -11,3 +11,4 @@ yarn-error.log*
|
||||
.vscode/*
|
||||
/dist/
|
||||
/logs/
|
||||
/private/
|
||||
|
@ -4,6 +4,7 @@ import tools from '../../utils/core/tool';
|
||||
import config from '../../utils/core/config';
|
||||
import redisTool from '../../utils/redis/redisTools';
|
||||
import IUser from '../../types/user';
|
||||
import Persistence from '../../utils/redis/persistence';
|
||||
|
||||
class RedisService {
|
||||
private client!: Redis;
|
||||
@ -107,6 +108,34 @@ class RedisService {
|
||||
return redisTool.reviveDates(deserialized);
|
||||
}
|
||||
|
||||
public async persistUser<T extends IUser>(user: T): Promise<void> {
|
||||
try {
|
||||
await this.setObject(`user:${user.qq}`, user);
|
||||
await Persistence.writeDataLocal(user.name, user);
|
||||
} catch (err) {
|
||||
logger.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
public async fetchUser<T extends IUser>(
|
||||
qq: string,
|
||||
username: string
|
||||
): Promise<IUser | undefined> {
|
||||
try {
|
||||
const fromRedis = await this.getObject<IUser>(`user:${qq}`);
|
||||
if (fromRedis) return fromRedis;
|
||||
const fromLocal = await Persistence.readDataLocal<IUser>(username);
|
||||
if (fromLocal) {
|
||||
await this.setObject(`user:${qq}`, fromLocal);
|
||||
return fromLocal;
|
||||
}
|
||||
logger.error(`用户${username},qq${qq}不存在!`);
|
||||
return undefined;
|
||||
} catch (err) {
|
||||
logger.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
public async test(): Promise<void> {
|
||||
const testData: IUser = {
|
||||
name: 'Jerry',
|
||||
@ -117,6 +146,9 @@ class RedisService {
|
||||
};
|
||||
let test = redisTool.reviveDates(testData);
|
||||
logger.debug(test);
|
||||
await this.setObject('test', test);
|
||||
const push = await this.getObject('test');
|
||||
logger.debug(push);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -33,6 +33,7 @@ class PathManager {
|
||||
log: path.join(this.baseDir, 'logs'),
|
||||
config: path.join(this.baseDir, 'config'),
|
||||
temp: path.join(this.baseDir, 'temp'),
|
||||
userData: path.join(this.baseDir, 'private/data'),
|
||||
};
|
||||
|
||||
return type ? mappings[type] : this.baseDir;
|
||||
@ -44,10 +45,13 @@ class PathManager {
|
||||
public init(): void {
|
||||
const logPath = this.get('log');
|
||||
const imagePath = this.get('images');
|
||||
const dataPath = this.get('userData');
|
||||
fc.createDir(logPath, false);
|
||||
fc.createDir(imagePath, false);
|
||||
fc.createDir(dataPath, false);
|
||||
logger.debug(`日志目录初始化: ${logPath}`);
|
||||
logger.debug(`图像目录初始化: ${imagePath}`);
|
||||
logger.debug(`用户数据目录初始化: ${dataPath}`);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -70,7 +74,7 @@ class PathManager {
|
||||
}
|
||||
}
|
||||
|
||||
type PathType = 'root' | 'public' | 'images' | 'log' | 'config' | 'temp';
|
||||
type PathType = 'root' | 'public' | 'images' | 'log' | 'config' | 'temp' | 'userData';
|
||||
|
||||
const paths = PathManager.getInstance();
|
||||
export default paths;
|
||||
|
46
src/utils/redis/persistence.ts
Normal file
46
src/utils/redis/persistence.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import path from 'path';
|
||||
import paths from '../core/path';
|
||||
import fc from '../core/file';
|
||||
import logger from '../core/logger';
|
||||
import fs from 'fs/promises';
|
||||
|
||||
class Persistence {
|
||||
private static getUserDataPath(username: string): string {
|
||||
return path.join(paths.get('userData'), username, 'data.json');
|
||||
}
|
||||
|
||||
private static async ensureUserPath(username: string): Promise<void> {
|
||||
const userPath = path.join(paths.get('userData'), username);
|
||||
try {
|
||||
await fc.createDir(userPath, false);
|
||||
} catch (err) {
|
||||
logger.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
public static async writeDataLocal<T>(username: string, data: T): Promise<void> {
|
||||
await this.ensureUserPath(username);
|
||||
const filePath = this.getUserDataPath(username);
|
||||
|
||||
try {
|
||||
await fs.writeFile(filePath, JSON.stringify(data, null, 2), 'utf-8');
|
||||
logger.debug(`用户数据已持久化到本地${filePath}`);
|
||||
} catch (err) {
|
||||
logger.error(err);
|
||||
}
|
||||
}
|
||||
|
||||
public static async readDataLocal<T>(username: string): Promise<T | undefined> {
|
||||
const filePath = this.getUserDataPath(username);
|
||||
|
||||
try {
|
||||
const data = await fs.readFile(filePath, 'utf-8');
|
||||
return JSON.parse(data) as T;
|
||||
} catch (err) {
|
||||
logger.error(err);
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default Persistence;
|
Loading…
x
Reference in New Issue
Block a user