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

@ -2,8 +2,11 @@ import isNativeToken from './common/is-native-token';
import { User } from '../../models/entities/user';
import { Users, AccessTokens, Apps } from '../../models';
import { AccessToken } from '../../models/entities/access-token';
import { Cache } from '../../misc/cache';
const cache = {} as Record<string, User>;
// TODO: TypeORMのカスタムキャッシュプロバイダを使っても良いかも
// ref. https://github.com/typeorm/typeorm/blob/master/docs/caching.md
const cache = new Cache<User>(1000 * 60 * 60);
export default async (token: string): Promise<[User | null | undefined, AccessToken | null | undefined]> => {
if (token == null) {
@ -11,8 +14,9 @@ export default async (token: string): Promise<[User | null | undefined, AccessTo
}
if (isNativeToken(token)) {
if (cache[token]) { // TODO: キャッシュされてから一定時間経過していたら破棄する
return [cache[token], null];
const cached = cache.get(token);
if (cached) {
return [cached, null];
}
// Fetch user
@ -23,7 +27,7 @@ export default async (token: string): Promise<[User | null | undefined, AccessTo
throw new Error('user not found');
}
cache[token] = user;
cache.set(token, user);
return [user, null];
} else {