aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/model-cache.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-02-04 15:00:47 +0100
committerChocobozzz <me@florianbigard.com>2020-02-04 15:00:47 +0100
commit7eba5e1fa81c8e54cb8fe298a96e8070afa50921 (patch)
treea6bd4b13dc0d65addfa82fcf200f2d1853a0723a /server/models/model-cache.ts
parente436baf0b00b3ecf3731aeba02437ebe4906ac5f (diff)
downloadPeerTube-7eba5e1fa81c8e54cb8fe298a96e8070afa50921.tar.gz
PeerTube-7eba5e1fa81c8e54cb8fe298a96e8070afa50921.tar.zst
PeerTube-7eba5e1fa81c8e54cb8fe298a96e8070afa50921.zip
Add model cache for video
When fetching only immutable attributes
Diffstat (limited to 'server/models/model-cache.ts')
-rw-r--r--server/models/model-cache.ts39
1 files changed, 37 insertions, 2 deletions
diff --git a/server/models/model-cache.ts b/server/models/model-cache.ts
index bfa163b6b..8afe3834f 100644
--- a/server/models/model-cache.ts
+++ b/server/models/model-cache.ts
@@ -6,6 +6,10 @@ type ModelCacheType =
6 'local-account-name' 6 'local-account-name'
7 | 'local-actor-name' 7 | 'local-actor-name'
8 | 'local-actor-url' 8 | 'local-actor-url'
9 | 'video-immutable'
10
11type DeleteKey =
12 'video'
9 13
10class ModelCache { 14class ModelCache {
11 15
@@ -14,7 +18,14 @@ class ModelCache {
14 private readonly localCache: { [id in ModelCacheType]: Map<string, any> } = { 18 private readonly localCache: { [id in ModelCacheType]: Map<string, any> } = {
15 'local-account-name': new Map(), 19 'local-account-name': new Map(),
16 'local-actor-name': new Map(), 20 'local-actor-name': new Map(),
17 'local-actor-url': new Map() 21 'local-actor-url': new Map(),
22 'video-immutable': new Map()
23 }
24
25 private readonly deleteIds: {
26 [deleteKey in DeleteKey]: Map<number, { cacheType: ModelCacheType, key: string }[]>
27 } = {
28 video: new Map()
18 } 29 }
19 30
20 private constructor () { 31 private constructor () {
@@ -29,8 +40,9 @@ class ModelCache {
29 key: string 40 key: string
30 fun: () => Bluebird<T> 41 fun: () => Bluebird<T>
31 whitelist?: () => boolean 42 whitelist?: () => boolean
43 deleteKey?: DeleteKey
32 }) { 44 }) {
33 const { cacheType, key, fun, whitelist } = options 45 const { cacheType, key, fun, whitelist, deleteKey } = options
34 46
35 if (whitelist && whitelist() !== true) return fun() 47 if (whitelist && whitelist() !== true) return fun()
36 48
@@ -42,11 +54,34 @@ class ModelCache {
42 } 54 }
43 55
44 return fun().then(m => { 56 return fun().then(m => {
57 if (!m) return m
58
45 if (!whitelist || whitelist()) cache.set(key, m) 59 if (!whitelist || whitelist()) cache.set(key, m)
46 60
61 if (deleteKey) {
62 const map = this.deleteIds[deleteKey]
63 if (!map.has(m.id)) map.set(m.id, [])
64
65 const a = map.get(m.id)
66 a.push({ cacheType, key })
67 }
68
47 return m 69 return m
48 }) 70 })
49 } 71 }
72
73 invalidateCache (deleteKey: DeleteKey, modelId: number) {
74 const map = this.deleteIds[deleteKey]
75
76 if (!map.has(modelId)) return
77
78 for (const toDelete of map.get(modelId)) {
79 logger.debug('Removing %s -> %d of model cache %s -> %s.', deleteKey, modelId, toDelete.cacheType, toDelete.key)
80 this.localCache[toDelete.cacheType].delete(toDelete.key)
81 }
82
83 map.delete(modelId)
84 }
50} 85}
51 86
52export { 87export {