]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video.ts
Add model cache for video
[github/Chocobozzz/PeerTube.git] / server / models / video / video.ts
index 1ec8d717e5d4c8592b0bec73c6f2844d6002e818..9e02d163f5f52ddf3b39f962ac7fdd8639d3800e 100644 (file)
@@ -120,7 +120,7 @@ import {
   MVideoFormattableDetails,
   MVideoForUser,
   MVideoFullLight,
-  MVideoIdThumbnail,
+  MVideoIdThumbnail, MVideoImmutable,
   MVideoThumbnail,
   MVideoThumbnailBlacklist,
   MVideoWithAllFiles,
@@ -132,6 +132,7 @@ import { MThumbnail } from '../../typings/models/video/thumbnail'
 import { VideoFile } from '@shared/models/videos/video-file.model'
 import { getHLSDirectory, getTorrentFileName, getTorrentFilePath, getVideoFilename, getVideoFilePath } from '@server/lib/video-paths'
 import validator from 'validator'
+import { ModelCache } from '@server/models/model-cache'
 
 export enum ScopeNames {
   AVAILABLE_FOR_LIST_IDS = 'AVAILABLE_FOR_LIST_IDS',
@@ -1074,6 +1075,11 @@ export class VideoModel extends Model<VideoModel> {
     return undefined
   }
 
+  @BeforeDestroy
+  static invalidateCache (instance: VideoModel) {
+    ModelCache.Instance.invalidateCache('video', instance.id)
+  }
+
   static listLocal (): Bluebird<MVideoWithAllFiles[]> {
     const query = {
       where: {
@@ -1468,6 +1474,28 @@ export class VideoModel extends Model<VideoModel> {
     ]).findOne(options)
   }
 
+  static loadImmutableAttributes (id: number | string, t?: Transaction): Bluebird<MVideoImmutable> {
+    const fun = () => {
+      const where = buildWhereIdOrUUID(id)
+      const options = {
+        attributes: [
+          'id', 'url', 'uuid'
+        ],
+        where,
+        transaction: t
+      }
+
+      return VideoModel.unscoped().findOne(options)
+    }
+
+    return ModelCache.Instance.doCache({
+      cacheType: 'video-immutable',
+      key: '' + id,
+      deleteKey: 'video',
+      fun
+    })
+  }
+
   static loadWithRights (id: number | string, t?: Transaction): Bluebird<MVideoWithRights> {
     const where = buildWhereIdOrUUID(id)
     const options = {