Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 6, 2023 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/4aa32e32ec3935b9e5f00d33a54f1203 to your computer and use it in GitHub Desktop.
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/4223c46feae8b1570b41ccde83b2d0d36b12ca51
// summary : ZIO learning - using cached
// keywords : scala, cached, zio, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// 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.2.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