Skip to content
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

fix: invatidation cache schema with sub schemas by create schema #4207

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 67 additions & 9 deletions api-gateway/src/api/service/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,9 @@ export class SchemaApi {
const schemas = await guardians.createSchema(newSchema, owner);
SchemaHelper.updatePermission(schemas, owner);

await this.cacheService.invalidate(getCacheKey([req.url], user))
const invalidedCacheKeys = [`${PREFIXES.SCHEMES}schema-with-sub-schemas`];

await this.cacheService.invalidate(getCacheKey([req.url, ...invalidedCacheKeys], user))

return SchemaUtils.toOld(schemas);
} catch (error) {
Expand Down Expand Up @@ -830,7 +832,8 @@ export class SchemaApi {
@HttpCode(HttpStatus.ACCEPTED)
async copySchemaAsync(
@AuthUser() user: IAuthUser,
@Body() body: any
@Body() body: any,
@Req() req
): Promise<TaskDTO> {
const taskManager = new TaskManager();
const guardians = new Guardians();
Expand All @@ -844,6 +847,11 @@ export class SchemaApi {
await this.logger.error(error, ['API_GATEWAY']);
taskManager.addError(task.taskId, { code: 500, message: error.message });
});

const invalidedCacheKeys = [`${PREFIXES.SCHEMES}schema-with-sub-schemas`];

await this.cacheService.invalidate(getCacheKey([req.url, ...invalidedCacheKeys], user))

return task;
}

Expand Down Expand Up @@ -884,7 +892,8 @@ export class SchemaApi {
async createNewSchemaAsync(
@AuthUser() user: IAuthUser,
@Param('topicId') topicId: string,
@Body() newSchema: SchemaDTO
@Body() newSchema: SchemaDTO,
@Req() req
): Promise<TaskDTO> {
const owner = new EntityOwner(user);
const guardians = new Guardians();
Expand All @@ -903,6 +912,11 @@ export class SchemaApi {
await this.logger.error(error, ['API_GATEWAY']);
taskManager.addError(task.taskId, { code: 500, message: error.message });
});

const invalidedCacheKeys = [`${PREFIXES.SCHEMES}schema-with-sub-schemas`];

await this.cacheService.invalidate(getCacheKey([req.url, ...invalidedCacheKeys], user))

return task;
}

Expand Down Expand Up @@ -1034,7 +1048,9 @@ export class SchemaApi {
const schemas = (await guardians.deleteSchema(schemaId, owner, true) as ISchema[]);
SchemaHelper.updatePermission(schemas, owner);

await this.cacheService.invalidate(getCacheKey([req.url], user))
const invalidedCacheKeys = [`${PREFIXES.SCHEMES}schema-with-sub-schemas`];

await this.cacheService.invalidate(getCacheKey([req.url, ...invalidedCacheKeys], user))

return SchemaUtils.toOld(schemas);
} catch (error) {
Expand Down Expand Up @@ -1420,7 +1436,8 @@ export class SchemaApi {
@AuthUser() user: IAuthUser,
@Param('topicId') topicId: string,
@Body() body: MessageSchemaDTO,
@Response() res: any
@Response() res: any,
@Req() req
): Promise<SchemaDTO[]> {
const guardians = new Guardians();
const messageId = body?.messageId;
Expand All @@ -1434,6 +1451,11 @@ export class SchemaApi {
category: SchemaCategory.POLICY
}, owner);
SchemaHelper.updatePermission(items, owner);

const invalidedCacheKeys = [`${PREFIXES.SCHEMES}schema-with-sub-schemas`];

await this.cacheService.invalidate(getCacheKey([req.url, ...invalidedCacheKeys], user))

return res.status(201).header('X-Total-Count', count).send(SchemaUtils.toOld(items));
} catch (error) {
await InternalException(error, this.logger);
Expand Down Expand Up @@ -1485,6 +1507,7 @@ export class SchemaApi {
@AuthUser() user: IAuthUser,
@Param('topicId') topicId: string,
@Body() body: MessageSchemaDTO,
@Req() req
): Promise<TaskDTO> {
const messageId = body?.messageId;
if (!messageId) {
Expand All @@ -1500,6 +1523,11 @@ export class SchemaApi {
await this.logger.error(error, ['API_GATEWAY']);
taskManager.addError(task.taskId, { code: 500, message: error.message });
});

const invalidedCacheKeys = [`${PREFIXES.SCHEMES}schema-with-sub-schemas`];

await this.cacheService.invalidate(getCacheKey([req.url, ...invalidedCacheKeys], user))

return task;
}

Expand Down Expand Up @@ -1542,7 +1570,8 @@ export class SchemaApi {
@AuthUser() user: IAuthUser,
@Param('topicId') topicId: string,
@Body() zip: any,
@Response() res: any
@Response() res: any,
@Req() req
): Promise<SchemaDTO[]> {
const guardians = new Guardians();
if (!zip) {
Expand All @@ -1556,6 +1585,11 @@ export class SchemaApi {
category: SchemaCategory.POLICY
}, owner);
SchemaHelper.updatePermission(items, owner);

const invalidedCacheKeys = [`${PREFIXES.SCHEMES}schema-with-sub-schemas`];

await this.cacheService.invalidate(getCacheKey([req.url, ...invalidedCacheKeys], user))

return res.status(201).header('X-Total-Count', count).send(SchemaUtils.toOld(items));
} catch (error) {
await InternalException(error, this.logger);
Expand Down Expand Up @@ -1599,6 +1633,7 @@ export class SchemaApi {
@AuthUser() user: IAuthUser,
@Param('topicId') topicId: string,
@Body() zip: any,
@Req() req
): Promise<TaskDTO> {
if (!zip) {
throw new HttpException('File in body is empty', HttpStatus.UNPROCESSABLE_ENTITY)
Expand All @@ -1614,6 +1649,11 @@ export class SchemaApi {
await this.logger.error(error, ['API_GATEWAY']);
taskManager.addError(task.taskId, { code: 500, message: error.message });
});

const invalidedCacheKeys = [`${PREFIXES.SCHEMES}schema-with-sub-schemas`];

await this.cacheService.invalidate(getCacheKey([req.url, ...invalidedCacheKeys], user))

return task;
}

Expand Down Expand Up @@ -1788,7 +1828,9 @@ export class SchemaApi {
SchemaHelper.updateOwner(newSchema, owner);
const schema = await guardians.createSystemSchema(newSchema);

await this.cacheService.invalidate(getCacheKey([req.url], req.user))
const invalidedCacheKeys = [`${PREFIXES.SCHEMES}schema-with-sub-schemas`];

await this.cacheService.invalidate(getCacheKey([req.url, ...invalidedCacheKeys], user))

return SchemaUtils.toOld(schema);
} catch (error) {
Expand Down Expand Up @@ -1957,6 +1999,7 @@ export class SchemaApi {
async deleteSystemSchema(
@AuthUser() user: IAuthUser,
@Param('schemaId') schemaId: string,
@Req() req
): Promise<any> {
try {
const guardians = new Guardians();
Expand All @@ -1973,6 +2016,10 @@ export class SchemaApi {
throw new HttpException('Schema is active.', HttpStatus.UNPROCESSABLE_ENTITY);
}
await guardians.deleteSchema(schemaId, owner);

const invalidedCacheKeys = [`${PREFIXES.SCHEMES}schema-with-sub-schemas`];

await this.cacheService.invalidate(getCacheKey([req.url, ...invalidedCacheKeys], user))
} catch (error) {
await InternalException(error, this.logger);
}
Expand Down Expand Up @@ -2246,7 +2293,8 @@ export class SchemaApi {
@AuthUser() user: IAuthUser,
@Param('topicId') topicId: string,
@Body() file: ArrayBuffer,
@Response() res: any
@Response() res: any,
@Req() req
): Promise<any> {
if (!file) {
throw new HttpException('File in body is empty', HttpStatus.UNPROCESSABLE_ENTITY)
Expand All @@ -2259,6 +2307,11 @@ export class SchemaApi {
category: SchemaCategory.POLICY
}, owner);
SchemaHelper.updatePermission(items, owner);

const invalidedCacheKeys = [`${PREFIXES.SCHEMES}schema-with-sub-schemas`];

await this.cacheService.invalidate(getCacheKey([req.url, ...invalidedCacheKeys], user))

return res.status(201).header('X-Total-Count', count).send(SchemaUtils.toOld(items));
} catch (error) {
await InternalException(error, this.logger);
Expand Down Expand Up @@ -2305,7 +2358,8 @@ export class SchemaApi {
@AuthUser() user: IAuthUser,
@Param('topicId') topicId: string,
@Body() file: ArrayBuffer,
@Response() res: any
@Response() res: any,
@Req() req
): Promise<any> {
if (!file) {
throw new HttpException('File in body is empty', HttpStatus.UNPROCESSABLE_ENTITY)
Expand All @@ -2320,6 +2374,10 @@ export class SchemaApi {
await this.logger.error(error, ['API_GATEWAY']);
taskManager.addError(task.taskId, { code: 500, message: 'Unknown error: ' + error.message });
});
const invalidedCacheKeys = [`${PREFIXES.SCHEMES}schema-with-sub-schemas`];

await this.cacheService.invalidate(getCacheKey([req.url, ...invalidedCacheKeys], user))

return res.status(202).send(task);
}

Expand Down