Skip to content

Commit

Permalink
fix(profilerepository): fix repo implementation for redis cache
Browse files Browse the repository at this point in the history
  • Loading branch information
fmauNeko committed May 28, 2024
1 parent e3b5099 commit 571dccf
Showing 1 changed file with 25 additions and 55 deletions.
80 changes: 25 additions & 55 deletions lib/core/security/profileRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -59,19 +58,14 @@ type UpdateOptions = {
*/
export class ProfileRepository extends ObjectRepository<Profile> {
private module: any;
private profiles: Map<string, Profile>;

/**
* @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;
Expand Down Expand Up @@ -129,7 +123,7 @@ export class ProfileRepository extends ObjectRepository<Profile> {
* @param {String} [id] - profile identifier
*/
global.kuzzle.onAsk("core:security:profile:invalidate", (id) =>
this.invalidate(id),
this.deleteFromCache(id),
);

/**
Expand Down Expand Up @@ -191,15 +185,26 @@ export class ProfileRepository extends ObjectRepository<Profile> {
* @returns {Promise.<Promise>}
* @throws {NotFoundError} If the corresponding profile doesn't exist
*/
async load(id: string): Promise<Profile> {
if (this.profiles.has(id)) {
return this.profiles.get(id);
}
override async load(
id: string,
options: { key?: string } = {},
): Promise<Profile> {
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;
}
Expand Down Expand Up @@ -232,16 +237,7 @@ export class ProfileRepository extends ObjectRepository<Profile> {
}

for (const id of profileIds) {
let profile: Profile | Promise<Profile> = 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<Profile> = this.load(id);
profiles.push(profile);
}

Expand Down Expand Up @@ -443,7 +439,7 @@ export class ProfileRepository extends ObjectRepository<Profile> {

await this.deleteFromDatabase(profile._id, { refresh });

this.profiles.delete(profile._id);
await this.deleteFromCache(profile._id);
}

/**
Expand Down Expand Up @@ -507,7 +503,7 @@ export class ProfileRepository extends ObjectRepository<Profile> {
updatedProfile.policies,
);

this.profiles.set(profile._id, updatedProfile);
await this.persistToCache(updatedProfile);
return updatedProfile;
}

Expand Down Expand Up @@ -538,32 +534,6 @@ export class ProfileRepository extends ObjectRepository<Profile> {
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.
Expand Down Expand Up @@ -636,11 +606,11 @@ export class ProfileRepository extends ObjectRepository<Profile> {
// Otherwise we cannot stub them
// ============================================

async toDTO(dto: Profile): Promise<JSONObject> {
toDTO(dto: Profile): JSONObject {
return super.toDTO(dto);
}

async deleteFromDatabase(id: string, options: JSONObject) {
deleteFromDatabase(id: string, options: JSONObject) {
return super.deleteFromDatabase(id, options);
}

Expand Down

0 comments on commit 571dccf

Please sign in to comment.