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

Extract getFromCacheOrExecuteAndCache utility function #1777

Merged
merged 6 commits into from
Jul 26, 2024

Conversation

hectorgomezv
Copy link
Member

@hectorgomezv hectorgomezv commented Jul 19, 2024

Summary

This PR extracts the utility function used to retrieve data from the service cache before executing a query to its own class.

A new CachedQueryResolver was created, which exposes a get function that:

  • Receives a CacheDir, a query, and ttl value.
  • Tries to get the data from the cache, using the record pointed by CacheDir. If the data is found, it's returned, if it's not found, then it tries to get it from the database. If successful, caches the data under CacheDir with ttl TTL.

(More context in: #1773 (comment))

Changes

  • Adds CachedQueryResolver class, to be used to retrieve data from the service cache before executing a query.

@hectorgomezv hectorgomezv self-assigned this Jul 19, 2024
@coveralls
Copy link

coveralls commented Jul 19, 2024

Pull Request Test Coverage Report for Build 10109419150

Details

  • 10 of 29 (34.48%) changed or added relevant lines in 4 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage increased (+0.01%) to 48.469%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/datasources/accounts/accounts.datasource.ts 2 6 33.33%
src/datasources/db/cached-query-resolver.ts 5 20 25.0%
Files with Coverage Reduction New Missed Lines %
src/datasources/accounts/accounts.datasource.ts 1 17.19%
Totals Coverage Status
Change from base Build 10109372074: 0.01%
Covered Lines: 4338
Relevant Lines: 7217

💛 - Coveralls

Base automatically changed from type-datasources-arguments to add-counterfactual-safes-datasource July 22, 2024 12:41
Base automatically changed from add-counterfactual-safes-datasource to main July 22, 2024 13:01
@hectorgomezv hectorgomezv force-pushed the cached-query-resolver branch from 09a0b7d to c3d9ba4 Compare July 23, 2024 10:55
@hectorgomezv hectorgomezv marked this pull request as ready for review July 23, 2024 15:57
@hectorgomezv hectorgomezv requested a review from a team as a code owner July 23, 2024 15:57
this.defaultExpirationTimeInSeconds,
);
query: this.sql<
Account[]
Copy link
Member

Choose a reason for hiding this comment

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

As the generic is defined on get, I don't think we need this here. (I won't mark the other instances.)

Copy link
Member Author

Choose a reason for hiding this comment

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

Done in 4f3b976

key: 'key',
field: 'field',
});
expect(mockQuery.catch).not.toHaveBeenCalled();
Copy link
Member

Choose a reason for hiding this comment

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

Nit: we should also check that then was called.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've changed the approach in 4f3b976, to explicitly call execute on the query so the promise handling is simpler (and also the testing). According to the library docs, it would cause the execution to happen on a single Nodejs tick.

const cacheDir = { key: 'key', field: 'field' };
const ttl = faker.number.int({ min: 1, max: 1000 });
const dbResult = { ...JSON.parse(fakeJson()), count: 1 };
mockQuery.catch.mockResolvedValue(dbResult);
Copy link
Member

Choose a reason for hiding this comment

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

Nit: to me this is when the query throws? We should adjust the description and add a test for then.

Copy link
Member Author

Choose a reason for hiding this comment

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

This is also affected by #1777 (comment)

src/datasources/db/cached-query-resolver.ts Outdated Show resolved Hide resolved
@hectorgomezv hectorgomezv requested a review from iamacook July 24, 2024 09:43
Comment on lines 51 to 52
const cacheContent = await fakeCacheService.get(cacheDir);
expect(cacheContent).toBe(JSON.stringify(value));
Copy link
Member

Choose a reason for hiding this comment

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

Nit: this is unecessary as we set the cache value.

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed. Thanks! Removed in e4138c0

@hectorgomezv hectorgomezv force-pushed the cached-query-resolver branch from f90dda5 to 42d9c6c Compare July 26, 2024 09:58
@hectorgomezv hectorgomezv requested a review from iamacook July 26, 2024 10:00
@hectorgomezv hectorgomezv merged commit 40b1555 into main Jul 26, 2024
16 checks passed
@hectorgomezv hectorgomezv deleted the cached-query-resolver branch July 26, 2024 15:34
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