feat(Server): invalidate caches on changes

This commit is contained in:
41666 2020-02-04 08:10:04 -05:00
parent 0e70e2590b
commit 4ffd5014df
No known key found for this signature in database
GPG key ID: BC51D07640DC10AF
3 changed files with 32 additions and 2 deletions

View file

@ -89,7 +89,11 @@ module.exports = (R, $) => {
...(categories != null ? { categories } : {}),
})
ctx.body = { ok: true }
$.P.invalidate(id)
$.discord.invalidate(id)
})
R.get('/api/admin/servers', async ctx => {
@ -131,8 +135,16 @@ module.exports = (R, $) => {
// last, add new roles
newRoles = [...newRoles, ...sanitizedAdded]
await $.discord.updateRoles(gm, newRoles)
if (!arrayMatches(currentRoles, newRoles)) {
await $.discord.updateRoles(gm, newRoles)
}
ctx.body = { ok: true }
$.P.invalidate(userId)
$.discord.invalidate(userId)
})
}
const arrayMatches = (a, b) =>
a.length === b.length && a.reduce((_, aValue) => b.contains(aValue), true)

View file

@ -238,6 +238,15 @@ class DiscordService extends Service {
return returnVal
}
invalidate(deadKey) {
const keys = this.cache.keys()
for (let key of keys) {
if (key.includes(deadKey)) {
this.cache.del(key)
}
}
}
}
module.exports = DiscordService

View file

@ -58,7 +58,7 @@ class PresentationService extends Service {
roles: serverRoles,
message: serverData.message,
categories: serverData.categories,
perms: this.discord.getPermissions(member, serverRoles, server),
perms: this.discord.getPermissions(member, serverRoles),
}
})
}
@ -74,6 +74,15 @@ class PresentationService extends Service {
return returnVal
}
invalidate(deadKey) {
const keys = this.cache.keys()
for (let key of keys) {
if (key.includes(deadKey)) {
this.cache.del(key)
}
}
}
}
module.exports = PresentationService