From e4a686b4a202f3212fdce6e789136ce826ac801d Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 27 Dec 2019 13:33:16 +0100 Subject: Cache some SQL queries --- server/models/account/account.ts | 16 +++++++++++++++- server/models/activitypub/actor.ts | 19 +++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) (limited to 'server/models') diff --git a/server/models/account/account.ts b/server/models/account/account.ts index a818a5a4d..a757b7203 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts @@ -27,7 +27,7 @@ import { VideoCommentModel } from '../video/video-comment' import { UserModel } from './user' import { AvatarModel } from '../avatar/avatar' import { VideoPlaylistModel } from '../video/video-playlist' -import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants' +import { CONSTRAINTS_FIELDS, SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants' import { FindOptions, IncludeOptions, Op, Transaction, WhereOptions } from 'sequelize' import { AccountBlocklistModel } from './account-blocklist' import { ServerBlocklistModel } from '../server/server-blocklist' @@ -218,6 +218,8 @@ export class AccountModel extends Model { }) BlockedAccounts: AccountBlocklistModel[] + private static cache: { [ id: string ]: any } = {} + @BeforeDestroy static async sendDeleteIfOwned (instance: AccountModel, options) { if (!instance.Actor) { @@ -245,6 +247,11 @@ export class AccountModel extends Model { } static loadLocalByName (name: string): Bluebird { + // The server actor never change, so we can easily cache it + if (name === SERVER_ACTOR_NAME && AccountModel.cache[name]) { + return Bluebird.resolve(AccountModel.cache[name]) + } + const query = { where: { [ Op.or ]: [ @@ -272,6 +279,13 @@ export class AccountModel extends Model { } return AccountModel.findOne(query) + .then(account => { + if (name === SERVER_ACTOR_NAME) { + AccountModel.cache[name] = account + } + + return account + }) } static loadByNameAndHost (name: string, host: string): Bluebird { diff --git a/server/models/activitypub/actor.ts b/server/models/activitypub/actor.ts index 71db44b2f..58b52ffb1 100644 --- a/server/models/activitypub/actor.ts +++ b/server/models/activitypub/actor.ts @@ -27,7 +27,7 @@ import { isActorPublicKeyValid } from '../../helpers/custom-validators/activitypub/actor' import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' -import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants' +import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONSTRAINTS_FIELDS, SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants' import { AccountModel } from '../account/account' import { AvatarModel } from '../avatar/avatar' import { ServerModel } from '../server/server' @@ -276,6 +276,8 @@ export class ActorModel extends Model { }) VideoChannel: VideoChannelModel + private static cache: { [ id: string ]: any } = {} + static load (id: number): Bluebird { return ActorModel.unscoped().findByPk(id) } @@ -342,6 +344,11 @@ 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.cache[preferredUsername]) { + return Bluebird.resolve(ActorModel.cache[preferredUsername]) + } + const query = { where: { preferredUsername, @@ -350,7 +357,15 @@ export class ActorModel extends Model { transaction } - return ActorModel.scope(ScopeNames.FULL).findOne(query) + return ActorModel.scope(ScopeNames.FULL) + .findOne(query) + .then(actor => { + if (preferredUsername === SERVER_ACTOR_NAME) { + ActorModel.cache[ preferredUsername ] = actor + } + + return actor + }) } static loadByNameAndHost (preferredUsername: string, host: string): Bluebird { -- cgit v1.2.3