-
Notifications
You must be signed in to change notification settings - Fork 71
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
Conversation
Pull Request Test Coverage Report for Build 10109419150Details
💛 - Coveralls |
09a0b7d
to
c3d9ba4
Compare
this.defaultExpirationTimeInSeconds, | ||
); | ||
query: this.sql< | ||
Account[] |
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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)
const cacheContent = await fakeCacheService.get(cacheDir); | ||
expect(cacheContent).toBe(JSON.stringify(value)); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Co-authored-by: Aaron Cook <aaron@safe.global>
f90dda5
to
42d9c6c
Compare
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 aget
function that:CacheDir
, aquery
, andttl
value.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 underCacheDir
withttl
TTL.(More context in: #1773 (comment))
Changes
CachedQueryResolver
class, to be used to retrieve data from the service cache before executing a query.