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

feat: price in memory caching #31

Merged
merged 11 commits into from
Aug 1, 2024
Merged

feat: price in memory caching #31

merged 11 commits into from
Aug 1, 2024

Conversation

0xnigir1
Copy link
Collaborator

🤖 Linear

Closes ZKS-120

Description

Add an InMemory caching layer for token prices:

  • Uses NestJS Caching Module
  • TTL of 60secs

Copy link

linear bot commented Jul 29, 2024

ZKS-120 Prices cache

Create an in-memory cache for token prices with 60s TTL.

AC:

  • getTokenPrices method modified to use the new in memory cache
  • Unit tests

Optional: use node-cache or NestJS cache

HttpModule,
CacheModule.register({
store: "memory",
ttl: 60, // seconds
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't like this value hardcoded here, is there any better solution for this ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we go with constants file ser 🫡

@0xnigir1 0xnigir1 force-pushed the feat/price-in-memory-caching branch from 747ee85 to 8e0015d Compare July 30, 2024 18:05
@0xnigir1 0xnigir1 marked this pull request as ready for review July 30, 2024 20:02
@0xnigir1 0xnigir1 requested review from 0xyaco and 0xkenj1 July 30, 2024 20:02
0xyaco
0xyaco previously approved these changes Jul 31, 2024
Copy link
Collaborator

@0xyaco 0xyaco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to address the comment, but it's good to go as it is, very elegant cache handling. 💎

},
{
provide: WINSTON_MODULE_PROVIDER,
useValue: mockLogger,
},
{
provide: CACHE_MANAGER,
useValue: createMock<Cache>({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty neat solution the createMock utility function, didn't know it existed, nice

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its from some guys that made utilities and helpers for NestJs actually jajaja

libs/pricing/src/services/coingecko.service.spec.ts Outdated Show resolved Hide resolved
LoggerModule,
CacheModule.register({
store: "memory",
ttl: TOKEN_CACHE_TTL_IN_SEC,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sweet

@@ -67,14 +80,15 @@ describe("CoingeckoService", () => {
});

describe("getTokenPrices", () => {
it("return token prices", async () => {
it("all token prices are fetched from Coingecko", async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what ? 🤣

{} as Record<string, number>,
);

const missingTokenIds = tokenIds.filter((_, index) => !cachedTokenPrices[index]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this filter could be done inside the reduce i think

@@ -50,6 +52,62 @@ export class CoingeckoService implements IPricingService {
): Promise<Record<string, number>> {
const { currency } = config;

const cacheKeys = tokenIds.map((tokenId) => this.formatTokenCacheKey(tokenId, currency));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it posible to have sth like this on the Cache ?

{ < currency > : { < tokenId > : < amount > } }

iam not a huge fan of formatting keys

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@0xyaco would love to hear your opinion here ser

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

storing an object/map as value?
the thing is that we use only tokenId as key we will get a hit but we will have to analyze the response map for the currency

a simpler solution is an only USD service

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, i think we are not gona be using other currency as baseCurrency , lets keep it like it is for now

@0xkenj1
Copy link
Collaborator

0xkenj1 commented Jul 31, 2024

i think we need to fix the pr title. try with feat: price in memory caching

@0xnigir1 0xnigir1 changed the title Feat: price in memory caching feat: price in memory caching Jul 31, 2024
Copy link
Collaborator

@0xkenj1 0xkenj1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀

@0xkenj1 0xkenj1 requested a review from 0xyaco August 1, 2024 16:02
@0xnigir1 0xnigir1 merged commit 50bb9f7 into dev Aug 1, 2024
6 checks passed
@0xnigir1 0xnigir1 deleted the feat/price-in-memory-caching branch August 1, 2024 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants