From 571dccf1452abbc954e528868e72cba03bdb55cd Mon Sep 17 00:00:00 2001 From: Florian Maunier Date: Tue, 28 May 2024 17:40:59 +0200 Subject: [PATCH] fix(profilerepository): fix repo implementation for redis cache --- lib/core/security/profileRepository.ts | 80 ++++++++------------------ 1 file changed, 25 insertions(+), 55 deletions(-) diff --git a/lib/core/security/profileRepository.ts b/lib/core/security/profileRepository.ts index 9c1e5b0c75..65da80a28b 100644 --- a/lib/core/security/profileRepository.ts +++ b/lib/core/security/profileRepository.ts @@ -26,7 +26,6 @@ import { JSONObject } from "kuzzle-sdk"; import { OptimizedPolicy, Policy } from "../../../index"; import * as kerror from "../../kerror"; import { Profile } from "../../model/security/profile"; -import { cacheDbEnum } from "../cache/cacheDbEnum"; import { ObjectRepository } from "../shared/ObjectRepository"; /** @internal */ @@ -59,19 +58,14 @@ type UpdateOptions = { */ export class ProfileRepository extends ObjectRepository { private module: any; - private profiles: Map; /** * @constructor */ constructor(securityModule) { - super({ - cache: cacheDbEnum.INTERNAL, - store: global.kuzzle.internalIndex, - }); + super({ store: global.kuzzle.internalIndex }); this.module = securityModule; - this.profiles = new Map(); this.collection = "profiles"; this.ObjectConstructor = Profile; @@ -129,7 +123,7 @@ export class ProfileRepository extends ObjectRepository { * @param {String} [id] - profile identifier */ global.kuzzle.onAsk("core:security:profile:invalidate", (id) => - this.invalidate(id), + this.deleteFromCache(id), ); /** @@ -191,15 +185,26 @@ export class ProfileRepository extends ObjectRepository { * @returns {Promise.} * @throws {NotFoundError} If the corresponding profile doesn't exist */ - async load(id: string): Promise { - if (this.profiles.has(id)) { - return this.profiles.get(id); - } + override async load( + id: string, + options: { key?: string } = {}, + ): Promise { + const profile = await this.loadFromCache(id, options); + + if (profile === null) { + const profileFromDatabase = await this.loadOneFromDatabase(id); + + if (profileFromDatabase !== null) { + profileFromDatabase.optimizedPolicies = this.optimizePolicies( + profileFromDatabase.policies, + ); + await this.persistToCache(profileFromDatabase); + } - const profile = await super.load(id); + return profileFromDatabase; + } - profile.optimizedPolicies = this.optimizePolicies(profile.policies); - this.profiles.set(id, profile); + await this.refreshCacheTTL(profile); return profile; } @@ -232,16 +237,7 @@ export class ProfileRepository extends ObjectRepository { } for (const id of profileIds) { - let profile: Profile | Promise = this.profiles.get(id); - - if (!profile) { - profile = this.loadOneFromDatabase(id).then((p) => { - p.optimizedPolicies = this.optimizePolicies(p.policies); - this.profiles.set(id, p); - return p; - }); - } - + const profile: Profile | Promise = this.load(id); profiles.push(profile); } @@ -443,7 +439,7 @@ export class ProfileRepository extends ObjectRepository { await this.deleteFromDatabase(profile._id, { refresh }); - this.profiles.delete(profile._id); + await this.deleteFromCache(profile._id); } /** @@ -507,7 +503,7 @@ export class ProfileRepository extends ObjectRepository { updatedProfile.policies, ); - this.profiles.set(profile._id, updatedProfile); + await this.persistToCache(updatedProfile); return updatedProfile; } @@ -538,32 +534,6 @@ export class ProfileRepository extends ObjectRepository { return profile; } - /** - * @override - */ - async truncate(opts: JSONObject) { - try { - await super.truncate(opts); - } finally { - // always clear the RAM cache: even if truncate fails in the middle of it, - // some of the cached profiles might not be valid anymore - this.invalidate(); - } - } - - /** - * Invalidate the cache entries for the given profile. If none is provided, - * the entire cache is emptied. - * @param {string} [profileId] - */ - invalidate(profileId?: string) { - if (!profileId) { - this.profiles.clear(); - } else { - this.profiles.delete(profileId); - } - } - /** * Optimize each policy to get a O(1) index access time * and a O(log(n)) collection search time. @@ -636,11 +606,11 @@ export class ProfileRepository extends ObjectRepository { // Otherwise we cannot stub them // ============================================ - async toDTO(dto: Profile): Promise { + toDTO(dto: Profile): JSONObject { return super.toDTO(dto); } - async deleteFromDatabase(id: string, options: JSONObject) { + deleteFromDatabase(id: string, options: JSONObject) { return super.deleteFromDatabase(id, options); }