|
|
|
@ -1,6 +1,5 @@ |
|
|
|
const { Client, utils } = require("@budibase/backend-core/redis") |
|
|
|
const { newid } = require("@budibase/backend-core/utils") |
|
|
|
const env = require("../environment") |
|
|
|
|
|
|
|
function getExpirySecondsForDB(db) { |
|
|
|
switch (db) { |
|
|
|
@ -13,7 +12,7 @@ function getExpirySecondsForDB(db) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
let pwResetClient, invitationClient, cachingClient |
|
|
|
let pwResetClient, invitationClient |
|
|
|
|
|
|
|
function getClient(db) { |
|
|
|
switch (db) { |
|
|
|
@ -21,8 +20,6 @@ function getClient(db) { |
|
|
|
return pwResetClient |
|
|
|
case utils.Databases.INVITATIONS: |
|
|
|
return invitationClient |
|
|
|
case utils.Databases.GENERIC_CACHE: |
|
|
|
return cachingClient |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -48,10 +45,8 @@ 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() |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
@ -110,23 +105,3 @@ 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 |
|
|
|
} |
|
|
|
} |
|
|
|
|