1
0
Fork 0
mirror of https://github.com/Chocobozzz/PeerTube.git synced 2025-10-04 02:09:37 +02:00

Limit user tokens cache

This commit is contained in:
Chocobozzz 2019-03-19 14:23:17 +01:00
parent 9f79ade627
commit d74d29ad9e
No known key found for this signature in database
GPG key ID: 583A612D890159BE
13 changed files with 36 additions and 25 deletions

View file

@ -4,12 +4,12 @@ import { logger } from '../helpers/logger'
import { UserModel } from '../models/account/user'
import { OAuthClientModel } from '../models/oauth/oauth-client'
import { OAuthTokenModel } from '../models/oauth/oauth-token'
import { CONFIG } from '../initializers/constants'
import { CONFIG, CACHE } from '../initializers/constants'
import { Transaction } from 'sequelize'
type TokenInfo = { accessToken: string, refreshToken: string, accessTokenExpiresAt: Date, refreshTokenExpiresAt: Date }
const accessTokenCache: { [ accessToken: string ]: OAuthTokenModel } = {}
const userHavingToken: { [ userId: number ]: string } = {}
let accessTokenCache: { [ accessToken: string ]: OAuthTokenModel } = {}
let userHavingToken: { [ userId: number ]: string } = {}
// ---------------------------------------------------------------------------
@ -43,6 +43,12 @@ function getAccessToken (bearerToken: string) {
return OAuthTokenModel.getByTokenAndPopulateUser(bearerToken)
.then(tokenModel => {
if (tokenModel) {
// Reinit our cache
if (Object.keys(accessTokenCache).length > CACHE.USER_TOKENS.MAX_SIZE) {
accessTokenCache = {}
userHavingToken = {}
}
accessTokenCache[ bearerToken ] = tokenModel
userHavingToken[ tokenModel.userId ] = tokenModel.accessToken
}