Last active
May 25, 2024 10:20
-
-
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/7b479ba1e69b59ad1090206b7c7eda890d6e5b68
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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.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