diff options
Diffstat (limited to 'server/lib/files-cache/abstract-video-static-file-cache.ts')
-rw-r--r-- | server/lib/files-cache/abstract-video-static-file-cache.ts | 32 |
1 files changed, 10 insertions, 22 deletions
diff --git a/server/lib/files-cache/abstract-video-static-file-cache.ts b/server/lib/files-cache/abstract-video-static-file-cache.ts index 7512f2b9d..61837e0f8 100644 --- a/server/lib/files-cache/abstract-video-static-file-cache.ts +++ b/server/lib/files-cache/abstract-video-static-file-cache.ts | |||
@@ -1,41 +1,29 @@ | |||
1 | import * as AsyncLRU from 'async-lru' | ||
2 | import { createWriteStream, remove } from 'fs-extra' | 1 | import { createWriteStream, remove } from 'fs-extra' |
3 | import { logger } from '../../helpers/logger' | 2 | import { logger } from '../../helpers/logger' |
4 | import { VideoModel } from '../../models/video/video' | 3 | import { VideoModel } from '../../models/video/video' |
5 | import { fetchRemoteVideoStaticFile } from '../activitypub' | 4 | import { fetchRemoteVideoStaticFile } from '../activitypub' |
5 | import * as memoizee from 'memoizee' | ||
6 | 6 | ||
7 | export abstract class AbstractVideoStaticFileCache <T> { | 7 | export abstract class AbstractVideoStaticFileCache <T> { |
8 | 8 | ||
9 | protected lru | 9 | getFilePath: (params: T) => Promise<string> |
10 | 10 | ||
11 | abstract getFilePath (params: T): Promise<string> | 11 | abstract getFilePathImpl (params: T): Promise<string> |
12 | 12 | ||
13 | // Load and save the remote file, then return the local path from filesystem | 13 | // Load and save the remote file, then return the local path from filesystem |
14 | protected abstract loadRemoteFile (key: string): Promise<string> | 14 | protected abstract loadRemoteFile (key: string): Promise<string> |
15 | 15 | ||
16 | init (max: number, maxAge: number) { | 16 | init (max: number, maxAge: number) { |
17 | this.lru = new AsyncLRU({ | 17 | this.getFilePath = memoizee(this.getFilePathImpl, { |
18 | max, | ||
19 | maxAge, | 18 | maxAge, |
20 | load: (key, cb) => { | 19 | max, |
21 | this.loadRemoteFile(key) | 20 | promise: true, |
22 | .then(res => cb(null, res)) | 21 | dispose: (value: string) => { |
23 | .catch(err => cb(err)) | 22 | remove(value) |
23 | .then(() => logger.debug('%s evicted from %s', value, this.constructor.name)) | ||
24 | .catch(err => logger.error('Cannot remove %s from cache %s.', value, this.constructor.name, { err })) | ||
24 | } | 25 | } |
25 | }) | 26 | }) |
26 | |||
27 | this.lru.on('evict', (obj: { key: string, value: string }) => { | ||
28 | remove(obj.value) | ||
29 | .then(() => logger.debug('%s evicted from %s', obj.value, this.constructor.name)) | ||
30 | }) | ||
31 | } | ||
32 | |||
33 | protected loadFromLRU (key: string) { | ||
34 | return new Promise<string>((res, rej) => { | ||
35 | this.lru.get(key, (err, value) => { | ||
36 | err ? rej(err) : res(value) | ||
37 | }) | ||
38 | }) | ||
39 | } | 27 | } |
40 | 28 | ||
41 | protected saveRemoteVideoFileAndReturnPath (video: VideoModel, remoteStaticPath: string, destPath: string) { | 29 | protected saveRemoteVideoFileAndReturnPath (video: VideoModel, remoteStaticPath: string, destPath: string) { |