Use a singleton for model cache

This commit is contained in:
Chocobozzz 2020-02-04 11:26:51 +01:00
parent 9a11f73392
commit 0ffd6d32c1
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
3 changed files with 124 additions and 79 deletions

View File

@ -32,8 +32,9 @@ import { FindOptions, IncludeOptions, Op, Transaction, WhereOptions } from 'sequ
import { AccountBlocklistModel } from './account-blocklist' import { AccountBlocklistModel } from './account-blocklist'
import { ServerBlocklistModel } from '../server/server-blocklist' import { ServerBlocklistModel } from '../server/server-blocklist'
import { ActorFollowModel } from '../activitypub/actor-follow' import { ActorFollowModel } from '../activitypub/actor-follow'
import { MAccountActor, MAccountDefault, MAccountSummaryFormattable, MAccountFormattable, MAccountAP } from '../../typings/models' import { MAccountActor, MAccountAP, MAccountDefault, MAccountFormattable, MAccountSummaryFormattable } from '../../typings/models'
import * as Bluebird from 'bluebird' import * as Bluebird from 'bluebird'
import { ModelCache } from '@server/models/model-cache'
export enum ScopeNames { export enum ScopeNames {
SUMMARY = 'SUMMARY' SUMMARY = 'SUMMARY'
@ -218,8 +219,6 @@ export class AccountModel extends Model<AccountModel> {
}) })
BlockedAccounts: AccountBlocklistModel[] BlockedAccounts: AccountBlocklistModel[]
private static cache: { [ id: string ]: any } = {}
@BeforeDestroy @BeforeDestroy
static async sendDeleteIfOwned (instance: AccountModel, options) { static async sendDeleteIfOwned (instance: AccountModel, options) {
if (!instance.Actor) { if (!instance.Actor) {
@ -247,45 +246,43 @@ export class AccountModel extends Model<AccountModel> {
} }
static loadLocalByName (name: string): Bluebird<MAccountDefault> { static loadLocalByName (name: string): Bluebird<MAccountDefault> {
// The server actor never change, so we can easily cache it const fun = () => {
if (name === SERVER_ACTOR_NAME && AccountModel.cache[name]) { const query = {
return Bluebird.resolve(AccountModel.cache[name]) where: {
} [Op.or]: [
{
const query = { userId: {
where: { [Op.ne]: null
[Op.or]: [ }
{ },
userId: { {
[Op.ne]: null applicationId: {
[Op.ne]: null
}
} }
}, ]
},
include: [
{ {
applicationId: { model: ActorModel,
[Op.ne]: null required: true,
where: {
preferredUsername: name
} }
} }
] ]
}, }
include: [
{ return AccountModel.findOne(query)
model: ActorModel,
required: true,
where: {
preferredUsername: name
}
}
]
} }
return AccountModel.findOne(query) return ModelCache.Instance.doCache({
.then(account => { cacheType: 'local-account-name',
if (name === SERVER_ACTOR_NAME) { key: name,
AccountModel.cache[name] = account fun,
} // The server actor never change, so we can easily cache it
whitelist: () => name === SERVER_ACTOR_NAME
return account })
})
} }
static loadByNameAndHost (name: string, host: string): Bluebird<MAccountDefault> { static loadByNameAndHost (name: string, host: string): Bluebird<MAccountDefault> {

View File

@ -48,6 +48,7 @@ import {
} from '../../typings/models' } from '../../typings/models'
import * as Bluebird from 'bluebird' import * as Bluebird from 'bluebird'
import { Op, Transaction, literal } from 'sequelize' import { Op, Transaction, literal } from 'sequelize'
import { ModelCache } from '@server/models/model-cache'
enum ScopeNames { enum ScopeNames {
FULL = 'FULL' FULL = 'FULL'
@ -276,9 +277,6 @@ export class ActorModel extends Model<ActorModel> {
}) })
VideoChannel: VideoChannelModel VideoChannel: VideoChannelModel
private static localNameCache: { [ id: string ]: any } = {}
private static localUrlCache: { [ id: string ]: any } = {}
static load (id: number): Bluebird<MActor> { static load (id: number): Bluebird<MActor> {
return ActorModel.unscoped().findByPk(id) return ActorModel.unscoped().findByPk(id)
} }
@ -345,54 +343,50 @@ export class ActorModel extends Model<ActorModel> {
} }
static loadLocalByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorFull> { static loadLocalByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorFull> {
// The server actor never change, so we can easily cache it const fun = () => {
if (preferredUsername === SERVER_ACTOR_NAME && ActorModel.localNameCache[preferredUsername]) { const query = {
return Bluebird.resolve(ActorModel.localNameCache[preferredUsername]) where: {
preferredUsername,
serverId: null
},
transaction
}
return ActorModel.scope(ScopeNames.FULL)
.findOne(query)
} }
const query = { return ModelCache.Instance.doCache({
where: { cacheType: 'local-actor-name',
preferredUsername, key: preferredUsername,
serverId: null // The server actor never change, so we can easily cache it
}, whitelist: () => preferredUsername === SERVER_ACTOR_NAME,
transaction fun
} })
return ActorModel.scope(ScopeNames.FULL)
.findOne(query)
.then(actor => {
if (preferredUsername === SERVER_ACTOR_NAME) {
ActorModel.localNameCache[preferredUsername] = actor
}
return actor
})
} }
static loadLocalUrlByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorUrl> { static loadLocalUrlByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorUrl> {
// The server actor never change, so we can easily cache it const fun = () => {
if (preferredUsername === SERVER_ACTOR_NAME && ActorModel.localUrlCache[preferredUsername]) { const query = {
return Bluebird.resolve(ActorModel.localUrlCache[preferredUsername]) attributes: [ 'url' ],
where: {
preferredUsername,
serverId: null
},
transaction
}
return ActorModel.unscoped()
.findOne(query)
} }
const query = { return ModelCache.Instance.doCache({
attributes: [ 'url' ], cacheType: 'local-actor-name',
where: { key: preferredUsername,
preferredUsername, // The server actor never change, so we can easily cache it
serverId: null whitelist: () => preferredUsername === SERVER_ACTOR_NAME,
}, fun
transaction })
}
return ActorModel.unscoped()
.findOne(query)
.then(actor => {
if (preferredUsername === SERVER_ACTOR_NAME) {
ActorModel.localUrlCache[preferredUsername] = actor
}
return actor
})
} }
static loadByNameAndHost (preferredUsername: string, host: string): Bluebird<MActorFull> { static loadByNameAndHost (preferredUsername: string, host: string): Bluebird<MActorFull> {

View File

@ -0,0 +1,54 @@
import { Model } from 'sequelize-typescript'
import * as Bluebird from 'bluebird'
import { logger } from '@server/helpers/logger'
type ModelCacheType =
'local-account-name'
| 'local-actor-name'
| 'local-actor-url'
class ModelCache {
private static instance: ModelCache
private readonly localCache: { [id in ModelCacheType]: Map<string, any> } = {
'local-account-name': new Map(),
'local-actor-name': new Map(),
'local-actor-url': new Map()
}
private constructor () {
}
static get Instance () {
return this.instance || (this.instance = new this())
}
doCache<T extends Model> (options: {
cacheType: ModelCacheType
key: string
fun: () => Bluebird<T>
whitelist?: () => boolean
}) {
const { cacheType, key, fun, whitelist } = options
if (whitelist && whitelist() !== true) return fun()
const cache = this.localCache[cacheType]
if (cache.has(key)) {
logger.debug('Model cache hit for %s -> %s.', cacheType, key)
return Bluebird.resolve<T>(cache.get(key))
}
return fun().then(m => {
if (!whitelist || whitelist()) cache.set(key, m)
return m
})
}
}
export {
ModelCache
}