Cache
In many applications, handling overlapping work is common. For example, in services that process incoming requests, it’s important to avoid redundant work like handling the same request multiple times. The Cache module helps improve performance by preventing duplicate work.
Key Features of Cache:
Feature | Description |
---|---|
Compositionality | Allows overlapping work across different parts of the application while preserving compositional programming. |
Unified Sync and Async Caches | Integrates both synchronous and asynchronous caches through a unified lookup function that computes values either way. |
Effect Integration | Works natively with the Effect library, supporting concurrent lookups, failure handling, and interruption. |
Cache Metrics | Tracks key metrics like entries, hits, and misses, providing insights for performance optimization. |
A cache is defined by a lookup function that computes the value for a given key if it’s not already cached:
The lookup function takes a Key
and returns an Effect
, which describes how to compute the value (Value
). This Effect
may require an environment (Requirements
), can fail with an Error
, and succeed with a Value
. Since it returns an Effect
, it can handle both synchronous and asynchronous workflows.
You create a cache by providing a lookup function along with a maximum size and a time-to-live (TTL) for cached values.
Once a cache is created, the most idiomatic way to work with it is the get
method.
The get
method returns the current value in the cache if it exists, or computes a new value, puts it in the cache, and returns it.
If multiple concurrent processes request the same value, it will only be computed once. All other processes will receive the computed value as soon as it is available. This is managed using Effect’s fiber-based concurrency model without blocking the underlying thread.
Example (Concurrent Cache Lookups)
In this example, we call timeConsumingEffect
three times concurrently with the same key.
The cache runs this effect only once, so concurrent lookups will wait until the value is available:
The cache is designed to be safe for concurrent access and efficient under concurrent conditions. If two concurrent processes request the same value and it is not in the cache, the value will be computed once and provided to both processes as soon as it is available. Concurrent processes will wait for the value without blocking the underlying thread.
If the lookup function fails or is interrupted, the error will be propagated to all concurrent processes waiting for the value. Failures are cached to prevent repeated computation of the same failed value. If interrupted, the key will be removed from the cache, so subsequent calls will attempt to compute the value again.
A cache is created with a specified capacity. When the cache reaches capacity, the least recently accessed values will be removed first. The cache size may slightly exceed the specified capacity between operations.
A cache can also have a specified time to live (TTL). Values older than the TTL will not be returned. The age is calculated from when the value was loaded into the cache.
In addition to get
, the cache provides several other methods:
Method | Description |
---|---|
refresh | Triggers a recomputation of the value for a key without removing the old value, allowing continued access. |
size | Returns the current size of the cache. The size is approximate under concurrent conditions. |
contains | Checks if a value associated with a specified key exists in the cache. Under concurrent access, the result is valid as of the check time but may change immediately after. |
invalidate | Evicts the value associated with a specific key. |
invalidateAll | Evicts all values from the cache. |