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:
mike12345567 2021-12-17 17:56:28 +00:00
parent 85aa2c27b5
commit 52eef17da0
3 changed files with 32 additions and 0 deletions

View File

@ -37,6 +37,9 @@ module.exports = (
try { try {
// check the actual user is authenticated first, try header or cookie // check the actual user is authenticated first, try header or cookie
const headerToken = ctx.request.headers[Headers.TOKEN] const headerToken = ctx.request.headers[Headers.TOKEN]
if (headerToken) {
throw { name: "JsonWebTokenError" }
}
const authCookie = getCookie(ctx, Cookies.Auth) || openJwt(headerToken) const authCookie = getCookie(ctx, Cookies.Auth) || openJwt(headerToken)
let authenticated = false, let authenticated = false,
user = null, user = null,

View File

@ -15,6 +15,8 @@ class QueryRunner {
this.transformer = input.transformer this.transformer = input.transformer
this.queryId = input.queryId this.queryId = input.queryId
this.noRecursiveQuery = flags.noRecursiveQuery this.noRecursiveQuery = flags.noRecursiveQuery
this.cachedVariables = []
this.hasRerun = false
} }
async execute() { async execute() {
@ -44,6 +46,19 @@ class QueryRunner {
rows = runner.execute() 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 // needs to an array for next step
if (!Array.isArray(rows)) { if (!Array.isArray(rows)) {
rows = [rows] rows = [rows]
@ -89,6 +104,8 @@ class QueryRunner {
if (!value) { if (!value) {
value = await this.runAnotherQuery(queryId, parameters) value = await this.runAnotherQuery(queryId, parameters)
await threadUtils.storeDynamicVariable(queryId, name, value) await threadUtils.storeDynamicVariable(queryId, name, value)
} else {
this.cachedVariables.push({ queryId, name })
} }
return value return value
} }
@ -125,6 +142,8 @@ class QueryRunner {
data: responses[i].rows, data: responses[i].rows,
info: responses[i].extra, info: responses[i].extra,
}) })
// make sure its known that this uses dynamic variables in case it fails
this.hasDynamicVariables = true
} }
} }
return parameters return parameters

View File

@ -38,6 +38,16 @@ exports.checkCacheForDynamicVariable = async (queryId, variable) => {
return cache.get(makeVariableKey(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) => { exports.storeDynamicVariable = async (queryId, variable, value) => {
const cache = await getClient() const cache = await getClient()
await cache.store( await cache.store(