From 0ffd6d32c13b3b59f96a212ebfd324ba06cbdf1f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 4 Feb 2020 11:26:51 +0100 Subject: Use a singleton for model cache --- server/models/activitypub/actor.ts | 78 ++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 42 deletions(-) (limited to 'server/models/activitypub/actor.ts') diff --git a/server/models/activitypub/actor.ts b/server/models/activitypub/actor.ts index 00e8dc954..9e8303a7b 100644 --- a/server/models/activitypub/actor.ts +++ b/server/models/activitypub/actor.ts @@ -48,6 +48,7 @@ import { } from '../../typings/models' import * as Bluebird from 'bluebird' import { Op, Transaction, literal } from 'sequelize' +import { ModelCache } from '@server/models/model-cache' enum ScopeNames { FULL = 'FULL' @@ -276,9 +277,6 @@ export class ActorModel extends Model { }) VideoChannel: VideoChannelModel - private static localNameCache: { [ id: string ]: any } = {} - private static localUrlCache: { [ id: string ]: any } = {} - static load (id: number): Bluebird { return ActorModel.unscoped().findByPk(id) } @@ -345,54 +343,50 @@ export class ActorModel extends Model { } static loadLocalByName (preferredUsername: string, transaction?: Transaction): Bluebird { - // The server actor never change, so we can easily cache it - if (preferredUsername === SERVER_ACTOR_NAME && ActorModel.localNameCache[preferredUsername]) { - return Bluebird.resolve(ActorModel.localNameCache[preferredUsername]) - } + const fun = () => { + const query = { + where: { + preferredUsername, + serverId: null + }, + transaction + } - const query = { - where: { - preferredUsername, - serverId: null - }, - transaction + return ActorModel.scope(ScopeNames.FULL) + .findOne(query) } - return ActorModel.scope(ScopeNames.FULL) - .findOne(query) - .then(actor => { - if (preferredUsername === SERVER_ACTOR_NAME) { - ActorModel.localNameCache[preferredUsername] = actor - } - - return actor - }) + return ModelCache.Instance.doCache({ + cacheType: 'local-actor-name', + key: preferredUsername, + // The server actor never change, so we can easily cache it + whitelist: () => preferredUsername === SERVER_ACTOR_NAME, + fun + }) } static loadLocalUrlByName (preferredUsername: string, transaction?: Transaction): Bluebird { - // The server actor never change, so we can easily cache it - if (preferredUsername === SERVER_ACTOR_NAME && ActorModel.localUrlCache[preferredUsername]) { - return Bluebird.resolve(ActorModel.localUrlCache[preferredUsername]) - } + const fun = () => { + const query = { + attributes: [ 'url' ], + where: { + preferredUsername, + serverId: null + }, + transaction + } - const query = { - attributes: [ 'url' ], - where: { - preferredUsername, - serverId: null - }, - transaction + return ActorModel.unscoped() + .findOne(query) } - return ActorModel.unscoped() - .findOne(query) - .then(actor => { - if (preferredUsername === SERVER_ACTOR_NAME) { - ActorModel.localUrlCache[preferredUsername] = actor - } - - return actor - }) + return ModelCache.Instance.doCache({ + cacheType: 'local-actor-name', + key: preferredUsername, + // The server actor never change, so we can easily cache it + whitelist: () => preferredUsername === SERVER_ACTOR_NAME, + fun + }) } static loadByNameAndHost (preferredUsername: string, host: string): Bluebird { -- cgit v1.2.3