← ZIO Cache docs

Cache

zio.cache.Cache
See theCache companion object
abstract class Cache[-Key, +Error, +Value]

A Cache is defined in terms of a lookup function that, given a key of type Key, can either fail with an error of type Error or succeed with a value of type Value. Getting a value from the cache will either return the previous result of the lookup function if it is available or else compute a new result with the lookup function, put it in the cache, and return it.

A cache also has a specified capacity and time to live. When the cache is at capacity the least recently accessed values in the cache will be removed to make room for new values. Getting a value with a life older than the specified time to live will result in a new value being computed with the lookup function and returned when available.

The cache is safe for concurrent access. If multiple fibers attempt to get the same key the lookup function will only be computed once and the result will be returned to all fibers.

Attributes

Companion
object
Source
Cache.scala
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Abstract methods

def cacheStats(implicit trace: Trace): UIO[CacheStats]

Returns statistics for this cache.

Returns statistics for this cache.

Attributes

Source
Cache.scala
def contains(key: Key)(implicit trace: Trace): UIO[Boolean]

Returns whether a value associated with the specified key exists in the cache.

Returns whether a value associated with the specified key exists in the cache.

Attributes

Source
Cache.scala
def entryStats(key: Key)(implicit trace: Trace): UIO[Option[EntryStats]]

Returns statistics for the specified entry.

Returns statistics for the specified entry.

Attributes

Source
Cache.scala
def get(key: Key)(implicit trace: Trace): IO[Error, Value]

Retrieves the value associated with the specified key if it exists. Otherwise computes the value with the lookup function, puts it in the cache, and returns it.

Retrieves the value associated with the specified key if it exists. Otherwise computes the value with the lookup function, puts it in the cache, and returns it.

Attributes

Source
Cache.scala
def invalidate(key: Key)(implicit trace: Trace): UIO[Unit]

Invalidates the value associated with the specified key.

Invalidates the value associated with the specified key.

Attributes

Source
Cache.scala

Invalidates all values in the cache.

Invalidates all values in the cache.

Attributes

Source
Cache.scala
def refresh(key: Key): IO[Error, Unit]

Computes the value associated with the specified key, with the lookup function, and puts it in the cache. The difference between this and get method is that refresh triggers (re)computation of the value without invalidating it in the cache, so any request to the associated key can still be served while the value is being re-computed/retrieved by the lookup function. Additionally, refresh always triggers the lookup function, disregarding the last Error.

Computes the value associated with the specified key, with the lookup function, and puts it in the cache. The difference between this and get method is that refresh triggers (re)computation of the value without invalidating it in the cache, so any request to the associated key can still be served while the value is being re-computed/retrieved by the lookup function. Additionally, refresh always triggers the lookup function, disregarding the last Error.

Attributes

Source
Cache.scala
def size(implicit trace: Trace): UIO[Int]

Returns the approximate number of values in the cache.

Returns the approximate number of values in the cache.

Attributes

Source
Cache.scala