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

Caching in readme #306

Closed
DenisNovac opened this issue May 7, 2020 · 2 comments
Closed

Caching in readme #306

DenisNovac opened this issue May 7, 2020 · 2 comments

Comments

@DenisNovac
Copy link

There is this example of caching at the end of readme:

def fetchTwice[F[_] : Concurrent]: Fetch[F, (String, String)] = for {
  one <- fetchString(1)
  two <- fetchString(1)
} yield (one, two)

Isn't this just a deduplication? I mean, results are not saved to the next call:

val l: Fetch[IO, (Option[String], Option[String])] = for {
    d1 <- data.fetchElem(0)
    d2 <- data.fetchElem(0)
  } yield (d1, d2)
  Fetch.run(l).unsafeRunSync()  // asks for elem 0 once
  Fetch.run(data.fetchElem(0)).unsafeRunSync()  // asks for elem 0 too, not cached?
@sloshy
Copy link
Contributor

sloshy commented Jan 14, 2021

As mentioned in the comment in the referenced issue, this is because a new cache is created each time you run a Fetch, by default. If, instead, you want to cache between requests, you should use runCache instead which returns the cache, which you can in turn pass to each subsequent Fetch request. It will then use that and properly fetch the element from the local cache.

I will create a PR to clear this up in the README so that this behavior is much more obvious.

@DenisNovac
Copy link
Author

@sloshy that is a great addition, thank you.

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