Skip to content

Commit

Permalink
Dynamic caching
Browse files Browse the repository at this point in the history
  • Loading branch information
jordankzf committed Mar 28, 2023
1 parent bf45346 commit 52f19d8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
17 changes: 14 additions & 3 deletions apps/server/src/ethereum/controllers/TransactionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,20 @@ export class TransactionsController {
async getTransactions(
@Query(new ValidationPipe()) { fromDate, toDate }: TransactionsQueryDto,
): Promise<TransactionsDto[] | undefined> {
const dateFrom = new Date(fromDate);
const dateTo = new Date(toDate);
const today = new Date();

const isTodayOrInFuture = dateTo >= today;
const cacheTtl = isTodayOrInFuture ? 5 * 60_000 : 3600_000 * 24; // 5 minutes if toDate is today or in the future, 1 day otherwise

const cacheKey = `ETH_TX_${fromDate}_${toDate}`;
return this.cache.get(cacheKey, async () => this.ethereumTransactionsService.getTransactions(fromDate, toDate), {
ttl: 3600_000 * 24, // 1 day
});
return this.cache.get(
cacheKey,
async () => this.ethereumTransactionsService.getTransactions(dateFrom, dateTo, today),
{
ttl: cacheTtl,
},
);
}
}
16 changes: 5 additions & 11 deletions apps/server/src/ethereum/services/EthereumTransactionsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,14 @@ import { BadRequestException, HttpException, HttpStatus, Injectable } from '@nes

import { SupportedEVMTokenSymbols } from '../../AppConfig';
import { PrismaService } from '../../PrismaService';
import { TransactionsDto, TransactionsQueryDto } from '../EthereumInterface';
import { TransactionsDto } from '../EthereumInterface';

@Injectable()
export class EthereumTransactionsService {
constructor(private prisma: PrismaService) {}

async getTransactions(
fromDate: TransactionsQueryDto['fromDate'],
toDate: TransactionsQueryDto['toDate'],
): Promise<TransactionsDto[]> {
async getTransactions(dateFrom: Date, dateTo: Date, today: Date): Promise<TransactionsDto[]> {
try {
const dateFrom = new Date(fromDate);
const dateTo = new Date(toDate);
const today = new Date();

if (dateFrom > today) {
throw new BadRequestException(`Cannot query future date`);
}
Expand Down Expand Up @@ -46,8 +39,9 @@ export class EthereumTransactionsService {
} catch (e: any) {
throw new HttpException(
{
status: e.code || HttpStatus.INTERNAL_SERVER_ERROR,
error: `API call for Ethereum transactions was unsuccessful: ${e.message}`,
statusCode: e.status ?? HttpStatus.INTERNAL_SERVER_ERROR,
error: `API call for Ethereum transactions was unsuccessful`,
message: e.message,
},
HttpStatus.INTERNAL_SERVER_ERROR,
{
Expand Down
24 changes: 19 additions & 5 deletions apps/server/test-i9n/testing/ethereum/Transactions.i9n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,29 @@ describe('Transactions Service Test', () => {
expect(parsedPayload.message).toStrictEqual(['toDate must be a valid ISO 8601 date string']);
});

it(`should throw an error if future date is provided`, async () => {
it(`should throw an error if fromDate is in the future`, async () => {
const txReceipt = await testing.inject({
method: 'GET',
url: `/ethereum/transactions?fromDate=2033-03-15&toDate=2033-03-16`,
});

expect(JSON.parse(txReceipt.payload).status).toStrictEqual(500);
expect(JSON.parse(txReceipt.payload).error).toStrictEqual(
'API call for Ethereum transactions was unsuccessful: Cannot query future date',
);
const parsedPayload = JSON.parse(txReceipt.payload);

expect(parsedPayload.statusCode).toStrictEqual(400);
expect(parsedPayload.error).toStrictEqual('API call for Ethereum transactions was unsuccessful');
expect(parsedPayload.message).toStrictEqual('Cannot query future date');
});

it(`should throw an error fromDate is more recent than toDate`, async () => {
const txReceipt = await testing.inject({
method: 'GET',
url: `/ethereum/transactions?fromDate=2023-03-15&toDate=2023-03-14`,
});

const parsedPayload = JSON.parse(txReceipt.payload);

expect(parsedPayload.statusCode).toStrictEqual(400);
expect(parsedPayload.error).toStrictEqual('API call for Ethereum transactions was unsuccessful');
expect(parsedPayload.message).toStrictEqual('fromDate cannot be more recent than toDate');
});
});

0 comments on commit 52f19d8

Please sign in to comment.