2020-02-04 11:26:51 +01:00
|
|
|
import { Model } from 'sequelize-typescript'
|
|
|
|
import { logger } from '@server/helpers/logger'
|
|
|
|
|
|
|
|
type ModelCacheType =
|
|
|
|
'local-account-name'
|
|
|
|
| 'local-actor-name'
|
|
|
|
| 'local-actor-url'
|
2020-02-04 15:45:41 +01:00
|
|
|
| 'load-video-immutable-id'
|
|
|
|
| 'load-video-immutable-url'
|
2020-02-04 15:00:47 +01:00
|
|
|
|
|
|
|
type DeleteKey =
|
|
|
|
'video'
|
2020-02-04 11:26:51 +01:00
|
|
|
|
|
|
|
class ModelCache {
|
|
|
|
|
|
|
|
private static instance: ModelCache
|
|
|
|
|
|
|
|
private readonly localCache: { [id in ModelCacheType]: Map<string, any> } = {
|
|
|
|
'local-account-name': new Map(),
|
|
|
|
'local-actor-name': new Map(),
|
2020-02-04 15:00:47 +01:00
|
|
|
'local-actor-url': new Map(),
|
2020-02-04 15:45:41 +01:00
|
|
|
'load-video-immutable-id': new Map(),
|
|
|
|
'load-video-immutable-url': new Map()
|
2020-02-04 15:00:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private readonly deleteIds: {
|
|
|
|
[deleteKey in DeleteKey]: Map<number, { cacheType: ModelCacheType, key: string }[]>
|
|
|
|
} = {
|
|
|
|
video: new Map()
|
2020-02-04 11:26:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private constructor () {
|
|
|
|
}
|
|
|
|
|
|
|
|
static get Instance () {
|
|
|
|
return this.instance || (this.instance = new this())
|
|
|
|
}
|
|
|
|
|
|
|
|
doCache<T extends Model> (options: {
|
|
|
|
cacheType: ModelCacheType
|
|
|
|
key: string
|
2020-12-08 14:30:29 +01:00
|
|
|
fun: () => Promise<T>
|
2020-02-04 11:26:51 +01:00
|
|
|
whitelist?: () => boolean
|
2020-02-04 15:00:47 +01:00
|
|
|
deleteKey?: DeleteKey
|
2020-02-04 11:26:51 +01:00
|
|
|
}) {
|
2020-02-04 15:00:47 +01:00
|
|
|
const { cacheType, key, fun, whitelist, deleteKey } = options
|
2020-02-04 11:26:51 +01:00
|
|
|
|
|
|
|
if (whitelist && whitelist() !== true) return fun()
|
|
|
|
|
|
|
|
const cache = this.localCache[cacheType]
|
|
|
|
|
|
|
|
if (cache.has(key)) {
|
|
|
|
logger.debug('Model cache hit for %s -> %s.', cacheType, key)
|
2021-08-27 14:32:44 +02:00
|
|
|
return Promise.resolve<T>(cache.get(key))
|
2020-02-04 11:26:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return fun().then(m => {
|
2020-02-04 15:00:47 +01:00
|
|
|
if (!m) return m
|
|
|
|
|
2020-02-04 11:26:51 +01:00
|
|
|
if (!whitelist || whitelist()) cache.set(key, m)
|
|
|
|
|
2020-02-04 15:00:47 +01:00
|
|
|
if (deleteKey) {
|
|
|
|
const map = this.deleteIds[deleteKey]
|
|
|
|
if (!map.has(m.id)) map.set(m.id, [])
|
|
|
|
|
|
|
|
const a = map.get(m.id)
|
|
|
|
a.push({ cacheType, key })
|
|
|
|
}
|
|
|
|
|
2020-02-04 11:26:51 +01:00
|
|
|
return m
|
|
|
|
})
|
|
|
|
}
|
2020-02-04 15:00:47 +01:00
|
|
|
|
|
|
|
invalidateCache (deleteKey: DeleteKey, modelId: number) {
|
|
|
|
const map = this.deleteIds[deleteKey]
|
|
|
|
|
|
|
|
if (!map.has(modelId)) return
|
|
|
|
|
|
|
|
for (const toDelete of map.get(modelId)) {
|
|
|
|
logger.debug('Removing %s -> %d of model cache %s -> %s.', deleteKey, modelId, toDelete.cacheType, toDelete.key)
|
|
|
|
this.localCache[toDelete.cacheType].delete(toDelete.key)
|
|
|
|
}
|
|
|
|
|
|
|
|
map.delete(modelId)
|
|
|
|
}
|
2020-02-04 11:26:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
ModelCache
|
|
|
|
}
|