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

Ability to add cache entries directly, bypassing the Lookup function #39

Open
ithinkicancode opened this issue Aug 26, 2021 · 7 comments · May be fixed by #56
Open

Ability to add cache entries directly, bypassing the Lookup function #39

ithinkicancode opened this issue Aug 26, 2021 · 7 comments · May be fixed by #56

Comments

@ithinkicancode
Copy link
Contributor

Motivation

In some scenarios, we might want to add a value to the cache directly without calling the lookup function. An example of such business logic is we have some special values that don't exist in the database (from which the lookup function retrieves values), nonetheless we want to serve these special values by injecting them directly into the cache.

Considerations

The decision to bypass lookup is most likely determined by some external conditions. Currently the Lookup function has the following signature:

def lookup(key: Key): ZIO[Environment, Error, Value]

And this function is provided upfront during the construction of the cache, when the conditions to bypass might not be available.

We'd probably either need to express the conditions and the value to add through Environment, or need to add additional (optional) parameters to the lookup function.

@fanf
Copy link

fanf commented Aug 27, 2021

This is important and what currently make us unable to use zio-cache. Our use case is that we have data persisted in base. When the app starts, or after a cache reset (which means we also need #43), we would load data from the base using the provided lookup method.
But we have only one way to create new values for these data (A Big Computation (tm)). It makes little sens to persist in base and make the cache go lookup here (in terms of IO) when we know that the data is updated. Plus it would help keep our consistancy model unchanged ((the cache is the law).

@DiegoTavares
Copy link

This is also in our way to start using zio-cache.

@calvinlfer
Copy link
Member

Hey all, I would find this useful as well as another use case to motivate this is the bulk addition of values into the cache.
The other way is perhaps to enable to Lookup function to support bulk retrieval and allow many keys to be provided

@adamgfraser
Copy link
Contributor

Yes I think supporting looking up a collection of values would be a better way to support this. I have an outstanding PR to add this that I will update.

@sinha-abhi
Copy link

sinha-abhi commented Feb 2, 2024

@adamgfraser I think you're referring to this PR #143. Do you have an ETA on when that will be added in/if there are still plans to support multi-lookup?

@valenterry
Copy link

I would really like to have this too. This is a very basic requirement for every cache imho.

@valenterry
Copy link

I digged into this and it I think this won't be possible with the current design. The Value-type of the cache is marked covariant, which means that there is no way to safely insert values into the cache. The only way that I can see to workaround this is to create something similar to Lookup - let's call it SetValue - then pass it to the cache at the time of creation and then use SetValue outside of the cache to set values.

It would probably be better to have another version of the cache that is invariant, which would allow to add a set-method directly next to the get-method.

Thoughts from the maintainers? I would start working on a PR if the design goal is clear.

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 a pull request may close this issue.

7 participants