Requests should be cached on a per-datasource level#491
Open
kyri-petrou wants to merge 3 commits intozio:series/2.xfrom
Open
Requests should be cached on a per-datasource level#491kyri-petrou wants to merge 3 commits intozio:series/2.xfrom
kyri-petrou wants to merge 3 commits intozio:series/2.xfrom
Conversation
kyri-petrou
commented
Jun 28, 2024
Comment on lines
19
to
33
| test("N + 1 on composed queries") { | ||
| for { | ||
| ref <- Ref.make(0) | ||
| ds = countingDs(ref) | ||
| query1 = identityDs.query(IdReq(1)).flatMap(i => ds.query(CountingReq(i))) | ||
| query2 = identityDs.query(IdReq(2)).flatMap(i => ds.query(CountingReq(i))) | ||
| res <- (query1 <~> query2).run | ||
| i <- ref.get | ||
| } yield assertTrue(i == 1, res == ("1", "2")) | ||
| }, | ||
| test("requests are cached per datasource") { | ||
| for { | ||
| res <- identityDs.query(IdReq(1)).flatMap(i => plus1Ds.query(IdReq(i))).run | ||
| } yield assertTrue(res == 2) | ||
| }, |
Collaborator
Author
There was a problem hiding this comment.
This test is irrelevant with the PR. It's something I've been meaning to add for some time now just as a safeguard when making changes to batching logic
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There is a rather annoying bug/issue currently with how we dedup/cache queries. The current Cache takes into consideration only the
Request, but not theDataSource. This means that if aRequestis reused across differentDataSources.For most usecases this shouldn't be a problem, because having multiple
DataSources returning the same type is unlikely. However, this is something that has happened to me before and failed to realise until it hit prod:In this case, if we execute a query against both datasources where the
idexists in both of them, we would be retrieving the result from the cache even if the request was meant for a different datasource.While the workaround is simple (create another case class for the 2nd case class), I personally believe that the existing behaviour is not correct. If I'm executing a request against a specific DataSource, I expect it to be cached on the datasource that I am making a request against.
Unfortunately, the only way to fix this is by breaking the Cache API by adding DataSource as a parameter to its methods. @paulpdaniels AFAICT you were the one that asked for the Cache to be made abstract, do you see any issues with this? (assuming you ended up using a custom Cache, otherwise ignore me 😅)