Skip to content

Commit

Permalink
fix: invalidate cache when no record inside workspace cache version (#…
Browse files Browse the repository at this point in the history
…3994)

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

* fix: use getVersion
  • Loading branch information
magrinj authored Feb 16, 2024
1 parent 34d02cf commit f47159d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ export class WorkspaceCacheVersionService {
private readonly workspaceCacheVersionRepository: Repository<WorkspaceCacheVersionEntity>,
) {}

async incrementVersion(workspaceId: string): Promise<void> {
const workspaceCacheVersion =
(await this.workspaceCacheVersionRepository.findOne({
where: { workspaceId },
})) ?? { version: '0' };
async incrementVersion(workspaceId: string): Promise<string> {
const workspaceCacheVersion = (await this.getVersion(workspaceId)) ?? {
version: '0',
};
const newVersion = `${+workspaceCacheVersion.version + 1}`;

await this.workspaceCacheVersionRepository.upsert(
{
Expand All @@ -25,14 +25,16 @@ export class WorkspaceCacheVersionService {
},
['workspaceId'],
);

return newVersion;
}

async getVersion(workspaceId: string): Promise<string> {
async getVersion(workspaceId: string): Promise<string | null> {
const workspaceCacheVersion =
await this.workspaceCacheVersionRepository.findOne({
where: { workspaceId },
});

return workspaceCacheVersion?.version ?? '0';
return workspaceCacheVersion?.version ?? null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ export class WorkspaceSchemaStorageService {
(await this.cacheVersionMemoryStorageService.read({
key: workspaceId,
})) ?? '0';
const latestVersion =
let latestVersion =
await this.workspaceCacheVersionService.getVersion(workspaceId);

if (currentVersion !== latestVersion) {
if (!latestVersion || currentVersion !== latestVersion) {
// Invalidate cache if version mismatch is detected
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
await this.cacheVersionMemoryStorageService.write({
key: workspaceId,
Expand Down

0 comments on commit f47159d

Please sign in to comment.