diff options
Diffstat (limited to 'server/models/model-cache.ts')
-rw-r--r-- | server/models/model-cache.ts | 39 |
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 | |||
11 | type DeleteKey = | ||
12 | 'video' | ||
9 | 13 | ||
10 | class ModelCache { | 14 | class 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 | ||
52 | export { | 87 | export { |