forked from tsai/budibase
11 changed files with 63 additions and 5 deletions
@ -1,3 +1,4 @@ |
|||||
module.exports = { |
module.exports = { |
||||
user: require("./src/cache/user"), |
user: require("./src/cache/user"), |
||||
|
app: require("./src/cache/appMetadata"), |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,35 @@ |
|||||
|
const redis = require("../redis/authRedis") |
||||
|
const { getDB } = require("../db") |
||||
|
const { DocumentTypes } = require("../db/constants") |
||||
|
|
||||
|
const EXPIRY_SECONDS = 3600 |
||||
|
|
||||
|
/** |
||||
|
* The default populate app metadata function |
||||
|
*/ |
||||
|
const populateFromDB = async appId => { |
||||
|
return getDB(appId, { skip_setup: true }).get(DocumentTypes.APP_METADATA) |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* Get the requested app metadata by id. |
||||
|
* Use redis cache to first read the app metadata. |
||||
|
* If not present fallback to loading the app metadata directly and re-caching. |
||||
|
* @param {*} appId the id of the app to get metadata from. |
||||
|
* @returns {object} the app metadata. |
||||
|
*/ |
||||
|
exports.getAppMetadata = async appId => { |
||||
|
const client = await redis.getAppClient() |
||||
|
// try cache
|
||||
|
let metadata = await client.get(appId) |
||||
|
if (!metadata) { |
||||
|
metadata = await populateFromDB(appId) |
||||
|
client.store(appId, metadata, EXPIRY_SECONDS) |
||||
|
} |
||||
|
return metadata |
||||
|
} |
||||
|
|
||||
|
exports.invalidateAppMetadata = async appId => { |
||||
|
const client = await redis.getAppClient() |
||||
|
await client.delete(appId) |
||||
|
} |
||||
Loading…
Reference in new issue