diff options
author | Chocobozzz <me@florianbigard.com> | 2019-08-09 11:32:40 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-08-09 11:32:40 +0200 |
commit | 557b13ae24019d9ab214bbea7eaa0f892c8f4b05 (patch) | |
tree | aa32396531acf93e3dfdb29880177813039ed77f /server/lib/oauth-model.ts | |
parent | c5407d7046168abb4098df1408e7aa84519cb61a (diff) | |
download | PeerTube-557b13ae24019d9ab214bbea7eaa0f892c8f4b05.tar.gz PeerTube-557b13ae24019d9ab214bbea7eaa0f892c8f4b05.tar.zst PeerTube-557b13ae24019d9ab214bbea7eaa0f892c8f4b05.zip |
Lazy load avatars
Diffstat (limited to 'server/lib/oauth-model.ts')
-rw-r--r-- | server/lib/oauth-model.ts | 34 |
1 files changed, 16 insertions, 18 deletions
diff --git a/server/lib/oauth-model.ts b/server/lib/oauth-model.ts index 45ac3e7c4..a1153e88a 100644 --- a/server/lib/oauth-model.ts +++ b/server/lib/oauth-model.ts | |||
@@ -4,13 +4,15 @@ import { logger } from '../helpers/logger' | |||
4 | import { UserModel } from '../models/account/user' | 4 | import { UserModel } from '../models/account/user' |
5 | import { OAuthClientModel } from '../models/oauth/oauth-client' | 5 | import { OAuthClientModel } from '../models/oauth/oauth-client' |
6 | import { OAuthTokenModel } from '../models/oauth/oauth-token' | 6 | import { OAuthTokenModel } from '../models/oauth/oauth-token' |
7 | import { CACHE } from '../initializers/constants' | 7 | import { LRU_CACHE } from '../initializers/constants' |
8 | import { Transaction } from 'sequelize' | 8 | import { Transaction } from 'sequelize' |
9 | import { CONFIG } from '../initializers/config' | 9 | import { CONFIG } from '../initializers/config' |
10 | import * as LRUCache from 'lru-cache' | ||
10 | 11 | ||
11 | type TokenInfo = { accessToken: string, refreshToken: string, accessTokenExpiresAt: Date, refreshTokenExpiresAt: Date } | 12 | type TokenInfo = { accessToken: string, refreshToken: string, accessTokenExpiresAt: Date, refreshTokenExpiresAt: Date } |
12 | let accessTokenCache: { [ accessToken: string ]: OAuthTokenModel } = {} | 13 | |
13 | let userHavingToken: { [ userId: number ]: string } = {} | 14 | const accessTokenCache = new LRUCache<string, OAuthTokenModel>({ max: LRU_CACHE.USER_TOKENS.MAX_SIZE }) |
15 | const userHavingToken = new LRUCache<number, string>({ max: LRU_CACHE.USER_TOKENS.MAX_SIZE }) | ||
14 | 16 | ||
15 | // --------------------------------------------------------------------------- | 17 | // --------------------------------------------------------------------------- |
16 | 18 | ||
@@ -21,18 +23,20 @@ function deleteUserToken (userId: number, t?: Transaction) { | |||
21 | } | 23 | } |
22 | 24 | ||
23 | function clearCacheByUserId (userId: number) { | 25 | function clearCacheByUserId (userId: number) { |
24 | const token = userHavingToken[userId] | 26 | const token = userHavingToken.get(userId) |
27 | |||
25 | if (token !== undefined) { | 28 | if (token !== undefined) { |
26 | accessTokenCache[ token ] = undefined | 29 | accessTokenCache.del(token) |
27 | userHavingToken[ userId ] = undefined | 30 | userHavingToken.del(userId) |
28 | } | 31 | } |
29 | } | 32 | } |
30 | 33 | ||
31 | function clearCacheByToken (token: string) { | 34 | function clearCacheByToken (token: string) { |
32 | const tokenModel = accessTokenCache[ token ] | 35 | const tokenModel = accessTokenCache.get(token) |
36 | |||
33 | if (tokenModel !== undefined) { | 37 | if (tokenModel !== undefined) { |
34 | userHavingToken[tokenModel.userId] = undefined | 38 | userHavingToken.del(tokenModel.userId) |
35 | accessTokenCache[ token ] = undefined | 39 | accessTokenCache.del(token) |
36 | } | 40 | } |
37 | } | 41 | } |
38 | 42 | ||
@@ -41,19 +45,13 @@ function getAccessToken (bearerToken: string) { | |||
41 | 45 | ||
42 | if (!bearerToken) return Bluebird.resolve(undefined) | 46 | if (!bearerToken) return Bluebird.resolve(undefined) |
43 | 47 | ||
44 | if (accessTokenCache[bearerToken] !== undefined) return Bluebird.resolve(accessTokenCache[bearerToken]) | 48 | if (accessTokenCache.has(bearerToken)) return Bluebird.resolve(accessTokenCache.get(bearerToken)) |
45 | 49 | ||
46 | return OAuthTokenModel.getByTokenAndPopulateUser(bearerToken) | 50 | return OAuthTokenModel.getByTokenAndPopulateUser(bearerToken) |
47 | .then(tokenModel => { | 51 | .then(tokenModel => { |
48 | if (tokenModel) { | 52 | if (tokenModel) { |
49 | // Reinit our cache | 53 | accessTokenCache.set(bearerToken, tokenModel) |
50 | if (Object.keys(accessTokenCache).length > CACHE.USER_TOKENS.MAX_SIZE) { | 54 | userHavingToken.set(tokenModel.userId, tokenModel.accessToken) |
51 | accessTokenCache = {} | ||
52 | userHavingToken = {} | ||
53 | } | ||
54 | |||
55 | accessTokenCache[ bearerToken ] = tokenModel | ||
56 | userHavingToken[ tokenModel.userId ] = tokenModel.accessToken | ||
57 | } | 55 | } |
58 | 56 | ||
59 | return tokenModel | 57 | return tokenModel |