chore: cache effective snippets results in acl role (#3102)

This commit is contained in:
ChengLei Shao 2023-11-27 15:49:50 +08:00 committed by GitHub
parent 5ebd5d5c62
commit cbcf70e587
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,12 +22,20 @@ export class ACLRole {
strategy: string | AvailableStrategyOptions;
resources = new Map<string, ACLResource>();
snippets: Set<string> = new Set();
_snippetCache = {
params: null,
result: null,
};
constructor(
public acl: ACL,
public name: string,
) {}
_serializeSet(set: Set<string>) {
return JSON.stringify([...set].sort());
}
getResource(name: string): ACLResource | undefined {
return this.resources.get(name);
}
@ -86,6 +94,11 @@ export class ACLRole {
}
public effectiveSnippets(): { allowed: Array<string>; rejected: Array<string> } {
const currentParams = this._serializeSet(this.snippets);
if (this._snippetCache.params === currentParams) {
return this._snippetCache.result;
}
const allowedSnippets = new Set<string>();
const rejectedSnippets = new Set<string>();
@ -108,10 +121,16 @@ export class ACLRole {
// get difference of allowed and rejected snippets
const effectiveSnippets = new Set([...allowedSnippets].filter((x) => !rejectedSnippets.has(x)));
return {
this._snippetCache = {
params: currentParams,
result: {
allowed: [...effectiveSnippets],
rejected: [...rejectedSnippets],
},
};
return this._snippetCache.result;
}
public snippetAllowed(actionPath: string) {