1
0
Fork 0
mirror of synced 2024-06-15 00:44:41 +12:00

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 58734cd1f2
commit 0f4469e6b8
3 changed files with 32 additions and 0 deletions

View file

@ -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,

View file

@ -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

View file

@ -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(