Skip to content

Commit

Permalink
refactor: reduce how often cache purge occurs in multiple operations
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Nov 28, 2023
1 parent 083aa40 commit cd2fe3e
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 115 deletions.
136 changes: 47 additions & 89 deletions services/api/src/models/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ export const Group = (clients: {
return membership;
};

const addGroup = async (groupInput: Group): Promise<Group> => {
const addGroup = async (groupInput: Group, purge: Boolean=true): Promise<Group> => {
// Don't allow duplicate subgroup names
try {
const existingGroup = await loadGroupByName(groupInput.name);
Expand Down Expand Up @@ -562,20 +562,14 @@ export const Group = (clients: {
}
}

const allGroups = await loadAllGroups();
const keycloakGroups = await transformKeycloakGroups(allGroups);
const data = Buffer.from(JSON.stringify(keycloakGroups)).toString('base64')
try {
// then attempt to save it to redis
await saveRedisKeycloakCache("allgroups", data);
} catch (err) {
logger.warn(`Couldn't save redis keycloak cache: ${err.message}`);
if (purge === true) {
await purgeCache()
}

return group;
};

const updateGroup = async (groupInput: GroupEdit): Promise<Group> => {
const updateGroup = async (groupInput: GroupEdit, purge: Boolean=true): Promise<Group> => {
const oldGroup = await loadGroupById(groupInput.id);

try {
Expand Down Expand Up @@ -609,23 +603,17 @@ export const Group = (clients: {
await updateGroup({
id: roleSubgroup.id,
name: R.replace(oldGroup.name, newGroup.name, roleSubgroup.name)
});
}, false);
}
}

const allGroups = await loadAllGroups();
const keycloakGroups = await transformKeycloakGroups(allGroups);
const data = Buffer.from(JSON.stringify(keycloakGroups)).toString('base64')
try {
// then attempt to save it to redis
await saveRedisKeycloakCache("allgroups", data);
} catch (err) {
logger.warn(`Couldn't save redis keycloak cache: ${err.message}`);
if (purge === true) {
await purgeCache()
}
return newGroup;
};

const deleteGroup = async (id: string): Promise<void> => {
const deleteGroup = async (id: string, purge: Boolean=true): Promise<void> => {
try {
await keycloakAdminClient.groups.del({ id });
} catch (err) {
Expand All @@ -636,21 +624,16 @@ export const Group = (clients: {
}
}

const allGroups = await loadAllGroups();
const keycloakGroups = await transformKeycloakGroups(allGroups);
const data = Buffer.from(JSON.stringify(keycloakGroups)).toString('base64')
try {
// then attempt to save it to redis
await saveRedisKeycloakCache("allgroups", data);
} catch (err) {
logger.warn(`Couldn't save redis keycloak cache: ${err.message}`);
if (purge === true) {
await purgeCache()
}
};

const addUserToGroup = async (
user: User,
groupInput: Group,
roleName: string
roleName: string,
purge: Boolean=true
): Promise<Group> => {
const group = await loadGroupById(groupInput.id);
// Load or create the role subgroup.
Expand All @@ -666,7 +649,7 @@ export const Group = (clients: {
attributes: {
type: ['role-subgroup']
}
});
}, false);
const role = await keycloakAdminClient.roles.findOneByName({
name: roleName
});
Expand All @@ -686,21 +669,16 @@ export const Group = (clients: {
throw new Error(`Could not add user to group: ${err.message}`);
}

const allGroups = await loadAllGroups();
const keycloakGroups = await transformKeycloakGroups(allGroups);
const data = Buffer.from(JSON.stringify(keycloakGroups)).toString('base64')
try {
// then attempt to save it to redis
await saveRedisKeycloakCache("allgroups", data);
} catch (err) {
logger.warn(`Couldn't save redis keycloak cache: ${err.message}`);
if (purge === true) {
await purgeCache()
}
return await loadGroupById(group.id);
};

const removeUserFromGroup = async (
user: User,
group: Group
group: Group,
purge: Boolean=true
): Promise<Group> => {
const members = await getGroupMembership(group);
const userMembership = R.find(R.pathEq(['user', 'id'], user.id))(members);
Expand All @@ -720,22 +698,17 @@ export const Group = (clients: {

}

const allGroups = await loadAllGroups();
const keycloakGroups = await transformKeycloakGroups(allGroups);
const data = Buffer.from(JSON.stringify(keycloakGroups)).toString('base64')
try {
// then attempt to save it to redis
await saveRedisKeycloakCache("allgroups", data);
} catch (err) {
logger.warn(`Couldn't save redis keycloak cache: ${err.message}`);
if (purge === true) {
await purgeCache()
}

return await loadGroupById(group.id);
};

const addProjectToGroup = async (
projectId: number,
groupInput: any
groupInput: any,
purge: Boolean=true
): Promise<void> => {
const group = await loadGroupById(groupInput.id);
let newGroupProjects = ""
Expand Down Expand Up @@ -769,20 +742,15 @@ export const Group = (clients: {
);
}

const allGroups = await loadAllGroups();
const keycloakGroups = await transformKeycloakGroups(allGroups);
const data = Buffer.from(JSON.stringify(keycloakGroups)).toString('base64')
try {
// then attempt to save it to redis
await saveRedisKeycloakCache("allgroups", data);
} catch (err) {
logger.warn(`Couldn't save redis keycloak cache: ${err.message}`);
if (purge === true) {
await purgeCache()
}
};

const removeProjectFromGroup = async (
projectId: number,
group: Group
group: Group,
purge: Boolean=true
): Promise<void> => {
const groupProjectIds = getProjectIdsFromGroup(group)
const newGroupProjects = R.pipe(
Expand Down Expand Up @@ -812,21 +780,16 @@ export const Group = (clients: {
);
}

const allGroups = await loadAllGroups();
const keycloakGroups = await transformKeycloakGroups(allGroups);
const data = Buffer.from(JSON.stringify(keycloakGroups)).toString('base64')
try {
// then attempt to save it to redis
await saveRedisKeycloakCache("allgroups", data);
} catch (err) {
logger.warn(`Couldn't save redis keycloak cache: ${err.message}`);
if (purge === true) {
await purgeCache()
}
};

// helper to remove project from groups
const removeProjectFromGroups = async (
projectId: number,
groups: Group[]
groups: Group[],
purge: Boolean=true
): Promise<void> => {
for (const g in groups) {
const group = groups[g]
Expand Down Expand Up @@ -859,22 +822,16 @@ export const Group = (clients: {
}
}

// once the project is remove from the groups, update the cache
const allGroups = await loadAllGroups();
const keycloakGroups = await transformKeycloakGroups(allGroups);
const data = Buffer.from(JSON.stringify(keycloakGroups)).toString('base64')
try {
// then attempt to save it to redis
await saveRedisKeycloakCache("allgroups", data);
} catch (err) {
logger.warn(`Couldn't save redis keycloak cache: ${err.message}`);
if (purge === true) {
await purgeCache()
}
};

// helper to remove user from groups
const removeUserFromGroups = async (
user: User,
groups: Group[]
groups: Group[],
purge: Boolean=true
): Promise<void> => {
for (const g in groups) {
const group = groups[g]
Expand All @@ -895,21 +852,16 @@ export const Group = (clients: {
}
}

const allGroups = await loadAllGroups();
const keycloakGroups = await transformKeycloakGroups(allGroups);
const data = Buffer.from(JSON.stringify(keycloakGroups)).toString('base64')
try {
// then attempt to save it to redis
await saveRedisKeycloakCache("allgroups", data);
} catch (err) {
logger.warn(`Couldn't save redis keycloak cache: ${err.message}`);
if (purge === true) {
await purgeCache()
}
};

// helper to remove all non default-users from project
const removeNonProjectDefaultUsersFromGroup = async (
group: Group,
project: String,
purge: Boolean=true
): Promise<Group> => {
const members = await getGroupMembership(group);

Expand All @@ -928,7 +880,14 @@ export const Group = (clients: {
}
}

// once the users are removed from the group, update the cache
if (purge === true) {
await purgeCache()
}

return await loadGroupById(group.id);
};

const purgeCache = async () => {
const allGroups = await loadAllGroups();
const keycloakGroups = await transformKeycloakGroups(allGroups);
const data = Buffer.from(JSON.stringify(keycloakGroups)).toString('base64')
Expand All @@ -938,9 +897,7 @@ export const Group = (clients: {
} catch (err) {
logger.warn(`Couldn't save redis keycloak cache: ${err.message}`);
}

return await loadGroupById(group.id);
};
}

return {
loadAllGroups,
Expand All @@ -967,6 +924,7 @@ export const Group = (clients: {
transformKeycloakGroups,
getGroupMembership,
getGroupMemberCount,
removeNonProjectDefaultUsersFromGroup
removeNonProjectDefaultUsersFromGroup,
purgeCache
};
};
Loading

0 comments on commit cd2fe3e

Please sign in to comment.