mirror of
https://iceshrimp.dev/limepotato/jormungandr-bite.git
synced 2024-11-29 21:37:34 -07:00
Improve API performance
This commit is contained in:
parent
43dd6b687d
commit
1dea826fb5
1 changed files with 9 additions and 0 deletions
|
@ -3,12 +3,18 @@ import { User } from '../../models/entities/user';
|
||||||
import { Users, AccessTokens, Apps } from '../../models';
|
import { Users, AccessTokens, Apps } from '../../models';
|
||||||
import { AccessToken } from '../../models/entities/access-token';
|
import { AccessToken } from '../../models/entities/access-token';
|
||||||
|
|
||||||
|
const cache = {} as Record<string, User>;
|
||||||
|
|
||||||
export default async (token: string): Promise<[User | null | undefined, AccessToken | null | undefined]> => {
|
export default async (token: string): Promise<[User | null | undefined, AccessToken | null | undefined]> => {
|
||||||
if (token == null) {
|
if (token == null) {
|
||||||
return [null, null];
|
return [null, null];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isNativeToken(token)) {
|
if (isNativeToken(token)) {
|
||||||
|
if (cache[token]) { // TODO: キャッシュされてから一定時間経過していたら破棄する
|
||||||
|
return [cache[token], null];
|
||||||
|
}
|
||||||
|
|
||||||
// Fetch user
|
// Fetch user
|
||||||
const user = await Users
|
const user = await Users
|
||||||
.findOne({ token });
|
.findOne({ token });
|
||||||
|
@ -17,8 +23,11 @@ export default async (token: string): Promise<[User | null | undefined, AccessTo
|
||||||
throw new Error('user not found');
|
throw new Error('user not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cache[token] = user;
|
||||||
|
|
||||||
return [user, null];
|
return [user, null];
|
||||||
} else {
|
} else {
|
||||||
|
// TODO: cache
|
||||||
const accessToken = await AccessTokens.findOne({
|
const accessToken = await AccessTokens.findOne({
|
||||||
where: [{
|
where: [{
|
||||||
hash: token.toLowerCase() // app
|
hash: token.toLowerCase() // app
|
||||||
|
|
Loading…
Reference in a new issue