aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/activitypub
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2019-12-27 13:33:16 +0100
committerChocobozzz <me@florianbigard.com>2019-12-27 13:33:16 +0100
commite4a686b4a202f3212fdce6e789136ce826ac801d (patch)
tree1eaf2ced273f5f9f6f9212bfa8945fd1333be715 /server/models/activitypub
parentf27a885a4368776ffb8158c917c6e3f3d21ef670 (diff)
downloadPeerTube-e4a686b4a202f3212fdce6e789136ce826ac801d.tar.gz
PeerTube-e4a686b4a202f3212fdce6e789136ce826ac801d.tar.zst
PeerTube-e4a686b4a202f3212fdce6e789136ce826ac801d.zip
Cache some SQL queries
Diffstat (limited to 'server/models/activitypub')
-rw-r--r--server/models/activitypub/actor.ts19
1 files changed, 17 insertions, 2 deletions
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 {
27 isActorPublicKeyValid 27 isActorPublicKeyValid
28} from '../../helpers/custom-validators/activitypub/actor' 28} from '../../helpers/custom-validators/activitypub/actor'
29import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' 29import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
30import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants' 30import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONSTRAINTS_FIELDS, SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants'
31import { AccountModel } from '../account/account' 31import { AccountModel } from '../account/account'
32import { AvatarModel } from '../avatar/avatar' 32import { AvatarModel } from '../avatar/avatar'
33import { ServerModel } from '../server/server' 33import { ServerModel } from '../server/server'
@@ -276,6 +276,8 @@ export class ActorModel extends Model<ActorModel> {
276 }) 276 })
277 VideoChannel: VideoChannelModel 277 VideoChannel: VideoChannelModel
278 278
279 private static cache: { [ id: string ]: any } = {}
280
279 static load (id: number): Bluebird<MActor> { 281 static load (id: number): Bluebird<MActor> {
280 return ActorModel.unscoped().findByPk(id) 282 return ActorModel.unscoped().findByPk(id)
281 } 283 }
@@ -342,6 +344,11 @@ export class ActorModel extends Model<ActorModel> {
342 } 344 }
343 345
344 static loadLocalByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorFull> { 346 static loadLocalByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorFull> {
347 // The server actor never change, so we can easily cache it
348 if (preferredUsername === SERVER_ACTOR_NAME && ActorModel.cache[preferredUsername]) {
349 return Bluebird.resolve(ActorModel.cache[preferredUsername])
350 }
351
345 const query = { 352 const query = {
346 where: { 353 where: {
347 preferredUsername, 354 preferredUsername,
@@ -350,7 +357,15 @@ export class ActorModel extends Model<ActorModel> {
350 transaction 357 transaction
351 } 358 }
352 359
353 return ActorModel.scope(ScopeNames.FULL).findOne(query) 360 return ActorModel.scope(ScopeNames.FULL)
361 .findOne(query)
362 .then(actor => {
363 if (preferredUsername === SERVER_ACTOR_NAME) {
364 ActorModel.cache[ preferredUsername ] = actor
365 }
366
367 return actor
368 })
354 } 369 }
355 370
356 static loadByNameAndHost (preferredUsername: string, host: string): Bluebird<MActorFull> { 371 static loadByNameAndHost (preferredUsername: string, host: string): Bluebird<MActorFull> {