Skip to content

Commit 5e21b8d

Browse files
committed
fix(backend:users): ensure whitelist cache entries with parameters are properly cleared
1 parent d13132a commit 5e21b8d

1 file changed

Lines changed: 22 additions & 23 deletions

File tree

backend/src/applications/users/services/users-queries.service.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -555,29 +555,28 @@ export class UsersQueries {
555555
}
556556

557557
clearWhiteListCaches(userIds: number[] | '*') {
558-
if (userIds === '*') {
559-
// Means all entries
560-
for (const pattern of [
561-
this.cache.genSlugKey(this.constructor.name, this.usersWhitelist.name, userIds),
562-
this.cache.genSlugKey(this.constructor.name, this.groupsWhitelist.name, userIds)
563-
]) {
564-
this.cache
565-
.keys(pattern)
566-
.then((keys: string[]) => {
567-
if (!keys.length) return
568-
this.logger.verbose({ tag: this.clearWhiteListCaches.name, msg: `${JSON.stringify(keys)}` })
569-
this.cache.mdel(keys).catch((e: Error) => this.logger.error({ tag: this.clearWhiteListCaches.name, msg: `${e}` }))
570-
})
571-
.catch((e: Error) => this.logger.error({ tag: this.clearWhiteListCaches.name, msg: `${e}` }))
572-
}
573-
} else {
574-
this.cache
575-
.mdel([
576-
...userIds.map((id: number) => this.cache.genSlugKey(this.constructor.name, this.usersWhitelist.name, id)),
577-
...userIds.map((id: number) => this.cache.genSlugKey(this.constructor.name, this.groupsWhitelist.name, id))
578-
])
579-
.catch((e: Error) => this.logger.error({ tag: this.clearWhiteListCaches.name, msg: `${e}` }))
580-
}
558+
// '*' -> Means all entries
559+
const whitelists = [this.usersWhitelist.name, this.groupsWhitelist.name]
560+
const keysPromise =
561+
userIds === '*'
562+
? Promise.all(whitelists.map((whitelist) => this.cache.keys(this.cache.genSlugKey(this.constructor.name, whitelist, userIds)))).then(
563+
(keyLists: string[][]) => keyLists.flat()
564+
)
565+
: Promise.resolve(
566+
whitelists.flatMap((whitelist) =>
567+
userIds.flatMap((id) => [
568+
this.cache.genSlugKey(this.constructor.name, whitelist, id),
569+
this.cache.genSlugKey(this.constructor.name, whitelist, id, '*')
570+
])
571+
)
572+
)
573+
keysPromise
574+
.then((keys: string[]) => {
575+
if (!keys.length) return
576+
this.logger.verbose({ tag: this.clearWhiteListCaches.name, msg: JSON.stringify(keys) })
577+
return this.cache.mdel(keys)
578+
})
579+
.catch((e: Error) => this.logger.error({ tag: this.clearWhiteListCaches.name, msg: `${e}` }))
581580
}
582581

583582
async allUserIdsFromGroupsAndSubGroups(groupIds: number[]): Promise<number[]> {

0 commit comments

Comments
 (0)