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 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'server/models/account') 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 { -- cgit v1.2.3