Add local only cache to cache service and cache typeorm entity metadata#16287
Conversation
...ngine/twenty-orm/global-workspace-datasource/workspace-orm-entity-metadatas-cache.service.ts
Show resolved
Hide resolved
| import { WorkspaceCache } from 'src/engine/workspace-cache/decorators/workspace-cache.decorator'; | ||
|
|
||
| @Injectable() | ||
| @WorkspaceCache('entityMetadatas', { localOnly: true }) |
Greptile OverviewGreptile SummaryThis PR introduces local-only caching for TypeORM Key changes:
Critical Issue: Confidence Score: 2/5
Important Files ChangedFile Analysis
Sequence DiagramsequenceDiagram
participant Client
participant CommonBaseQueryRunner
participant GlobalWorkspaceOrmManager
participant WorkspaceCacheService
participant WorkspaceEntityMetadatasCacheService
participant LocalCache
participant Database
Client->>CommonBaseQueryRunner: execute()
CommonBaseQueryRunner->>GlobalWorkspaceOrmManager: executeInWorkspaceContext()
GlobalWorkspaceOrmManager->>WorkspaceCacheService: getOrRecompute(workspaceId, [..., 'entityMetadatas'])
alt entityMetadatas in LocalCache
WorkspaceCacheService->>LocalCache: get(entityMetadatas)
LocalCache-->>WorkspaceCacheService: cached EntityMetadata[]
else entityMetadatas not in LocalCache
WorkspaceCacheService->>WorkspaceEntityMetadatasCacheService: computeForCache(workspaceId)
WorkspaceEntityMetadatasCacheService->>Database: find ObjectMetadata, FieldMetadata
Database-->>WorkspaceEntityMetadatasCacheService: raw entities
WorkspaceEntityMetadatasCacheService->>WorkspaceEntityMetadatasCacheService: buildFlatMaps()
WorkspaceEntityMetadatasCacheService->>WorkspaceEntityMetadatasCacheService: buildEntityMetadatas()
WorkspaceEntityMetadatasCacheService-->>WorkspaceCacheService: EntityMetadata[]
WorkspaceCacheService->>LocalCache: set(entityMetadatas) [localOnly - no Redis]
end
WorkspaceCacheService-->>GlobalWorkspaceOrmManager: cached data
GlobalWorkspaceOrmManager-->>CommonBaseQueryRunner: ORMWorkspaceContext
CommonBaseQueryRunner-->>Client: query results
|
...rc/engine/twenty-orm/global-workspace-datasource/workspace-entity-metadatas-cache.service.ts
Outdated
Show resolved
Hide resolved
|
🚀 Preview Environment Ready! Your preview environment is available at: http://bore.pub:3166 This environment will automatically shut down when the PR is closed or after 5 hours. |
packages/twenty-server/src/engine/workspace-cache/services/workspace-cache.service.ts
Outdated
Show resolved
Hide resolved
…-cache-typeorm-entity-metadata
Problem
buildEntityMetadatas in GlobalWorkspaceOrmManager is computationally expensive and was running on every executeInWorkspaceContext call. This method uses TypeORM's EntitySchemaTransformer and EntityMetadataBuilder to build metadata for all workspace entities (30-50+ objects with many fields each).
The resulting EntityMetadata[] is not serialisable which means it cannot be cached in Redis because they contain:
Solution
Extended the workspace cache system to support local-only caching, then created a cache provider for entityMetadatas.
Implementation details
Updated @WorkspaceCache decorator (workspace-cache.decorator.ts)
Created WorkspaceEntityMetadatasCacheService
Simplified GlobalWorkspaceOrmManager
Updated Workspace migration runner - the only entry point where metadata can change