fix: invalidate cache when no record inside workspace cache version (#3994)

* fix: invalidate cache when no record inside workspace cache version

* fix: use getVersion
This commit is contained in:
Jérémy M
2024-02-16 10:37:25 +01:00
committed by GitHub
parent 34d02cf4ed
commit f47159d84d
2 changed files with 15 additions and 9 deletions

View File

@ -12,11 +12,11 @@ export class WorkspaceCacheVersionService {
private readonly workspaceCacheVersionRepository: Repository<WorkspaceCacheVersionEntity>, private readonly workspaceCacheVersionRepository: Repository<WorkspaceCacheVersionEntity>,
) {} ) {}
async incrementVersion(workspaceId: string): Promise<void> { async incrementVersion(workspaceId: string): Promise<string> {
const workspaceCacheVersion = const workspaceCacheVersion = (await this.getVersion(workspaceId)) ?? {
(await this.workspaceCacheVersionRepository.findOne({ version: '0',
where: { workspaceId }, };
})) ?? { version: '0' }; const newVersion = `${+workspaceCacheVersion.version + 1}`;
await this.workspaceCacheVersionRepository.upsert( await this.workspaceCacheVersionRepository.upsert(
{ {
@ -25,14 +25,16 @@ export class WorkspaceCacheVersionService {
}, },
['workspaceId'], ['workspaceId'],
); );
return newVersion;
} }
async getVersion(workspaceId: string): Promise<string> { async getVersion(workspaceId: string): Promise<string | null> {
const workspaceCacheVersion = const workspaceCacheVersion =
await this.workspaceCacheVersionRepository.findOne({ await this.workspaceCacheVersionRepository.findOne({
where: { workspaceId }, where: { workspaceId },
}); });
return workspaceCacheVersion?.version ?? '0'; return workspaceCacheVersion?.version ?? null;
} }
} }

View File

@ -28,13 +28,17 @@ export class WorkspaceSchemaStorageService {
(await this.cacheVersionMemoryStorageService.read({ (await this.cacheVersionMemoryStorageService.read({
key: workspaceId, key: workspaceId,
})) ?? '0'; })) ?? '0';
const latestVersion = let latestVersion =
await this.workspaceCacheVersionService.getVersion(workspaceId); await this.workspaceCacheVersionService.getVersion(workspaceId);
if (currentVersion !== latestVersion) { if (!latestVersion || currentVersion !== latestVersion) {
// Invalidate cache if version mismatch is detected // Invalidate cache if version mismatch is detected
await this.invalidateCache(workspaceId); await this.invalidateCache(workspaceId);
// If the latest version is not found, increment the version
latestVersion ??=
await this.workspaceCacheVersionService.incrementVersion(workspaceId);
// Update the cache version after invalidation // Update the cache version after invalidation
await this.cacheVersionMemoryStorageService.write({ await this.cacheVersionMemoryStorageService.write({
key: workspaceId, key: workspaceId,