Adding query invalidation, when a query fails that has dynamic variables it will invalidate the cache value for all dynamic variable values.
This commit is contained in:
parent
85aa2c27b5
commit
52eef17da0
|
@ -37,6 +37,9 @@ module.exports = (
|
|||
try {
|
||||
// check the actual user is authenticated first, try header or cookie
|
||||
const headerToken = ctx.request.headers[Headers.TOKEN]
|
||||
if (headerToken) {
|
||||
throw { name: "JsonWebTokenError" }
|
||||
}
|
||||
const authCookie = getCookie(ctx, Cookies.Auth) || openJwt(headerToken)
|
||||
let authenticated = false,
|
||||
user = null,
|
||||
|
|
|
@ -15,6 +15,8 @@ class QueryRunner {
|
|||
this.transformer = input.transformer
|
||||
this.queryId = input.queryId
|
||||
this.noRecursiveQuery = flags.noRecursiveQuery
|
||||
this.cachedVariables = []
|
||||
this.hasRerun = false
|
||||
}
|
||||
|
||||
async execute() {
|
||||
|
@ -44,6 +46,19 @@ class QueryRunner {
|
|||
rows = runner.execute()
|
||||
}
|
||||
|
||||
// if the request fails we retry once, invalidating the cached value
|
||||
if (
|
||||
info &&
|
||||
info.code >= 400 &&
|
||||
this.cachedVariables.length > 0 &&
|
||||
!this.hasRerun
|
||||
) {
|
||||
this.hasRerun = true
|
||||
// invalidate the cache value
|
||||
await threadUtils.invalidateDynamicVariables(this.cachedVariables)
|
||||
return this.execute()
|
||||
}
|
||||
|
||||
// needs to an array for next step
|
||||
if (!Array.isArray(rows)) {
|
||||
rows = [rows]
|
||||
|
@ -89,6 +104,8 @@ class QueryRunner {
|
|||
if (!value) {
|
||||
value = await this.runAnotherQuery(queryId, parameters)
|
||||
await threadUtils.storeDynamicVariable(queryId, name, value)
|
||||
} else {
|
||||
this.cachedVariables.push({ queryId, name })
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
@ -125,6 +142,8 @@ class QueryRunner {
|
|||
data: responses[i].rows,
|
||||
info: responses[i].extra,
|
||||
})
|
||||
// make sure its known that this uses dynamic variables in case it fails
|
||||
this.hasDynamicVariables = true
|
||||
}
|
||||
}
|
||||
return parameters
|
||||
|
|
|
@ -38,6 +38,16 @@ exports.checkCacheForDynamicVariable = async (queryId, variable) => {
|
|||
return cache.get(makeVariableKey(queryId, variable))
|
||||
}
|
||||
|
||||
exports.invalidateDynamicVariables = async cachedVars => {
|
||||
let promises = []
|
||||
for (let variable of cachedVars) {
|
||||
promises.push(
|
||||
client.delete(makeVariableKey(variable.queryId, variable.name))
|
||||
)
|
||||
}
|
||||
await Promise.all(promises)
|
||||
}
|
||||
|
||||
exports.storeDynamicVariable = async (queryId, variable, value) => {
|
||||
const cache = await getClient()
|
||||
await cache.store(
|
||||
|
|
Loading…
Reference in New Issue