Skip to main content
Version: 2.x

How to Set a Time to Live per Entry

Cache.make gives every value the same time to live. When successful and failed lookups need different lifetimes, use Cache.makeWith instead. Its time to live function receives the Exit of each lookup and returns the time to live for that entry.

Suppose a profile lookup succeeds rarely enough to keep for ten minutes, but a failure should be retried after five seconds. We pass a function to Cache.makeWith that checks the Exit:

import zio._
import zio.cache.{Cache, Lookup}

def loadProfile(userId: String): IO[String, String] =
if (userId.isEmpty) ZIO.fail("user id must not be empty")
else ZIO.succeed(s"profile of $userId")

val profiles: UIO[Cache[String, String, String]] =
Cache.makeWith(capacity = 1000, lookup = Lookup(loadProfile))(exit =>
if (exit.isSuccess) 10.minutes else 5.seconds
)

The function runs once for each lookup, when the lookup completes. The expiry time of the entry is that moment plus the returned duration. A failed entry expires after five seconds, so the next Cache#get for that key runs the lookup again.

note

The time to live is fixed when the lookup completes. The function cannot change the lifetime of an entry after it is stored.

To give each entry a time to live based on its key or value rather than its Exit, compute the duration from the key in your own code and store values with a separate cache per lifetime.

ZIO Chat

Hello! ๐Ÿ˜Š I'm your ZIO Chat Bot, designed to be your go-to resource for all things ZIO! Whether you're new to this powerful functional effect system or a seasoned pro, I'm here to assist you. ZIO empowers you to build concurrent and resilient applications in Scala, offering referential transparency, composable data types, type-safety, and more. So, go ahead and ask me anything about ZIO โ€“ I'm here to provide the answers you need! ๐Ÿค–