bump ver,system.get更换为同步操作

This commit is contained in:
undefined committed 2020-09-26 18:39:15 +08:00
1 parent 99b7d33157
commit 03d199fe9d
19 files changed
+100 -111

No files matched your search

+25 -46
View File
@@ -1,72 +1,45 @@
import Lru from 'lru-cache';
import { NumberKeys } from '../typeutils';
import { SystemKeys } from '../interface';
import * as db from '../service/db';
import * as bus from '../service/bus';
const coll = db.collection('system');
const cache: Record<string, any> = {};
const cache = new Lru<string, any>({
maxAge: 5000,
});
export async function get<K extends keyof SystemKeys>(_id: K): Promise<SystemKeys[K]> {
const res = cache.get(_id);
if (res !== undefined) return res;
const doc = await coll.findOne({ _id });
if (doc) {
cache.set(_id, doc.value);
return doc.value;
}
return null;
export function get<K extends keyof SystemKeys>(key: K): SystemKeys[K] {
return cache[key];
}
export async function getMany<
export function getMany<
A extends keyof SystemKeys, B extends keyof SystemKeys,
>(keys: [A, B]): Promise<[SystemKeys[A], SystemKeys[B]]>
export async function getMany<
>(keys: [A, B]): [SystemKeys[A], SystemKeys[B]]
export function getMany<
A extends keyof SystemKeys, B extends keyof SystemKeys, C extends keyof SystemKeys,
>(keys: [A, B, C]): Promise<[SystemKeys[A], SystemKeys[B], SystemKeys[C]]>
export async function getMany<
>(keys: [A, B, C]): [SystemKeys[A], SystemKeys[B], SystemKeys[C]]
export function getMany<
A extends keyof SystemKeys, B extends keyof SystemKeys, C extends keyof SystemKeys,
D extends keyof SystemKeys,
>(keys: [A, B, C, D]): Promise<[SystemKeys[A], SystemKeys[B], SystemKeys[C], SystemKeys[D]]>
export async function getMany<
>(keys: [A, B, C, D]): [SystemKeys[A], SystemKeys[B], SystemKeys[C], SystemKeys[D]]
export function getMany<
A extends keyof SystemKeys, B extends keyof SystemKeys, C extends keyof SystemKeys,
D extends keyof SystemKeys, E extends keyof SystemKeys,
>(keys: [A, B, C, D, E]): Promise<[SystemKeys[A], SystemKeys[B], SystemKeys[C], SystemKeys[D], SystemKeys[E]]>
export async function getMany<
>(keys: [A, B, C, D, E]): [SystemKeys[A], SystemKeys[B], SystemKeys[C], SystemKeys[D], SystemKeys[E]]
export function getMany<
A extends keyof SystemKeys, B extends keyof SystemKeys, C extends keyof SystemKeys,
D extends keyof SystemKeys, E extends keyof SystemKeys, F extends keyof SystemKeys,
>(keys: [A, B, C, D, E, F]): Promise<[SystemKeys[A], SystemKeys[B], SystemKeys[C], SystemKeys[D], SystemKeys[E], SystemKeys[F]]>
export async function getMany(keys: (keyof SystemKeys)[]): Promise<any[]>
export async function getMany(keys: string[]): Promise<any[]> {
const r = [];
let success = true;
for (const key of keys) {
const res = cache.get(key);
if (res !== undefined) r.push(res);
else success = false;
}
if (success) return r;
const docs = await coll.find({ _id: { $in: keys } }).toArray();
const dict = {};
const res = [];
for (const doc of docs) {
dict[doc._id] = doc.value;
}
for (const key of keys) {
res.push(dict[key] || null);
}
return res;
>(keys: [A, B, C, D, E, F]): [SystemKeys[A], SystemKeys[B], SystemKeys[C], SystemKeys[D], SystemKeys[E], SystemKeys[F]]
export function getMany(keys: (keyof SystemKeys)[]): any[]
export function getMany(keys: string[]): any[] {
return keys.map((key) => cache[key]);
}
export async function set<K extends keyof SystemKeys>(_id: K, value: SystemKeys[K]) {
cache.set(_id, value);
const res = await coll.findOneAndUpdate(
{ _id },
{ $set: { value } },
{ upsert: true, returnOriginal: false },
);
cache[_id] = res.value.value;
return res.value.value;
}
@@ -78,10 +51,16 @@ export async function inc<K extends NumberKeys<SystemKeys>>(_id: K) {
{ $inc: { value: 1 } },
{ upsert: true, returnOriginal: false },
);
cache.set(_id, res.value.value);
cache[_id] = res.value.value;
return res.value.value;
}
(async () => {
const config = await coll.find({}).toArray();
for (const i of config) cache[i._id] = i.value;
bus.emit('database/config');
})();
global.Hydro.model.system = {
get,
getMany,