-
-
Notifications
You must be signed in to change notification settings - Fork 826
chore: cache projects response #10927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { subDays } from 'date-fns'; | ||
| import { subDays, secondsToMilliseconds } from 'date-fns'; | ||
| import joi from 'joi'; | ||
| const { ValidationError } = joi; | ||
| import createSlug from 'slug'; | ||
|
|
@@ -86,7 +86,31 @@ | |
| import { batchExecute } from '../../util/index.js'; | ||
| import metricsHelper from '../../util/metrics-helper.js'; | ||
| import { FUNCTION_TIME } from '../../metric-events.js'; | ||
| import memoizee from 'memoizee'; | ||
| import { | ||
| PROJECT_ACCESS_ADDED, | ||
| PROJECT_ACCESS_GROUP_ROLES_DELETED, | ||
| PROJECT_ACCESS_GROUP_ROLES_UPDATED, | ||
| PROJECT_ACCESS_UPDATED, | ||
| PROJECT_ACCESS_USER_ROLES_DELETED, | ||
| PROJECT_ACCESS_USER_ROLES_UPDATED, | ||
| PROJECT_ARCHIVED, | ||
| PROJECT_CREATED, | ||
| PROJECT_DELETED, | ||
| PROJECT_ENVIRONMENT_ADDED, | ||
| PROJECT_ENVIRONMENT_REMOVED, | ||
| PROJECT_FAVORITED, | ||
| PROJECT_GROUP_ADDED, | ||
| PROJECT_IMPORT, | ||
| PROJECT_REVIVED, | ||
| PROJECT_UNFAVORITED, | ||
| PROJECT_UPDATED, | ||
| PROJECT_USER_ADDED, | ||
| PROJECT_USER_REMOVED, | ||
| PROJECT_USER_ROLE_CHANGED, | ||
| } from '../../events/index.js'; | ||
| import type { ResourceLimitsService } from '../resource-limits/resource-limits-service.js'; | ||
| import type { Logger } from '../../logger.js'; | ||
|
|
||
| type Days = number; | ||
| type Count = number; | ||
|
|
@@ -126,7 +150,7 @@ | |
|
|
||
| private groupService: GroupService; | ||
|
|
||
| private logger: any; | ||
| private logger: Logger; | ||
|
|
||
| private featureToggleService: FeatureToggleService; | ||
|
|
||
|
|
@@ -156,6 +180,40 @@ | |
|
|
||
| private timer: Function; | ||
|
|
||
| private getProjectsForAdminUiCached: (( | ||
| query?: IProjectQuery & IProjectsQuery, | ||
| userId?: number, | ||
| ) => Promise<ProjectForUi[]>) & | ||
| memoizee.Memoized< | ||
| ( | ||
| query?: IProjectQuery & IProjectsQuery, | ||
| userId?: number, | ||
| ) => Promise<ProjectForUi[]> | ||
| >; | ||
|
|
||
| private readonly projectCacheInvalidationEvents = [ | ||
| PROJECT_CREATED, | ||
| PROJECT_UPDATED, | ||
| PROJECT_DELETED, | ||
| PROJECT_ARCHIVED, | ||
| PROJECT_REVIVED, | ||
| PROJECT_IMPORT, | ||
| PROJECT_ENVIRONMENT_ADDED, | ||
| PROJECT_ENVIRONMENT_REMOVED, | ||
| PROJECT_USER_ADDED, | ||
| PROJECT_USER_REMOVED, | ||
| PROJECT_USER_ROLE_CHANGED, | ||
| PROJECT_GROUP_ADDED, | ||
| PROJECT_FAVORITED, | ||
| PROJECT_UNFAVORITED, | ||
| PROJECT_ACCESS_ADDED, | ||
| PROJECT_ACCESS_UPDATED, | ||
| PROJECT_ACCESS_USER_ROLES_UPDATED, | ||
| PROJECT_ACCESS_USER_ROLES_DELETED, | ||
| PROJECT_ACCESS_GROUP_ROLES_UPDATED, | ||
| PROJECT_ACCESS_GROUP_ROLES_DELETED, | ||
| ]; | ||
|
|
||
| constructor( | ||
| { | ||
| projectStore, | ||
|
|
@@ -221,16 +279,39 @@ | |
| className: 'ProjectService', | ||
| functionName, | ||
| }); | ||
| const cacheTtl = secondsToMilliseconds(60); | ||
| this.getProjectsForAdminUiCached = memoizee( | ||
| ( | ||
| projectsQuery?: IProjectQuery & IProjectsQuery, | ||
| projectsUserId?: number, | ||
| ) => | ||
| this.projectReadModel.getProjectsForAdminUi( | ||
| projectsQuery, | ||
| projectsUserId, | ||
| ), | ||
| { | ||
| promise: true, | ||
| maxAge: cacheTtl, | ||
| normalizer: ([projectsQuery, projectsUserId]) => | ||
| this.buildProjectsCacheKey( | ||
| projectsQuery as | ||
| | (IProjectQuery & IProjectsQuery) | ||
| | undefined, | ||
| projectsUserId as number | undefined, | ||
| ), | ||
| }, | ||
| ); | ||
| this.registerProjectCacheInvalidationListeners(); | ||
| } | ||
|
|
||
| async getProjects( | ||
| query?: IProjectQuery & IProjectsQuery, | ||
| userId?: number, | ||
| ): Promise<ProjectForUi[]> { | ||
| const projects = await this.projectReadModel.getProjectsForAdminUi( | ||
| query, | ||
| userId, | ||
| ); | ||
| const useCache = this.flagResolver.isEnabled('project-admin-cache'); | ||
| const projects = useCache | ||
| ? await this.getProjectsForAdminUiCached(query, userId) | ||
| : await this.projectReadModel.getProjectsForAdminUi(query, userId); | ||
|
|
||
| if (userId) { | ||
| const projectAccess = | ||
|
|
@@ -249,6 +330,27 @@ | |
| return projects; | ||
| } | ||
|
|
||
| private buildProjectsCacheKey( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pretty sure this will work but it feels a bit dirty to be using big ass JSON blobs as hash keys, we can't just do this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, we could just concatenate strings... hsould be better, will do!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But I like your approach |
||
| query?: IProjectQuery & IProjectsQuery, | ||
| userId?: number, | ||
| ): string { | ||
| const ids = query?.ids === undefined ? null : [...query.ids].sort(); | ||
| return JSON.stringify({ | ||
| userId: userId ?? null, | ||
| id: query?.id ?? null, | ||
| archived: | ||
| typeof query?.archived === 'undefined' ? null : query.archived, | ||
| ids, | ||
| }); | ||
| } | ||
|
|
||
| private registerProjectCacheInvalidationListeners(): void { | ||
| const invalidate = () => this.getProjectsForAdminUiCached.clear(); | ||
| this.projectCacheInvalidationEvents.forEach((eventName) => { | ||
| this.eventBus.on(eventName, invalidate); | ||
|
Check failure on line 350 in src/lib/features/project/project-service.ts
|
||
| }); | ||
| } | ||
|
|
||
| async addOwnersToProjects( | ||
| projects: ProjectForUi[], | ||
| ): Promise<ProjectForUi[]> { | ||
|
|
@@ -1096,10 +1198,6 @@ | |
| ); | ||
| } | ||
|
|
||
| async getMembers(projectId: string): Promise<number> { | ||
| return this.projectStore.getMembersCountByProject(projectId); | ||
| } | ||
|
|
||
| async getProjectUsers( | ||
| projectId: string, | ||
| ): Promise<Array<Pick<IUser, 'id' | 'email' | 'username'>>> { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to differentiate this from the project store that also has a similar query