diff options
author | Chocobozzz <me@florianbigard.com> | 2019-12-27 13:33:16 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-12-27 13:33:16 +0100 |
commit | e4a686b4a202f3212fdce6e789136ce826ac801d (patch) | |
tree | 1eaf2ced273f5f9f6f9212bfa8945fd1333be715 /server/models | |
parent | f27a885a4368776ffb8158c917c6e3f3d21ef670 (diff) | |
download | PeerTube-e4a686b4a202f3212fdce6e789136ce826ac801d.tar.gz PeerTube-e4a686b4a202f3212fdce6e789136ce826ac801d.tar.zst PeerTube-e4a686b4a202f3212fdce6e789136ce826ac801d.zip |
Cache some SQL queries
Diffstat (limited to 'server/models')
-rw-r--r-- | server/models/account/account.ts | 16 | ||||
-rw-r--r-- | server/models/activitypub/actor.ts | 19 |
2 files changed, 32 insertions, 3 deletions
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' | |||
27 | import { UserModel } from './user' | 27 | import { UserModel } from './user' |
28 | import { AvatarModel } from '../avatar/avatar' | 28 | import { AvatarModel } from '../avatar/avatar' |
29 | import { VideoPlaylistModel } from '../video/video-playlist' | 29 | import { VideoPlaylistModel } from '../video/video-playlist' |
30 | import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants' | 30 | import { CONSTRAINTS_FIELDS, SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants' |
31 | import { FindOptions, IncludeOptions, Op, Transaction, WhereOptions } from 'sequelize' | 31 | import { FindOptions, IncludeOptions, Op, Transaction, WhereOptions } from 'sequelize' |
32 | import { AccountBlocklistModel } from './account-blocklist' | 32 | import { AccountBlocklistModel } from './account-blocklist' |
33 | import { ServerBlocklistModel } from '../server/server-blocklist' | 33 | import { ServerBlocklistModel } from '../server/server-blocklist' |
@@ -218,6 +218,8 @@ export class AccountModel extends Model<AccountModel> { | |||
218 | }) | 218 | }) |
219 | BlockedAccounts: AccountBlocklistModel[] | 219 | BlockedAccounts: AccountBlocklistModel[] |
220 | 220 | ||
221 | private static cache: { [ id: string ]: any } = {} | ||
222 | |||
221 | @BeforeDestroy | 223 | @BeforeDestroy |
222 | static async sendDeleteIfOwned (instance: AccountModel, options) { | 224 | static async sendDeleteIfOwned (instance: AccountModel, options) { |
223 | if (!instance.Actor) { | 225 | if (!instance.Actor) { |
@@ -245,6 +247,11 @@ export class AccountModel extends Model<AccountModel> { | |||
245 | } | 247 | } |
246 | 248 | ||
247 | static loadLocalByName (name: string): Bluebird<MAccountDefault> { | 249 | static loadLocalByName (name: string): Bluebird<MAccountDefault> { |
250 | // The server actor never change, so we can easily cache it | ||
251 | if (name === SERVER_ACTOR_NAME && AccountModel.cache[name]) { | ||
252 | return Bluebird.resolve(AccountModel.cache[name]) | ||
253 | } | ||
254 | |||
248 | const query = { | 255 | const query = { |
249 | where: { | 256 | where: { |
250 | [ Op.or ]: [ | 257 | [ Op.or ]: [ |
@@ -272,6 +279,13 @@ export class AccountModel extends Model<AccountModel> { | |||
272 | } | 279 | } |
273 | 280 | ||
274 | return AccountModel.findOne(query) | 281 | return AccountModel.findOne(query) |
282 | .then(account => { | ||
283 | if (name === SERVER_ACTOR_NAME) { | ||
284 | AccountModel.cache[name] = account | ||
285 | } | ||
286 | |||
287 | return account | ||
288 | }) | ||
275 | } | 289 | } |
276 | 290 | ||
277 | static loadByNameAndHost (name: string, host: string): Bluebird<MAccountDefault> { | 291 | static loadByNameAndHost (name: string, host: string): Bluebird<MAccountDefault> { |
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' |
29 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' | 29 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' |
30 | import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants' | 30 | import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONSTRAINTS_FIELDS, SERVER_ACTOR_NAME, WEBSERVER } from '../../initializers/constants' |
31 | import { AccountModel } from '../account/account' | 31 | import { AccountModel } from '../account/account' |
32 | import { AvatarModel } from '../avatar/avatar' | 32 | import { AvatarModel } from '../avatar/avatar' |
33 | import { ServerModel } from '../server/server' | 33 | import { 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> { |