Improve server performance

This commit is contained in:
syuilo
2021-03-18 10:49:14 +09:00
parent d3c8368b0c
commit 8aa089178a
4 changed files with 54 additions and 10 deletions

View File

@ -3,10 +3,16 @@ import { Instances } from '../models';
import { federationChart } from './chart';
import { genId } from '../misc/gen-id';
import { toPuny } from '../misc/convert-host';
import { Cache } from '../misc/cache';
const cache = new Cache<Instance>(1000 * 60 * 60);
export async function registerOrFetchInstanceDoc(host: string): Promise<Instance> {
host = toPuny(host);
const cached = cache.get(host);
if (cached) return cached;
const index = await Instances.findOne({ host });
if (index == null) {
@ -19,8 +25,10 @@ export async function registerOrFetchInstanceDoc(host: string): Promise<Instance
federationChart.update(true);
cache.set(host, i);
return i;
} else {
cache.set(host, index);
return index;
}
}