Merge pull request #6022 from Budibase/fix/caching-helper
withCache wrapper for working with redis
This commit is contained in:
commit
dc1229f30e
|
@ -18,6 +18,7 @@ exports.Databases = {
|
|||
APP_METADATA: "appMetadata",
|
||||
QUERY_VARS: "queryVars",
|
||||
LICENSES: "license",
|
||||
DATA_CACHE: "data_cache",
|
||||
}
|
||||
|
||||
exports.SEPARATOR = SEPARATOR
|
||||
|
|
|
@ -14,6 +14,7 @@ const {
|
|||
const { getGlobalDB, getTenantId } = require("@budibase/backend-core/tenancy")
|
||||
const env = require("../../../environment")
|
||||
const { googleCallbackUrl, oidcCallbackUrl } = require("./auth")
|
||||
const { withCache } = require("../../../utilities/redis")
|
||||
|
||||
const BB_TENANT_CDN = "https://tenants.cdn.budi.live"
|
||||
|
||||
|
@ -249,8 +250,12 @@ exports.configChecklist = async function (ctx) {
|
|||
const tenantId = getTenantId()
|
||||
|
||||
try {
|
||||
// TODO: Watch get started video
|
||||
const ONE_MINUTE = 600
|
||||
|
||||
ctx.body = await withCache(
|
||||
`checklist:${tenantId}`,
|
||||
ONE_MINUTE,
|
||||
async () => {
|
||||
let apps = []
|
||||
if (!env.MULTI_TENANCY || tenantId) {
|
||||
// Apps exist
|
||||
|
@ -279,8 +284,7 @@ exports.configChecklist = async function (ctx) {
|
|||
limit: 1,
|
||||
})
|
||||
)
|
||||
|
||||
ctx.body = {
|
||||
return {
|
||||
apps: {
|
||||
checked: apps.length > 0,
|
||||
label: "Create your first app",
|
||||
|
@ -302,6 +306,8 @@ exports.configChecklist = async function (ctx) {
|
|||
link: "/builder/portal/manage/auth",
|
||||
},
|
||||
}
|
||||
}
|
||||
)
|
||||
} catch (err) {
|
||||
ctx.throw(err.status, err)
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
const { Client, utils } = require("@budibase/backend-core/redis")
|
||||
const { newid } = require("@budibase/backend-core/utils")
|
||||
const env = require("../environment")
|
||||
|
||||
function getExpirySecondsForDB(db) {
|
||||
switch (db) {
|
||||
|
@ -12,7 +13,7 @@ function getExpirySecondsForDB(db) {
|
|||
}
|
||||
}
|
||||
|
||||
let pwResetClient, invitationClient
|
||||
let pwResetClient, invitationClient, cachingClient
|
||||
|
||||
function getClient(db) {
|
||||
switch (db) {
|
||||
|
@ -20,6 +21,8 @@ function getClient(db) {
|
|||
return pwResetClient
|
||||
case utils.Databases.INVITATIONS:
|
||||
return invitationClient
|
||||
case utils.Databases.DATA_CACHE:
|
||||
return cachingClient
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -45,8 +48,10 @@ async function getACode(db, code, deleteCode = true) {
|
|||
exports.init = async () => {
|
||||
pwResetClient = new Client(utils.Databases.PW_RESETS)
|
||||
invitationClient = new Client(utils.Databases.INVITATIONS)
|
||||
cachingClient = new Client(utils.Databases.DATA_CACHE)
|
||||
await pwResetClient.init()
|
||||
await invitationClient.init()
|
||||
await cachingClient.init()
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -104,3 +109,23 @@ exports.checkInviteCode = async (inviteCode, deleteCode = true) => {
|
|||
throw "Invitation is not valid or has expired, please request a new one."
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: move into backend-core
|
||||
exports.withCache = async (key, ttl, fetchFn) => {
|
||||
const cachedValue = await cachingClient.get(key)
|
||||
if (cachedValue) {
|
||||
return cachedValue
|
||||
}
|
||||
|
||||
try {
|
||||
const fetchedValue = await fetchFn()
|
||||
|
||||
if (!env.isTest()) {
|
||||
await cachingClient.store(key, fetchedValue, ttl)
|
||||
}
|
||||
return fetchedValue
|
||||
} catch (err) {
|
||||
console.error("Error calling fetch function", err)
|
||||
throw err
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue