Cache some SQL queries

This commit is contained in:
Chocobozzz 2019-12-27 13:33:16 +01:00
parent f27a885a43
commit e4a686b4a2
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
2 changed files with 32 additions and 3 deletions

View File

@ -27,7 +27,7 @@ import { VideoCommentModel } from '../video/video-comment'
import { UserModel } from './user' import { UserModel } from './user'
import { AvatarModel } from '../avatar/avatar' import { AvatarModel } from '../avatar/avatar'
import { VideoPlaylistModel } from '../video/video-playlist' 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 { FindOptions, IncludeOptions, Op, Transaction, WhereOptions } from 'sequelize'
import { AccountBlocklistModel } from './account-blocklist' import { AccountBlocklistModel } from './account-blocklist'
import { ServerBlocklistModel } from '../server/server-blocklist' import { ServerBlocklistModel } from '../server/server-blocklist'
@ -218,6 +218,8 @@ 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) {
@ -245,6 +247,11 @@ 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
if (name === SERVER_ACTOR_NAME && AccountModel.cache[name]) {
return Bluebird.resolve(AccountModel.cache[name])
}
const query = { const query = {
where: { where: {
[ Op.or ]: [ [ Op.or ]: [
@ -272,6 +279,13 @@ export class AccountModel extends Model<AccountModel> {
} }
return AccountModel.findOne(query) return AccountModel.findOne(query)
.then(account => {
if (name === SERVER_ACTOR_NAME) {
AccountModel.cache[name] = account
}
return account
})
} }
static loadByNameAndHost (name: string, host: string): Bluebird<MAccountDefault> { static loadByNameAndHost (name: string, host: string): Bluebird<MAccountDefault> {

View File

@ -27,7 +27,7 @@ import {
isActorPublicKeyValid isActorPublicKeyValid
} from '../../helpers/custom-validators/activitypub/actor' } from '../../helpers/custom-validators/activitypub/actor'
import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' 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 { AccountModel } from '../account/account'
import { AvatarModel } from '../avatar/avatar' import { AvatarModel } from '../avatar/avatar'
import { ServerModel } from '../server/server' import { ServerModel } from '../server/server'
@ -276,6 +276,8 @@ export class ActorModel extends Model<ActorModel> {
}) })
VideoChannel: VideoChannelModel VideoChannel: VideoChannelModel
private static cache: { [ 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)
} }
@ -342,6 +344,11 @@ 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
if (preferredUsername === SERVER_ACTOR_NAME && ActorModel.cache[preferredUsername]) {
return Bluebird.resolve(ActorModel.cache[preferredUsername])
}
const query = { const query = {
where: { where: {
preferredUsername, preferredUsername,
@ -350,7 +357,15 @@ export class ActorModel extends Model<ActorModel> {
transaction 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<MActorFull> { static loadByNameAndHost (preferredUsername: string, host: string): Bluebird<MActorFull> {