-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into update-zio-quickstart-restful
- Loading branch information
Showing
3 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
scalaVersion := "2.13.13" | ||
|
||
libraryDependencies += "dev.zio" %% "zio-cache" % "0.2.3" |
43 changes: 43 additions & 0 deletions
43
zio-quickstart-cache/src/main/scala/dev/zio/quickstart/CacheApp.scala
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package dev.zio.quickstart | ||
|
||
import zio._ | ||
import zio.cache.{Cache, Lookup} | ||
import java.time.temporal.ChronoUnit | ||
|
||
object CacheApp extends ZIOAppDefault { | ||
private val key = "key1" | ||
|
||
private def timeConsumingEffect(key: String) = | ||
ZIO.sleep(5.seconds).as(key.hashCode) | ||
|
||
def run = | ||
for { | ||
cache <- Cache.make( | ||
capacity = 100, | ||
timeToLive = Duration.Infinity, | ||
lookup = Lookup(timeConsumingEffect) | ||
) | ||
_ <- Console.printLine(s"Start getting results by $key:") | ||
startTime <- Clock.currentTime(ChronoUnit.SECONDS) | ||
result <- cache | ||
.get(key) | ||
.zipPar(cache.get(key)) | ||
.zipPar(cache.get(key)) | ||
endTime <- Clock.currentTime(ChronoUnit.SECONDS) | ||
_ <- ZIO.debug( | ||
s"Result of parallel execution of three effects with $key: $result" | ||
) | ||
|
||
hits <- cache.cacheStats.map(_.hits) | ||
misses <- cache.cacheStats.map(_.misses) | ||
keyLastLoadedTimeOpt <- cache.entryStats(key) | ||
keyLastLoadedTime <- ZIO.fromOption(keyLastLoadedTimeOpt).map(_.loaded) | ||
_ <- ZIO.debug(s"Number of cache hits: $hits") | ||
_ <- ZIO.debug(s"Number of cache misses: $misses") | ||
_ <- ZIO.debug(s"The last time of getting $key: $keyLastLoadedTime") | ||
_ <- ZIO.debug( | ||
s"Time to get result by $key 3 times: ${endTime - startTime} seconds" | ||
) | ||
} yield () | ||
|
||
} |