aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/activitypub
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-02-04 11:26:51 +0100
committerChocobozzz <me@florianbigard.com>2020-02-04 11:26:51 +0100
commit0ffd6d32c13b3b59f96a212ebfd324ba06cbdf1f (patch)
treecdc6a598e1f274cae7db15e66ad52962a85ee248 /server/models/activitypub
parent9a11f73392928c81d4a478191e126d3ec754f781 (diff)
downloadPeerTube-0ffd6d32c13b3b59f96a212ebfd324ba06cbdf1f.tar.gz
PeerTube-0ffd6d32c13b3b59f96a212ebfd324ba06cbdf1f.tar.zst
PeerTube-0ffd6d32c13b3b59f96a212ebfd324ba06cbdf1f.zip
Use a singleton for model cache
Diffstat (limited to 'server/models/activitypub')
-rw-r--r--server/models/activitypub/actor.ts78
1 files changed, 36 insertions, 42 deletions
diff --git a/server/models/activitypub/actor.ts b/server/models/activitypub/actor.ts
index 00e8dc954..9e8303a7b 100644
--- a/server/models/activitypub/actor.ts
+++ b/server/models/activitypub/actor.ts
@@ -48,6 +48,7 @@ import {
48} from '../../typings/models' 48} from '../../typings/models'
49import * as Bluebird from 'bluebird' 49import * as Bluebird from 'bluebird'
50import { Op, Transaction, literal } from 'sequelize' 50import { Op, Transaction, literal } from 'sequelize'
51import { ModelCache } from '@server/models/model-cache'
51 52
52enum ScopeNames { 53enum ScopeNames {
53 FULL = 'FULL' 54 FULL = 'FULL'
@@ -276,9 +277,6 @@ export class ActorModel extends Model<ActorModel> {
276 }) 277 })
277 VideoChannel: VideoChannelModel 278 VideoChannel: VideoChannelModel
278 279
279 private static localNameCache: { [ id: string ]: any } = {}
280 private static localUrlCache: { [ id: string ]: any } = {}
281
282 static load (id: number): Bluebird<MActor> { 280 static load (id: number): Bluebird<MActor> {
283 return ActorModel.unscoped().findByPk(id) 281 return ActorModel.unscoped().findByPk(id)
284 } 282 }
@@ -345,54 +343,50 @@ export class ActorModel extends Model<ActorModel> {
345 } 343 }
346 344
347 static loadLocalByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorFull> { 345 static loadLocalByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorFull> {
348 // The server actor never change, so we can easily cache it 346 const fun = () => {
349 if (preferredUsername === SERVER_ACTOR_NAME && ActorModel.localNameCache[preferredUsername]) { 347 const query = {
350 return Bluebird.resolve(ActorModel.localNameCache[preferredUsername]) 348 where: {
351 } 349 preferredUsername,
350 serverId: null
351 },
352 transaction
353 }
352 354
353 const query = { 355 return ActorModel.scope(ScopeNames.FULL)
354 where: { 356 .findOne(query)
355 preferredUsername,
356 serverId: null
357 },
358 transaction
359 } 357 }
360 358
361 return ActorModel.scope(ScopeNames.FULL) 359 return ModelCache.Instance.doCache({
362 .findOne(query) 360 cacheType: 'local-actor-name',
363 .then(actor => { 361 key: preferredUsername,
364 if (preferredUsername === SERVER_ACTOR_NAME) { 362 // The server actor never change, so we can easily cache it
365 ActorModel.localNameCache[preferredUsername] = actor 363 whitelist: () => preferredUsername === SERVER_ACTOR_NAME,
366 } 364 fun
367 365 })
368 return actor
369 })
370 } 366 }
371 367
372 static loadLocalUrlByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorUrl> { 368 static loadLocalUrlByName (preferredUsername: string, transaction?: Transaction): Bluebird<MActorUrl> {
373 // The server actor never change, so we can easily cache it 369 const fun = () => {
374 if (preferredUsername === SERVER_ACTOR_NAME && ActorModel.localUrlCache[preferredUsername]) { 370 const query = {
375 return Bluebird.resolve(ActorModel.localUrlCache[preferredUsername]) 371 attributes: [ 'url' ],
376 } 372 where: {
373 preferredUsername,
374 serverId: null
375 },
376 transaction
377 }
377 378
378 const query = { 379 return ActorModel.unscoped()
379 attributes: [ 'url' ], 380 .findOne(query)
380 where: {
381 preferredUsername,
382 serverId: null
383 },
384 transaction
385 } 381 }
386 382
387 return ActorModel.unscoped() 383 return ModelCache.Instance.doCache({
388 .findOne(query) 384 cacheType: 'local-actor-name',
389 .then(actor => { 385 key: preferredUsername,
390 if (preferredUsername === SERVER_ACTOR_NAME) { 386 // The server actor never change, so we can easily cache it
391 ActorModel.localUrlCache[preferredUsername] = actor 387 whitelist: () => preferredUsername === SERVER_ACTOR_NAME,
392 } 388 fun
393 389 })
394 return actor
395 })
396 } 390 }
397 391
398 static loadByNameAndHost (preferredUsername: string, host: string): Bluebird<MActorFull> { 392 static loadByNameAndHost (preferredUsername: string, host: string): Bluebird<MActorFull> {