Skip to content

Commit

Permalink
feat: add cache and invalidation for ipfs api
Browse files Browse the repository at this point in the history
  • Loading branch information
Ihar committed Jul 26, 2024
1 parent 67dcea4 commit 1f02777
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
29 changes: 22 additions & 7 deletions api-gateway/src/api/service/ipfs.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { Body, Controller, Get, HttpCode, HttpException, HttpStatus, Param, Post, StreamableFile } from '@nestjs/common';
import { Body, Controller, Get, HttpCode, HttpException, HttpStatus, Param, Post, Req, StreamableFile } from '@nestjs/common';
import { ApiBody, ApiExtraModels, ApiInternalServerErrorResponse, ApiOkResponse, ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger';
import { Permissions } from '@guardian/interfaces';
import { Auth } from '#auth';
import { Examples, InternalServerErrorDTO } from '#middlewares';
import { Guardians, InternalException } from '#helpers';
import { CacheService, getCacheKey, Guardians, InternalException, UseCache } from '#helpers';
import { PinoLogger } from '@guardian/common';
import { CACHE, PREFIXES } from '#constants';

@Controller('ipfs')
@ApiTags('ipfs')
export class IpfsApi {
constructor(private readonly logger: PinoLogger) {
constructor(private readonly cacheService: CacheService, private readonly logger: PinoLogger) {
}

/**
Expand Down Expand Up @@ -41,7 +42,8 @@ export class IpfsApi {
@ApiExtraModels(InternalServerErrorDTO)
@HttpCode(HttpStatus.CREATED)
async postFile(
@Body() body: any
@Body() body: any,
@Req() req
): Promise<string> {
try {
if (!Object.values(body).length) {
Expand All @@ -54,6 +56,12 @@ export class IpfsApi {
throw new HttpException('File is not uploaded', HttpStatus.BAD_REQUEST);
}

const invalidedCacheTags = [
`${PREFIXES.IPFS}file/${cid}`,
`${PREFIXES.IPFS}file/${cid}/dry-run`,
];
await this.cacheService.invalidate(getCacheKey([req.url, ...invalidedCacheTags], req.user));

return JSON.stringify(cid);
} catch (error) {
await InternalException(error, this.logger);
Expand Down Expand Up @@ -97,7 +105,8 @@ export class IpfsApi {
@HttpCode(HttpStatus.CREATED)
async postFileDryRun(
@Param('policyId') policyId: string,
@Body() body: any
@Body() body: any,
@Req() req
): Promise<string> {
try {
if (!Object.values(body).length) {
Expand All @@ -107,6 +116,12 @@ export class IpfsApi {
const guardians = new Guardians();
const { cid } = await guardians.addFileToDryRunStorage(body, policyId);

const invalidedCacheTags = [
`${PREFIXES.IPFS}file/${cid}`,
`${PREFIXES.IPFS}file/${cid}/dry-run`,
];
await this.cacheService.invalidate(getCacheKey([req.url, ...invalidedCacheTags], req.user));

return JSON.stringify(cid);
} catch (error) {
await InternalException(error, this.logger);
Expand Down Expand Up @@ -145,7 +160,7 @@ export class IpfsApi {
type: InternalServerErrorDTO
})
@ApiExtraModels(InternalServerErrorDTO)
// @UseCache({ ttl: CACHE.LONG_TTL })
@UseCache({ ttl: CACHE.LONG_TTL })
@HttpCode(HttpStatus.OK)
async getFile(
@Param('cid') cid: string
Expand Down Expand Up @@ -194,7 +209,7 @@ export class IpfsApi {
type: InternalServerErrorDTO
})
@ApiExtraModels(InternalServerErrorDTO)
// @UseCache({ ttl: CACHE.LONG_TTL })
@UseCache({ ttl: CACHE.LONG_TTL })
@HttpCode(HttpStatus.OK)
async getFileDryRun(
@Param('cid') cid: string
Expand Down
1 change: 1 addition & 0 deletions api-gateway/src/constants/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export const PREFIXES = {
POLICIES: '/policies/',
CONTRACTS: '/contracts/',
TAGS: '/tags/',
IPFS: 'ipfs',
};

0 comments on commit 1f02777

Please sign in to comment.