Skip to content

Commit

Permalink
feat: adds caching to costly endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
kenodressel committed Jul 26, 2024
1 parent cd2d80f commit 6d21829
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 3 deletions.
57 changes: 55 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
},
"dependencies": {
"@aeternity/aepp-sdk": "^13.3.2",
"@nestjs/cache-manager": "^2.2.2",
"@nestjs/common": "^10.3.3",
"@nestjs/core": "^10.3.3",
"@nestjs/platform-express": "^10.3.3",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CacheModule } from '@nestjs/cache-manager';
import { INestApplication } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { Pair, PairLiquidityInfoHistory } from '@prisma/client';
Expand Down Expand Up @@ -29,6 +30,11 @@ describe('PairLiquidityInfoHistoryController', () => {
useValue: mockPairLiquidityInfoHistoryService,
},
],
imports: [
CacheModule.register({
isGlobal: true,
}),
],
}).compile();

app = module.createNestApplication();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { CacheInterceptor, CacheTTL } from '@nestjs/cache-manager';
import {
Controller,
Get,
ParseEnumPipe,
ParseIntPipe,
Query,
UseInterceptors,
} from '@nestjs/common';
import { ApiOperation, ApiQuery, ApiResponse } from '@nestjs/swagger';
import BigNumber from 'bignumber.js';
Expand All @@ -15,6 +17,7 @@ import { ContractAddress } from '@/clients/sdk-client.model';
import { calculateUsdValue } from '@/lib/utils';

@Controller('history')
@UseInterceptors(CacheInterceptor)
export class PairLiquidityInfoHistoryController {
constructor(
private readonly pairLiquidityInfoHistoryService: PairLiquidityInfoHistoryService,
Expand Down Expand Up @@ -76,6 +79,7 @@ export class PairLiquidityInfoHistoryController {
required: false,
})
@ApiResponse({ status: 200, type: [PairLiquidityInfoHistoryEntry] })
@CacheTTL(24 * 60 * 60 * 1000)
async findAll(
@Query('limit', new ParseIntPipe({ optional: true })) limit: number = 100,
@Query('offset', new ParseIntPipe({ optional: true })) offset: number = 0,
Expand Down
4 changes: 4 additions & 0 deletions src/api/pairs/pairs.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { CacheInterceptor, CacheTTL } from '@nestjs/cache-manager';
import {
Controller,
Get,
NotFoundException,
Param,
Query,
UseInterceptors,
} from '@nestjs/common';
import { ApiOperation, ApiParam, ApiQuery, ApiResponse } from '@nestjs/swagger';
import * as prisma from '@prisma/client';
Expand Down Expand Up @@ -45,6 +47,7 @@ const toLiquidityInfoDto = (
): dto.LiquidityInfo | undefined =>
liquidityInfo ? removeId(liquidityInfo) : undefined;

@UseInterceptors(CacheInterceptor)
@Controller('pairs')
export class PairsController {
constructor(private readonly pairsService: PairsService) {}
Expand All @@ -70,6 +73,7 @@ for this purpose use the individual \`pairs/:address\` route`,
required: false,
})
@ApiResponse({ status: 200, type: [dto.PairWithUsd] })
@CacheTTL(24 * 60 * 60 * 1000)
async getAllPairs(
@Query('only-listed') onlyListedStr?: string, // false | true
@Query('token') token?: ContractAddress,
Expand Down
5 changes: 5 additions & 0 deletions src/api/tokens/tokens.controller.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CacheInterceptor, CacheTTL } from '@nestjs/cache-manager';
import {
Controller,
Delete,
Expand All @@ -7,6 +8,7 @@ import {
Param,
Post,
UnauthorizedException,
UseInterceptors,
} from '@nestjs/common';
import {
ApiHeaders,
Expand Down Expand Up @@ -43,6 +45,8 @@ const toDtoToken = ({
id, // eslint-disable-line @typescript-eslint/no-unused-vars
...tail
}: prisma.Token) => tail;

@UseInterceptors(CacheInterceptor)
@Controller('tokens')
export class TokensController {
constructor(private readonly tokensService: TokensService) {}
Expand All @@ -53,6 +57,7 @@ export class TokensController {
description: `All the tokens no matter if are officially supported by the DEX (listed=true) or not will be retrieved`,
})
@ApiResponse({ status: 200, type: [dto.TokenWithUsd] })
@CacheTTL(24 * 60 * 60 * 1000)
async getAllTokens(): Promise<dto.TokenWithUsd[]> {
return this.tokensService.getAllTokensWithAggregation();
}
Expand Down
11 changes: 10 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CacheModule } from '@nestjs/cache-manager';
import { Module } from '@nestjs/common';

import { ApiModule } from '@/api/api.module';
Expand All @@ -13,7 +14,15 @@ import { PairSyncService } from '@/tasks/pair-sync/pair-sync.service';
import { TasksModule } from '@/tasks/tasks.module';

@Module({
imports: [ApiModule, ClientsModule, DatabaseModule, TasksModule],
imports: [
ApiModule,
ClientsModule,
DatabaseModule,
TasksModule,
CacheModule.register({
isGlobal: true,
}),
],
controllers: [AppController],
providers: [
MdwWsClientService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { PairLiquidityInfoHistoryDbService } from '@/database/pair-liquidity-inf
import { bigIntToDecimal } from '@/lib/utils';
import { PairLiquidityInfoHistoryImporterService } from '@/tasks/pair-liquidity-info-history-importer/pair-liquidity-info-history-importer.service';
import resetAllMocks = jest.resetAllMocks;
import { CacheModule } from '@nestjs/cache-manager';

import { CoinmarketcapClientService } from '@/clients/coinmarketcap-client.service';
import { PairLiquidityInfoHistoryErrorDbService } from '@/database/pair-liquidity-info-history-error/pair-liquidity-info-history-error-db.service';
import {
Expand Down Expand Up @@ -73,6 +75,11 @@ describe('PairLiquidityInfoHistoryImporterService', () => {
useValue: mockCoinmarketcapClient,
},
],
imports: [
CacheModule.register({
isGlobal: true,
}),
],
}).compile();
service = module.get<PairLiquidityInfoHistoryImporterService>(
PairLiquidityInfoHistoryImporterService,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Cache } from '@nestjs/cache-manager';
import { Injectable, Logger } from '@nestjs/common';
import { orderBy } from 'lodash';

Expand Down Expand Up @@ -41,6 +42,7 @@ export class PairLiquidityInfoHistoryImporterService {
private mdwClient: MdwHttpClientService,
private sdkClient: SdkClientService,
private coinmarketcapClient: CoinmarketcapClientService,
private cacheManager: Cache,
) {}

readonly logger = new Logger(PairLiquidityInfoHistoryImporterService.name);
Expand Down Expand Up @@ -265,6 +267,7 @@ export class PairLiquidityInfoHistoryImporterService {
}

if (numUpserted > 0) {
await this.cacheManager.reset();
this.logger.log(
`Completed sync for pair ${pairWithTokens.id} ${pairWithTokens.address}. Synced ${numUpserted} log(s).`,
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Cache } from '@nestjs/cache-manager';
import { Injectable, Logger } from '@nestjs/common';
import { Decimal } from '@prisma/client/runtime/library';
import BigNumber from 'bignumber.js';
Expand All @@ -11,6 +12,7 @@ export class PairPathCalculatorService {
constructor(
private pairLiquidityInfoHistoryDb: PairLiquidityInfoHistoryDbService,
private tokenDb: TokenDbService,
private cacheManager: Cache,
) {}

readonly logger = new Logger(PairPathCalculatorService.name);
Expand All @@ -27,6 +29,8 @@ export class PairPathCalculatorService {
throw new Error('WAE token not found in db');
}

let updates = 0;

for (const entry of entries) {
this.logger.debug(`Processing entry ${entry.id}`);
// for each entry get the timestamp and use the info history db service to fetch the graph at the time
Expand Down Expand Up @@ -127,6 +131,11 @@ export class PairPathCalculatorService {
? new Decimal(aePrices[1].toString())
: new Decimal(-1),
});
updates += 1;
}
if (updates > 0) {
this.logger.debug(`Updated tokenAePrice in ${updates} entries`);
await this.cacheManager.reset();
}
}
}
6 changes: 6 additions & 0 deletions src/tasks/tasks.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CacheModule } from '@nestjs/cache-manager';
import { Test, TestingModule } from '@nestjs/testing';

import { CoinmarketcapClientService } from '@/clients/coinmarketcap-client.service';
Expand Down Expand Up @@ -36,6 +37,11 @@ describe('TasksService', () => {
TokenDbService,
PairPathCalculatorService,
],
imports: [
CacheModule.register({
isGlobal: true,
}),
],
}).compile();

tasksService = module.get<TasksService>(TasksService);
Expand Down

0 comments on commit 6d21829

Please sign in to comment.