Use bulk
This commit is contained in:
parent
4fe7e67dd5
commit
ebcb7718b8
|
@ -46,6 +46,25 @@ export default class BaseCache {
|
||||||
await client.store(key, value, ttl)
|
await client.store(key, value, ttl)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bulk write to the cache.
|
||||||
|
*/
|
||||||
|
async bulkStore(
|
||||||
|
data: Record<string, any>,
|
||||||
|
ttl: number | null = null,
|
||||||
|
opts = { useTenancy: true }
|
||||||
|
) {
|
||||||
|
if (opts.useTenancy) {
|
||||||
|
data = Object.entries(data).reduce((acc, [key, value]) => {
|
||||||
|
acc[generateTenantKey(key)] = value
|
||||||
|
return acc
|
||||||
|
}, {} as Record<string, any>)
|
||||||
|
}
|
||||||
|
|
||||||
|
const client = await this.getClient()
|
||||||
|
await client.bulkStore(data, ttl)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove from cache.
|
* Remove from cache.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -3,7 +3,6 @@ import { getDocWritethroughClient } from "../redis/init"
|
||||||
import { AnyDocument, Database } from "@budibase/types"
|
import { AnyDocument, Database } from "@budibase/types"
|
||||||
|
|
||||||
import { JobQueue, createQueue } from "../queue"
|
import { JobQueue, createQueue } from "../queue"
|
||||||
import * as context from "../context"
|
|
||||||
import * as dbUtils from "../db"
|
import * as dbUtils from "../db"
|
||||||
|
|
||||||
let CACHE: BaseCache | null = null
|
let CACHE: BaseCache | null = null
|
||||||
|
@ -101,9 +100,10 @@ export class DocWritethrough {
|
||||||
}
|
}
|
||||||
|
|
||||||
private async storeToCache(cache: BaseCache, data: Record<string, any>) {
|
private async storeToCache(cache: BaseCache, data: Record<string, any>) {
|
||||||
for (const [key, value] of Object.entries(data)) {
|
data = Object.entries(data).reduce((acc, [key, value]) => {
|
||||||
const cacheKey = this.cacheKeyPrefix + ":data:" + key
|
acc[this.cacheKeyPrefix + ":data:" + key] = { key, value }
|
||||||
await cache.store(cacheKey, { key, value }, undefined)
|
return acc
|
||||||
}
|
}, {} as Record<string, any>)
|
||||||
|
await cache.bulkStore(data, null)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue