Last active
May 6, 2023 15:39
-
-
Save dacr/cae54752f18c3175145f693a95bde82a to your computer and use it in GitHub Desktop.
ZIO learning - using cached ttl and explicit invalidation / published by https://github.com/dacr/code-examples-manager #ebd2b441-6aa0-488e-ab6a-97c2cbe15b8f/9e88d13987225726fa511e77011640675547f866
This file contains 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 ttl and explicit invalidation | |
// keywords : scala, cached, ttl, zio, invalidate, @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 : ebd2b441-6aa0-488e-ab6a-97c2cbe15b8f | |
// created-on : 2022-01-26T17:27:49+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)) | |
cachedInvalidator <- getAndIncrement.cachedInvalidate(2.seconds) | |
(cached, invalidator) = cachedInvalidator | |
a1 <- cached | |
a2 <- cached | |
_ <- invalidator // Enough to invalidate ! | |
a3 <- cached | |
a4 <- cached | |
_ <- Clock.sleep(2100.millis) | |
a5 <- cached | |
a6 <- cached | |
_ <- Console.printLine(List(a1, a2, a3, a4, a5, a6).mkString("-")) | |
} yield () | |
} | |
Encapsulated.main(Array.empty) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment