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
a2d1a9a4f2
commit
4acc9936db
1
.idea/codeStyles/Project.xml
generated
1
.idea/codeStyles/Project.xml
generated
@ -14,6 +14,7 @@
|
|||||||
</JSCodeStyleSettings>
|
</JSCodeStyleSettings>
|
||||||
<TypeScriptCodeStyleSettings version="0">
|
<TypeScriptCodeStyleSettings version="0">
|
||||||
<option name="FORCE_SEMICOLON_STYLE" value="true" />
|
<option name="FORCE_SEMICOLON_STYLE" value="true" />
|
||||||
|
<option name="FILE_NAME_STYLE" value="CAMEL_CASE" />
|
||||||
<option name="SPACE_BEFORE_FUNCTION_LEFT_PARENTH" value="false" />
|
<option name="SPACE_BEFORE_FUNCTION_LEFT_PARENTH" value="false" />
|
||||||
<option name="USE_PUBLIC_MODIFIER" value="true" />
|
<option name="USE_PUBLIC_MODIFIER" value="true" />
|
||||||
<option name="USE_DOUBLE_QUOTES" value="false" />
|
<option name="USE_DOUBLE_QUOTES" value="false" />
|
||||||
|
@ -108,12 +108,25 @@ class RedisService {
|
|||||||
return redisTool.reviveDates(deserialized);
|
return redisTool.reviveDates(deserialized);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async updateUser(qq: string, updates: Partial<IUser>): Promise<IUser> {
|
||||||
|
const existing = await this.getObject<IUser>(`user:${qq}`);
|
||||||
|
if (!existing) {
|
||||||
|
throw new Error(`用户 ${qq} 不存在`);
|
||||||
|
}
|
||||||
|
const updatedUser = { ...existing, ...updates, updatedAt: new Date() };
|
||||||
|
await this.persistUser(updatedUser);
|
||||||
|
return updatedUser;
|
||||||
|
}
|
||||||
|
|
||||||
public async persistUser<T extends IUser>(user: T): Promise<void> {
|
public async persistUser<T extends IUser>(user: T): Promise<void> {
|
||||||
try {
|
try {
|
||||||
await this.setObject(`user:${user.qq}`, user);
|
await Promise.all([
|
||||||
await Persistence.writeDataLocal(user.name, user);
|
this.setObject(`user:${user.qq}`, user),
|
||||||
|
Persistence.writeDataLocal(user.name, user),
|
||||||
|
]);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error(err);
|
logger.error(err);
|
||||||
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,15 +137,18 @@ class RedisService {
|
|||||||
try {
|
try {
|
||||||
const fromRedis = await this.getObject<IUser>(`user:${qq}`);
|
const fromRedis = await this.getObject<IUser>(`user:${qq}`);
|
||||||
if (fromRedis) return fromRedis;
|
if (fromRedis) return fromRedis;
|
||||||
|
|
||||||
const fromLocal = await Persistence.readDataLocal<IUser>(username);
|
const fromLocal = await Persistence.readDataLocal<IUser>(username);
|
||||||
if (fromLocal) {
|
if (fromLocal) {
|
||||||
await this.setObject(`user:${qq}`, fromLocal);
|
await this.setObject(`user:${qq}`, fromLocal);
|
||||||
return fromLocal;
|
return fromLocal;
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.error(`用户${username},qq${qq}不存在!`);
|
logger.error(`用户${username},qq${qq}不存在!`);
|
||||||
return undefined;
|
return undefined;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error(err);
|
logger.error(err);
|
||||||
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -144,11 +160,10 @@ class RedisService {
|
|||||||
password: '114514',
|
password: '114514',
|
||||||
createdAt: new Date(),
|
createdAt: new Date(),
|
||||||
};
|
};
|
||||||
let test = redisTool.reviveDates(testData);
|
|
||||||
logger.debug(test);
|
await this.persistUser(testData);
|
||||||
await this.setObject('test', test);
|
const user = await this.fetchUser('114514', 'Jerry');
|
||||||
const push = await this.getObject('test');
|
logger.debug(user);
|
||||||
logger.debug(push);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user