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

Add getIfCached(key) in DataLoader #50

Closed
MarcBridner opened this issue Aug 21, 2019 · 6 comments
Closed

Add getIfCached(key) in DataLoader #50

MarcBridner opened this issue Aug 21, 2019 · 6 comments

Comments

@MarcBridner
Copy link

MarcBridner commented Aug 21, 2019

It'd be great if the DataLoader had a getIfCached method returning the future (or the actual object) if it exists, without triggering a load. Alternatively if the DataLoader had it's member variables as protected so that the class could be extended.

We have a quite complicated setup with lots of relationships between entities as well as partial loading of some entities. Occasionally we want to check if an item is in the DL cache and use it if it is (and has the data we need), otherwise go down a different path.

We also prime the cache of one data loader when loading a list of the same entities in a batch loader belonging to another data loader.

We try to avoid chaining dataloader calls in a thenCompose due to manual invocation of dispatch() causing double loads of the same entity from the same data-loader, so we instead want to implement some slightly more intelligent batch loaders.

@bbakerman
Copy link
Member

Are you think of a sifnature like

Optional<CompleteFuture<V>> loadIfCached(K key)

Where is returns the cached CF otherwise Optinal.empty()?

Remember the CF might not be resolved but its in the cache of CFs. Also remember the cache is a cache of promises ot Values not the Values themselves. You cant be sure the CF to Value has finished hence you cant really inspect it (I guess you can via java.util.concurrent.CompletableFuture#isDone but its pretty fiddly)

Is that what you are after?

@bbakerman
Copy link
Member

if you could propose a change via a PR that would be great

@bbakerman
Copy link
Member

I started a proof of concept here

#52

Is this what you are after?

@MarcBridner
Copy link
Author

Yes, that looks exactly like what I'm after. I was going to try to do something similar with a locally modified copy, but I guess you beat me to it :)

@bbakerman
Copy link
Member

I have released 2.2.3 with getIfPresent and getIfCompleted

@MarcBridner
Copy link
Author

Thank you! That was super fast :)

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

No branches or pull requests

2 participants