Skip to content

Commit aec87dc

Browse files
committed
feat: integrate performance caching for base node list
1 parent d39f3a7 commit aec87dc

File tree

5 files changed

+35
-12
lines changed

5 files changed

+35
-12
lines changed

apps/nestjs-backend/src/features/base-node/base-node.service.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ import type {
5656
WorkflowDeleteEvent,
5757
WorkflowUpdateEvent,
5858
} from '../../event-emitter/events/workflow/workflow.event';
59+
import { generateBaseNodeListCacheKey } from '../../performance-cache/generate-keys';
60+
import { PerformanceCacheService } from '../../performance-cache/service';
61+
import type { IPerformanceCacheStore } from '../../performance-cache/types';
5962
import { ShareDbService } from '../../share-db/share-db.service';
6063
import type { IClsStore } from '../../types/cls';
6164
import { updateOrder } from '../../utils/update-order';
@@ -104,6 +107,7 @@ const maxFolderDepth = 2;
104107
export class BaseNodeService {
105108
private readonly logger = new Logger(BaseNodeService.name);
106109
constructor(
110+
private readonly performanceCacheService: PerformanceCacheService<IPerformanceCacheStore>,
107111
private readonly prismaService: PrismaService,
108112
@InjectModel('CUSTOM_KNEX') private readonly knex: Knex,
109113
private readonly cls: ClsService<IClsStore>,
@@ -168,6 +172,7 @@ export class BaseNodeService {
168172
| IBaseNodePresenceUpdatePayload
169173
| IBaseNodePresenceDeletePayload,
170174
>(baseId: string, handler: (presence: LocalPresence<T>) => void) {
175+
this.performanceCacheService.del(generateBaseNodeListCacheKey(baseId));
171176
const channel = getBaseNodeChannel(baseId);
172177
const presence = this.shareDbService.connect().getPresence(channel);
173178
const localPresence = presence.create(channel);
@@ -234,7 +239,7 @@ export class BaseNodeService {
234239
];
235240
}
236241

237-
async prepareTree(baseId: string): Promise<IBaseNodeVo[]> {
242+
async prepareNodeList(baseId: string): Promise<IBaseNodeVo[]> {
238243
const resourceTypes = this.getResourceTypes();
239244
const resourceResults = await Promise.all(
240245
resourceTypes.map((type) => this.getNodeResource(baseId, type))
@@ -336,12 +341,23 @@ export class BaseNodeService {
336341
);
337342
}
338343

344+
async getNodeListWithCache(baseId: string): Promise<IBaseNodeVo[]> {
345+
return this.performanceCacheService.wrap(
346+
generateBaseNodeListCacheKey(baseId),
347+
() => this.prepareNodeList(baseId),
348+
{
349+
ttl: 60 * 60, // 1 hour
350+
statsType: 'base-node-list',
351+
}
352+
);
353+
}
354+
339355
async getList(baseId: string): Promise<IBaseNodeVo[]> {
340-
return this.prepareTree(baseId);
356+
return this.getNodeListWithCache(baseId);
341357
}
342358

343359
async getTree(baseId: string): Promise<IBaseNodeTreeVo> {
344-
const nodes = await this.prepareTree(baseId);
360+
const nodes = await this.getNodeListWithCache(baseId);
345361

346362
return {
347363
nodes,
@@ -805,12 +821,11 @@ export class BaseNodeService {
805821
return;
806822
}
807823

808-
await this.prismaService.$tx(async (prisma) => {
824+
const createNode = async (prisma: PrismaService) => {
809825
const findNode = await prisma.baseNode.findFirst({
810826
where: { baseId, resourceType, resourceId },
811827
});
812828
if (findNode) {
813-
this.logger.warn('Base node already exists', event);
814829
return;
815830
}
816831
const maxOrder = await this.getMaxOrder(baseId);
@@ -825,7 +840,8 @@ export class BaseNodeService {
825840
createdBy: userId || ANONYMOUS_USER_ID,
826841
},
827842
});
828-
});
843+
};
844+
await createNode(this.prismaService);
829845

830846
this.presenceHandler(baseId, (presence) => {
831847
presence.submit({
@@ -970,12 +986,11 @@ export class BaseNodeService {
970986
return;
971987
}
972988

973-
await this.prismaService.$tx(async (prisma) => {
989+
const deleteNode = async (prisma: PrismaService) => {
974990
const toDeleteNode = await prisma.baseNode.findFirst({
975991
where: { baseId, resourceType, resourceId },
976992
});
977993
if (!toDeleteNode) {
978-
this.logger.warn('Base node has already been deleted', event);
979994
return;
980995
}
981996
const maxOrder = await this.getMaxOrder(baseId);
@@ -997,7 +1012,8 @@ export class BaseNodeService {
9971012
}))
9981013
);
9991014
}
1000-
});
1015+
};
1016+
await deleteNode(this.prismaService);
10011017

10021018
this.presenceHandler(baseId, (presence) => {
10031019
presence.submit({

apps/nestjs-backend/src/performance-cache/generate-keys.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,7 @@ export function generateSettingCacheKey() {
4141
export function generateIntegrationCacheKey(spaceId: string) {
4242
return `integration:${spaceId}` as const;
4343
}
44+
45+
export function generateBaseNodeListCacheKey(baseId: string) {
46+
return `base-node-list:${baseId}` as const;
47+
}

apps/nestjs-backend/src/performance-cache/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ export interface IPerformanceCacheStore {
2929

3030
// instance setting cache, format: instance:setting
3131
'instance:setting': unknown;
32+
33+
// base node list cache, format: base-node-list:base_id
34+
[key: `base-node-list:${string}`]: unknown;
3235
}
3336

3437
/**

apps/nextjs-app/src/features/app/blocks/base/base-node/hooks/use-base-node.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,14 @@ export const useBaseNode = () => {
7676

7777
const queryClient = useQueryClient();
7878
const { data: queryData } = useQuery({
79-
queryKey: ReactQueryKeys.baseNode(baseId),
79+
queryKey: ReactQueryKeys.baseNodeTree(baseId),
8080
queryFn: ({ queryKey }) => getBaseNodeTree(queryKey[1]).then((res) => res.data),
8181
enabled: Boolean(baseId),
8282
});
8383

8484
const invalidateMenu = useCallback(() => {
8585
if (baseId) {
86-
queryClient.invalidateQueries({ queryKey: ReactQueryKeys.baseNode(baseId) });
86+
queryClient.invalidateQueries({ queryKey: ReactQueryKeys.baseNodeTree(baseId) });
8787
}
8888
}, [baseId, queryClient]);
8989

packages/sdk/src/config/react-query-keys.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,5 +223,5 @@ export const ReactQueryKeys = {
223223

224224
oauthApp: (clientId: string) => ['oauth-app', clientId] as const,
225225

226-
baseNode: (baseId: string) => ['base-node', baseId] as const,
226+
baseNodeTree: (baseId: string) => ['base-node-tree', baseId] as const,
227227
};

0 commit comments

Comments
 (0)