From 943e5193905908dd1f2800d8810c635d86e3b28f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 4 Feb 2020 15:45:41 +0100 Subject: Don't refresh videos when processing views It allows us to use a cache --- server/models/model-cache.ts | 6 ++++-- server/models/video/video.ts | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 10 deletions(-) (limited to 'server/models') diff --git a/server/models/model-cache.ts b/server/models/model-cache.ts index 8afe3834f..a87f99aa2 100644 --- a/server/models/model-cache.ts +++ b/server/models/model-cache.ts @@ -6,7 +6,8 @@ type ModelCacheType = 'local-account-name' | 'local-actor-name' | 'local-actor-url' - | 'video-immutable' + | 'load-video-immutable-id' + | 'load-video-immutable-url' type DeleteKey = 'video' @@ -19,7 +20,8 @@ class ModelCache { 'local-account-name': new Map(), 'local-actor-name': new Map(), 'local-actor-url': new Map(), - 'video-immutable': new Map() + 'load-video-immutable-id': new Map(), + 'load-video-immutable-url': new Map() } private readonly deleteIds: { diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 9e02d163f..5964526a9 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -145,6 +145,7 @@ export enum ScopeNames { WITH_USER_HISTORY = 'WITH_USER_HISTORY', WITH_STREAMING_PLAYLISTS = 'WITH_STREAMING_PLAYLISTS', WITH_USER_ID = 'WITH_USER_ID', + WITH_IMMUTABLE_ATTRIBUTES = 'WITH_IMMUTABLE_ATTRIBUTES', WITH_THUMBNAILS = 'WITH_THUMBNAILS' } @@ -188,6 +189,9 @@ export type AvailableForListIDsOptions = { } @Scopes(() => ({ + [ScopeNames.WITH_IMMUTABLE_ATTRIBUTES]: { + attributes: [ 'id', 'url', 'uuid', 'remote' ] + }, [ScopeNames.FOR_API]: (options: ForAPIOptions) => { const query: FindOptions = { include: [ @@ -1476,20 +1480,16 @@ export class VideoModel extends Model { static loadImmutableAttributes (id: number | string, t?: Transaction): Bluebird { const fun = () => { - const where = buildWhereIdOrUUID(id) - const options = { - attributes: [ - 'id', 'url', 'uuid' - ], - where, + const query = { + where: buildWhereIdOrUUID(id), transaction: t } - return VideoModel.unscoped().findOne(options) + return VideoModel.scope(ScopeNames.WITH_IMMUTABLE_ATTRIBUTES).findOne(query) } return ModelCache.Instance.doCache({ - cacheType: 'video-immutable', + cacheType: 'load-video-immutable-id', key: '' + id, deleteKey: 'video', fun @@ -1559,6 +1559,26 @@ export class VideoModel extends Model { return VideoModel.scope(ScopeNames.WITH_THUMBNAILS).findOne(query) } + static loadByUrlImmutableAttributes (url: string, transaction?: Transaction): Bluebird { + const fun = () => { + const query: FindOptions = { + where: { + url + }, + transaction + } + + return VideoModel.scope(ScopeNames.WITH_IMMUTABLE_ATTRIBUTES).findOne(query) + } + + return ModelCache.Instance.doCache({ + cacheType: 'load-video-immutable-url', + key: url, + deleteKey: 'video', + fun + }) + } + static loadByUrlAndPopulateAccount (url: string, transaction?: Transaction): Bluebird { const query: FindOptions = { where: { -- cgit v1.2.3