Cache meta

This commit is contained in:
syuilo
2019-04-24 08:11:19 +09:00
parent 8a55bdd89d
commit 5aa5896b22
34 changed files with 62 additions and 55 deletions

View File

@@ -1,7 +1,11 @@
import { Meta } from '../models/entities/meta';
import { getConnection } from 'typeorm';
export default async function(): Promise<Meta> {
let cache: Meta;
export async function fetchMeta(noCache = false): Promise<Meta> {
if (!noCache && cache) return cache;
return await getConnection().transaction(async transactionalEntityManager => {
// バグでレコードが複数出来てしまっている可能性があるので新しいIDを優先する
const meta = await transactionalEntityManager.findOne(Meta, {
@@ -11,11 +15,21 @@ export default async function(): Promise<Meta> {
});
if (meta) {
cache = meta;
return meta;
} else {
return await transactionalEntityManager.save(Meta, {
const saved = await transactionalEntityManager.save(Meta, {
id: 'x'
}) as Meta;
cache = saved;
return saved;
}
});
}
setInterval(() => {
fetchMeta(true).then(meta => {
cache = meta;
});
}, 5000);