Skip to content

Commit

Permalink
add roleSetCacheService
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinyanakiev committed Feb 12, 2025
1 parent 9495108 commit d8081b2
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 20 deletions.
2 changes: 2 additions & 0 deletions alkemio.yml
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ notifications:
enabled: ${NOTIFICATIONS_ENABLED}:true

collaboration:
memberships:
cache_ttl: ${MEMBERSHIP_TTL}:300
whiteboards:
enabled: ${WHITEBOARDS_ENABLED}:true
# the window in which contributions are accepted to be counted towards a single contribution event;
Expand Down
65 changes: 65 additions & 0 deletions src/domain/access/role-set/role.set.cache.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { CommunityMembershipStatus } from '@common/enums/community.membership.status';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Injectable, Inject, LoggerService } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { AlkemioConfig } from '@src/types';
import { Cache } from 'cache-manager';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';

@Injectable()
export class RoleSetCacheService {
private readonly cache_ttl: number;

constructor(
@Inject(CACHE_MANAGER)
private readonly cacheManager: Cache,
@Inject(WINSTON_MODULE_NEST_PROVIDER)
private readonly logger: LoggerService,
private configService: ConfigService<AlkemioConfig, true>
) {
this.cache_ttl = this.configService.get(
'collaboration.membership.cache_ttl',
{ infer: true }
);
}

public async getMembershipFromCache(
agentId: string,
roleSetId: string
): Promise<CommunityMembershipStatus | undefined> {
return await this.cacheManager.get<CommunityMembershipStatus>(
this.getMembershipStatusCacheKey(agentId, roleSetId)
);
}

public async deleteAgentInfoFromCache(
agentId: string,
roleSetId: string
): Promise<any> {
return await this.cacheManager.del(
this.getMembershipStatusCacheKey(agentId, roleSetId)
);
}

public async setMembershipStatusCache(
agentId: string,
roleSetId: string,
membershipStatus: CommunityMembershipStatus
): Promise<CommunityMembershipStatus> {
const cacheKey = this.getMembershipStatusCacheKey(agentId, roleSetId);
return await this.cacheManager.set<CommunityMembershipStatus>(
cacheKey,
membershipStatus,
{
ttl: this.cache_ttl,
}
);
}

private getMembershipStatusCacheKey(
agentId: string,
roleSetId: string
): string {
return `membershipStatus:${agentId}:${roleSetId}`;
}
}
2 changes: 2 additions & 0 deletions src/domain/access/role-set/role.set.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { OrganizationLookupModule } from '@domain/community/organization-lookup/
import { UserModule } from '@domain/community/user/user.module';
import { RoleSetAgentRolesDataLoader } from './role.set.data.loaders';
import { RoleSetMembershipStatusDataLoader } from './role.set.data.loader.membership.status';
import { RoleSetCacheService } from './role.set.cache.service';

@Module({
imports: [
Expand Down Expand Up @@ -73,6 +74,7 @@ import { RoleSetMembershipStatusDataLoader } from './role.set.data.loader.member
RoleSetServiceLifecycleInvitation,
RoleSetAgentRolesDataLoader,
RoleSetMembershipStatusDataLoader,
RoleSetCacheService,
],
exports: [RoleSetService, RoleSetAuthorizationService, RoleSetLicenseService],
})
Expand Down
47 changes: 27 additions & 20 deletions src/domain/access/role-set/role.set.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import { UserLookupService } from '@domain/community/user-lookup/user.lookup.ser
import { RoleSetType } from '@common/enums/role.set.type';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { Cache } from 'cache-manager';
import { RoleSetCacheService } from './role.set.cache.service';

@Injectable()
export class RoleSetService {
Expand All @@ -87,7 +88,8 @@ export class RoleSetService {
@Inject(WINSTON_MODULE_NEST_PROVIDER)
private readonly logger: LoggerService,
@Inject(CACHE_MANAGER)
private readonly cacheManager: Cache
private readonly cacheManager: Cache,
private readonly roleSetCacheService: RoleSetCacheService
) {}

async createRoleSet(roleSetData: CreateRoleSetInput): Promise<IRoleSet> {
Expand Down Expand Up @@ -395,19 +397,23 @@ export class RoleSetService {
return CommunityMembershipStatus.NOT_MEMBER;
}

const cacheKey = `membershipStatus:${agentInfo.agentID}:${roleSet.id}`;
const cached =
await this.cacheManager.get<CommunityMembershipStatus>(cacheKey);
if (cached !== undefined && cached !== null) {
const cached = await this.roleSetCacheService.getMembershipFromCache(
agentInfo.agentID,
roleSet.id
);
if (cached) {
return cached;
}

const agent = await this.agentService.getAgentOrFail(agentInfo.agentID);
const isMember = await this.isMember(agent, roleSet);
if (isMember) {
await this.cacheManager.set(cacheKey, CommunityMembershipStatus.MEMBER, {
ttl: 300,
});
await this.roleSetCacheService.setMembershipStatusCache(
agent.id,
roleSet.id,
CommunityMembershipStatus.MEMBER
);

return CommunityMembershipStatus.MEMBER;
}

Expand All @@ -416,10 +422,10 @@ export class RoleSetService {
roleSet.id
);
if (openApplication) {
await this.cacheManager.set(
cacheKey,
CommunityMembershipStatus.APPLICATION_PENDING,
{ ttl: 300 }
await this.roleSetCacheService.setMembershipStatusCache(
agent.id,
roleSet.id,
CommunityMembershipStatus.APPLICATION_PENDING
);
return CommunityMembershipStatus.APPLICATION_PENDING;
}
Expand All @@ -432,19 +438,20 @@ export class RoleSetService {
openInvitation &&
(await this.invitationService.canInvitationBeAccepted(openInvitation.id))
) {
await this.cacheManager.set(
cacheKey,
CommunityMembershipStatus.INVITATION_PENDING,
{ ttl: 300 }
await this.roleSetCacheService.setMembershipStatusCache(
agent.id,
roleSet.id,
CommunityMembershipStatus.INVITATION_PENDING
);
return CommunityMembershipStatus.INVITATION_PENDING;
}

await this.cacheManager.set(
cacheKey,
CommunityMembershipStatus.NOT_MEMBER,
{ ttl: 300 }
await this.roleSetCacheService.setMembershipStatusCache(
agent.id,
roleSet.id,
CommunityMembershipStatus.NOT_MEMBER
);

return CommunityMembershipStatus.NOT_MEMBER;
}

Expand Down
3 changes: 3 additions & 0 deletions src/types/alkemio.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ export type AlkemioConfig = {
enabled: boolean;
};
collaboration: {
membership: {
cache_ttl: number;
};
whiteboards: {
enabled: boolean;
contribution_window: number;
Expand Down

0 comments on commit d8081b2

Please sign in to comment.