Cache meta

This commit is contained in:
syuilo 2019-04-24 08:11:19 +09:00
parent 8a55bdd89d
commit 5aa5896b22
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69
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);

View file

@ -1,4 +1,4 @@
import fetchMeta from './fetch-meta';
import { fetchMeta } from './fetch-meta';
import { ILocalUser } from '../models/entities/user';
import { Users } from '../models';
import { ensure } from '../prelude/ensure';

View file

@ -1,5 +1,5 @@
import { emojiRegex } from './emoji-regex';
import fetchMeta from './fetch-meta';
import { fetchMeta } from './fetch-meta';
import { Emojis } from '../models';
const basic10: Record<string, string> = {