Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active February 3, 2026 20:20
Show Gist options
  • Select an option

  • Save dacr/4aa32e32ec3935b9e5f00d33a54f1203 to your computer and use it in GitHub Desktop.

Select an option

Save dacr/4aa32e32ec3935b9e5f00d33a54f1203 to your computer and use it in GitHub Desktop.
ZIO learning - using cached / published by https://github.com/dacr/code-examples-manager #b7f0504d-7bdd-4c7a-85e1-d460fad0e696/4f75f8ef0eeb57ebc99f304b90f26126678a99d6
// summary : ZIO learning - using cached
// keywords : scala, cached, zio, @testable
// publish : gist
// authors : David Crosson
// license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt)
// id : b7f0504d-7bdd-4c7a-85e1-d460fad0e696
// created-on : 2022-01-26T09:11:32+01:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.4.2"
//> using dep "dev.zio::zio:2.0.13"
// ---------------------
import zio.*
object Encapsulated extends ZIOAppDefault {
val run = for {
ref <- Ref.make(0)
getAndIncrement = ref.modify(n => (n, n + 1))
cached <- getAndIncrement.cached(30.seconds)
a <- cached
b <- cached
c <- cached
_ <- Console.printLine(s"$a $b $c (0 everywhere if it works)")
} yield ()
}
Encapsulated.main(Array.empty)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment