Namespace key in redis by db

This commit is contained in:
Adria Navarro 2024-03-01 13:41:40 +01:00
parent 6bd7e6b374
commit eb78103764
1 changed files with 5 additions and 3 deletions

View File

@ -23,6 +23,7 @@ export class DocWritethrough {
private _docId: string
private writeRateMs: number
private cacheKeyPrefix: string
private docInfoCacheKey: string
constructor(
@ -33,7 +34,8 @@ export class DocWritethrough {
this.db = db
this._docId = docId
this.writeRateMs = writeRateMs
this.docInfoCacheKey = `${this.db.name}:${this.docId}:info`
this.cacheKeyPrefix = `${this.db.name}:${this.docId}`
this.docInfoCacheKey = `${this.cacheKeyPrefix}:info`
}
get docId() {
@ -85,7 +87,7 @@ export class DocWritethrough {
private async storeToCache(cache: BaseCache, data: Record<string, any>) {
for (const [key, value] of Object.entries(data)) {
const cacheKey = this.docId + ":data:" + key
const cacheKey = this.cacheKeyPrefix + ":data:" + key
await cache.store(cacheKey, { key, value }, undefined)
}
}
@ -98,7 +100,7 @@ export class DocWritethrough {
doc = { _id: this.docId }
}
const keysToPersist = await cache.keys(`${this.docId}:data:*`)
const keysToPersist = await cache.keys(`${this.cacheKeyPrefix}:data:*`)
for (const key of keysToPersist) {
const data = await cache.get(key, { useTenancy: false })
doc[data.key] = data.value