diff options
author | Chocobozzz <me@florianbigard.com> | 2023-01-10 11:09:30 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-01-10 11:56:08 +0100 |
commit | 8c4bbd946d2247c2e239cbbf8773d2d31c1a57aa (patch) | |
tree | 4e7d3711cccb095d4356c59554bdfd4e16502832 /server/models/shared/model-cache.ts | |
parent | a8749f7c3b137f433e6944bb99fd721a6f0cfc1e (diff) | |
download | PeerTube-8c4bbd946d2247c2e239cbbf8773d2d31c1a57aa.tar.gz PeerTube-8c4bbd946d2247c2e239cbbf8773d2d31c1a57aa.tar.zst PeerTube-8c4bbd946d2247c2e239cbbf8773d2d31c1a57aa.zip |
Refactor model utils
Diffstat (limited to 'server/models/shared/model-cache.ts')
-rw-r--r-- | server/models/shared/model-cache.ts | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/server/models/shared/model-cache.ts b/server/models/shared/model-cache.ts new file mode 100644 index 000000000..3651267e7 --- /dev/null +++ b/server/models/shared/model-cache.ts | |||
@@ -0,0 +1,90 @@ | |||
1 | import { Model } from 'sequelize-typescript' | ||
2 | import { logger } from '@server/helpers/logger' | ||
3 | |||
4 | type ModelCacheType = | ||
5 | 'local-account-name' | ||
6 | | 'local-actor-name' | ||
7 | | 'local-actor-url' | ||
8 | | 'load-video-immutable-id' | ||
9 | | 'load-video-immutable-url' | ||
10 | |||
11 | type DeleteKey = | ||
12 | 'video' | ||
13 | |||
14 | class ModelCache { | ||
15 | |||
16 | private static instance: ModelCache | ||
17 | |||
18 | private readonly localCache: { [id in ModelCacheType]: Map<string, any> } = { | ||
19 | 'local-account-name': new Map(), | ||
20 | 'local-actor-name': new Map(), | ||
21 | 'local-actor-url': new Map(), | ||
22 | 'load-video-immutable-id': new Map(), | ||
23 | 'load-video-immutable-url': new Map() | ||
24 | } | ||
25 | |||
26 | private readonly deleteIds: { | ||
27 | [deleteKey in DeleteKey]: Map<number, { cacheType: ModelCacheType, key: string }[]> | ||
28 | } = { | ||
29 | video: new Map() | ||
30 | } | ||
31 | |||
32 | private constructor () { | ||
33 | } | ||
34 | |||
35 | static get Instance () { | ||
36 | return this.instance || (this.instance = new this()) | ||
37 | } | ||
38 | |||
39 | doCache<T extends Model> (options: { | ||
40 | cacheType: ModelCacheType | ||
41 | key: string | ||
42 | fun: () => Promise<T> | ||
43 | whitelist?: () => boolean | ||
44 | deleteKey?: DeleteKey | ||
45 | }) { | ||
46 | const { cacheType, key, fun, whitelist, deleteKey } = options | ||
47 | |||
48 | if (whitelist && whitelist() !== true) return fun() | ||
49 | |||
50 | const cache = this.localCache[cacheType] | ||
51 | |||
52 | if (cache.has(key)) { | ||
53 | logger.debug('Model cache hit for %s -> %s.', cacheType, key) | ||
54 | return Promise.resolve<T>(cache.get(key)) | ||
55 | } | ||
56 | |||
57 | return fun().then(m => { | ||
58 | if (!m) return m | ||
59 | |||
60 | if (!whitelist || whitelist()) cache.set(key, m) | ||
61 | |||
62 | if (deleteKey) { | ||
63 | const map = this.deleteIds[deleteKey] | ||
64 | if (!map.has(m.id)) map.set(m.id, []) | ||
65 | |||
66 | const a = map.get(m.id) | ||
67 | a.push({ cacheType, key }) | ||
68 | } | ||
69 | |||
70 | return m | ||
71 | }) | ||
72 | } | ||
73 | |||
74 | invalidateCache (deleteKey: DeleteKey, modelId: number) { | ||
75 | const map = this.deleteIds[deleteKey] | ||
76 | |||
77 | if (!map.has(modelId)) return | ||
78 | |||
79 | for (const toDelete of map.get(modelId)) { | ||
80 | logger.debug('Removing %s -> %d of model cache %s -> %s.', deleteKey, modelId, toDelete.cacheType, toDelete.key) | ||
81 | this.localCache[toDelete.cacheType].delete(toDelete.key) | ||
82 | } | ||
83 | |||
84 | map.delete(modelId) | ||
85 | } | ||
86 | } | ||
87 | |||
88 | export { | ||
89 | ModelCache | ||
90 | } | ||