Quick update to the app caching to improve performance even further, cache when an app doesn't have metadata/is invalid, meaning we don't need to poll the database everytime to see if the metadata doc exists.
This commit is contained in:
parent
4235aba7a2
commit
720fa12b21
|
@ -2,6 +2,9 @@ const redis = require("../redis/authRedis")
|
||||||
const { getCouch } = require("../db")
|
const { getCouch } = require("../db")
|
||||||
const { DocumentTypes } = require("../db/constants")
|
const { DocumentTypes } = require("../db/constants")
|
||||||
|
|
||||||
|
const AppState = {
|
||||||
|
INVALID: "invalid",
|
||||||
|
}
|
||||||
const EXPIRY_SECONDS = 3600
|
const EXPIRY_SECONDS = 3600
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -27,8 +30,25 @@ exports.getAppMetadata = async (appId, CouchDB = null) => {
|
||||||
// try cache
|
// try cache
|
||||||
let metadata = await client.get(appId)
|
let metadata = await client.get(appId)
|
||||||
if (!metadata) {
|
if (!metadata) {
|
||||||
metadata = await populateFromDB(appId, CouchDB)
|
let expiry = EXPIRY_SECONDS
|
||||||
client.store(appId, metadata, EXPIRY_SECONDS)
|
try {
|
||||||
|
metadata = await populateFromDB(appId, CouchDB)
|
||||||
|
} catch (err) {
|
||||||
|
// app DB left around, but no metadata, it is invalid
|
||||||
|
if (err && err.status === 404) {
|
||||||
|
metadata = { state: AppState.INVALID }
|
||||||
|
// don't expire the reference to an invalid app, it'll only be
|
||||||
|
// updated if a metadata doc actually gets stored (app is remade/reverted)
|
||||||
|
expiry = undefined
|
||||||
|
} else {
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
client.store(appId, metadata, expiry)
|
||||||
|
}
|
||||||
|
// we've stored in the cache an object to tell us that it is currently invalid
|
||||||
|
if (!metadata || metadata.state === AppState.INVALID) {
|
||||||
|
throw { status: 404, message: "No app metadata found" }
|
||||||
}
|
}
|
||||||
return metadata
|
return metadata
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue