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

Fetch.run in batches without optimizations #305

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

Fetch.run in batches without optimizations #305

DenisNovac opened this issue May 6, 2020 · 2 comments

Comments

@DenisNovac
Copy link

Hi everyone.

I have this piece of code:

def fetchRandomInt(max: Int) = Fetch(max, randomSource.source)

val fetchTrv: Fetch[IO, List[Int]] = List(10, 10, 10, 10, 10).traverse(fetchRandomInt)

println(Fetch.run(fetchTrv).unsafeRunSync)  // List(7, 7, 7, 7, 7)

Where randomSource is just a generator of random ints:

override def fetch(max: Int): IO[Option[Int]] =
  CF.delay {
    scala.util.Random.nextInt(max).some
  }

Is there any way to run this list of commands concurrently in one batch but without optimization by ID?

@sloshy
Copy link
Contributor

sloshy commented Jan 14, 2021

Hi, obviously this is a late response and I apologize for that. I'm new to the project but I believe I can help you (or future people browsing/arriving from Google) here.

As you may have guessed from the other issue you posted (#306), the Fetch library uses a cache (DataCache) with every call to .run, with the default cache being a brand new InMemoryCache. The behavior of this cache is it effectively deduplicates requests within a single Fetch run. The implementation of .traverse here means that this cache is shared amongst each request so you see this behavior. If you do not traverse or flatMap them and instead run them individually (as .run without passing in any cache), you should see a random number generated each time, as you expect.

@DenisNovac
Copy link
Author

@sloshy thank you for answer.

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