Skip to content

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:

FeatureDescription
CompositionalityAllows overlapping work across different parts of the application while preserving compositional programming.
Unified Sync and Async CachesIntegrates both synchronous and asynchronous caches through a unified lookup function that computes values either way.
Effect IntegrationWorks natively with the Effect library, supporting concurrent lookups, failure handling, and interruption.
Cache MetricsTracks 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:

type Lookup<Key, Value, Error, Requirements> = (
key: Key
) => Effect<Value, Error, Requirements>

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.

declare const make: <Key, Value, Error, Requirements>(options: {
readonly capacity: number
readonly timeToLive: Duration.DurationInput
readonly lookup: Lookup<Key, Value, Error, Requirements>
}) => Effect<Cache<Key, Value, Error>, never, Requirements>

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:

1
import {
import Effect
Effect
,
import Cache
Cache
,
import Duration
Duration
} from "effect"
2
3
// Simulating an expensive lookup with a delay
4
const
const expensiveLookup: (key: string) => Effect.Effect<number, never, never>
expensiveLookup
= (
(parameter) key: string
key
: string) =>
5
import Effect
Effect
.
const sleep: (duration: Duration.DurationInput) => Effect.Effect<void>

Returns an effect that suspends for the specified duration. This method is asynchronous, and does not actually block the fiber executing the effect.

sleep
("2 seconds").
(method) Pipeable.pipe<Effect.Effect<void, never, never>, Effect.Effect<number, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<void, never, never>) => Effect.Effect<number, never, never>): Effect.Effect<...> (+21 overloads)
pipe
(
import Effect
Effect
.
const as: <number>(value: number) => <A, E, R>(self: Effect.Effect<A, E, R>) => Effect.Effect<number, E, R> (+1 overload)

This function maps the success value of an `Effect` value to a specified constant value.

as
(
(parameter) key: string
key
.
(property) String.length: number

Returns the length of a String object.

length
))
6
7
const
const program: Effect.Effect<void, never, never>
program
=
import Effect
Effect
.
const gen: <YieldWrap<Effect.Effect<Cache.Cache<string, number, never>, never, never>> | YieldWrap<Effect.Effect<[number, number, number], never, never>> | YieldWrap<...>, void>(f: (resume: Effect.Adapter) => Generator<...>) => Effect.Effect<...> (+1 overload)
gen
(function* () {
8
// Create a cache with a capacity of 100 and an infinite TTL
9
const
const cache: Cache.Cache<string, number, never>
cache
= yield*
import Cache
Cache
.
const make: <string, number, never, never>(options: { readonly capacity: number; readonly timeToLive: Duration.DurationInput; readonly lookup: Cache.Lookup<string, number, never, never>; }) => Effect.Effect<...>

Constructs a new cache with the specified capacity, time to live, and lookup function.

make
({
10
(property) capacity: number
capacity
: 100,
11
(property) timeToLive: Duration.DurationInput
timeToLive
:
import Duration
Duration
.
const infinity: Duration.Duration
infinity
,
12
(property) lookup: Cache.Lookup<string, number, never, never>
lookup
:
const expensiveLookup: (key: string) => Effect.Effect<number, never, never>
expensiveLookup
13
})
14
15
// Perform concurrent lookups using the same key
16
const
const result: [number, number, number]
result
= yield*
import Effect
Effect
.
const all: <readonly [Effect.Effect<number, never, never>, Effect.Effect<number, never, never>, Effect.Effect<number, never, never>], { concurrency: "unbounded"; }>(arg: readonly [...], options?: { ...; } | undefined) => Effect.Effect<...>

Runs all the provided effects in sequence respecting the structure provided in input. Supports multiple arguments, a single argument tuple / array or record / struct.

all
(
17
[
const cache: Cache.Cache<string, number, never>
cache
.
(method) Cache<string, number, never>.get(key: string): Effect.Effect<number, never, never>

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.

get
("key1"),
const cache: Cache.Cache<string, number, never>
cache
.
(method) Cache<string, number, never>.get(key: string): Effect.Effect<number, never, never>

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.

get
("key1"),
const cache: Cache.Cache<string, number, never>
cache
.
(method) Cache<string, number, never>.get(key: string): Effect.Effect<number, never, never>

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.

get
("key1")],
18
{
(property) concurrency: "unbounded"
concurrency
: "unbounded" }
19
)
20
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(
21
"Result of parallel execution of three effects" +
22
`with the same key: ${
const result: [number, number, number]
result
}`
23
)
24
25
// Fetch and display cache stats
26
const
const hits: number
hits
= yield*
const cache: Cache.Cache<string, number, never>
cache
.
(property) ConsumerCache<string, number, never>.cacheStats: Effect.Effect<Cache.CacheStats, never, never>

Returns statistics for this cache.

cacheStats
.
(method) Pipeable.pipe<Effect.Effect<Cache.CacheStats, never, never>, Effect.Effect<number, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<Cache.CacheStats, never, never>) => Effect.Effect<...>): Effect.Effect<...> (+21 overloads)
pipe
(
27
import Effect
Effect
.
const map: <Cache.CacheStats, number>(f: (a: Cache.CacheStats) => number) => <E, R>(self: Effect.Effect<Cache.CacheStats, E, R>) => Effect.Effect<number, E, R> (+1 overload)
map
((
(parameter) stats: Cache.CacheStats
stats
) =>
(parameter) stats: Cache.CacheStats
stats
.
(property) CacheStats.hits: number
hits
)
28
)
29
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(`Number of cache hits: ${
const hits: number
hits
}`)
30
const
const misses: number
misses
= yield*
const cache: Cache.Cache<string, number, never>
cache
.
(property) ConsumerCache<string, number, never>.cacheStats: Effect.Effect<Cache.CacheStats, never, never>

Returns statistics for this cache.

cacheStats
.
(method) Pipeable.pipe<Effect.Effect<Cache.CacheStats, never, never>, Effect.Effect<number, never, never>>(this: Effect.Effect<...>, ab: (_: Effect.Effect<Cache.CacheStats, never, never>) => Effect.Effect<...>): Effect.Effect<...> (+21 overloads)
pipe
(
31
import Effect
Effect
.
const map: <Cache.CacheStats, number>(f: (a: Cache.CacheStats) => number) => <E, R>(self: Effect.Effect<Cache.CacheStats, E, R>) => Effect.Effect<number, E, R> (+1 overload)
map
((
(parameter) stats: Cache.CacheStats
stats
) =>
(parameter) stats: Cache.CacheStats
stats
.
(property) CacheStats.misses: number
misses
)
32
)
33
namespace console var console: Console

The `console` module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers. The module exports two specific components: * A `Console` class with methods such as `console.log()`, `console.error()` and `console.warn()` that can be used to write to any Node.js stream. * A global `console` instance configured to write to [`process.stdout`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstdout) and [`process.stderr`](https://nodejs.org/docs/latest-v22.x/api/process.html#processstderr). The global `console` can be used without importing the `node:console` module. _**Warning**_: The global console object's methods are neither consistently synchronous like the browser APIs they resemble, nor are they consistently asynchronous like all other Node.js streams. See the [`note on process I/O`](https://nodejs.org/docs/latest-v22.x/api/process.html#a-note-on-process-io) for more information. Example using the global `console`: ```js console.log('hello world'); // Prints: hello world, to stdout console.log('hello %s', 'world'); // Prints: hello world, to stdout console.error(new Error('Whoops, something bad happened')); // Prints error message and stack trace to stderr: // Error: Whoops, something bad happened // at [eval]:5:15 // at Script.runInThisContext (node:vm:132:18) // at Object.runInThisContext (node:vm:309:38) // at node:internal/process/execution:77:19 // at [eval]-wrapper:6:22 // at evalScript (node:internal/process/execution:76:60) // at node:internal/main/eval_string:23:3 const name = 'Will Robinson'; console.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to stderr ``` Example using the `Console` class: ```js const out = getStreamSomehow(); const err = getStreamSomehow(); const myConsole = new console.Console(out, err); myConsole.log('hello world'); // Prints: hello world, to out myConsole.log('hello %s', 'world'); // Prints: hello world, to out myConsole.error(new Error('Whoops, something bad happened')); // Prints: [Error: Whoops, something bad happened], to err const name = 'Will Robinson'; myConsole.warn(`Danger ${name}! Danger!`); // Prints: Danger Will Robinson! Danger!, to err ```

console
.
(method) Console.log(message?: any, ...optionalParams: any[]): void

Prints to `stdout` with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to [`printf(3)`](http://man7.org/linux/man-pages/man3/printf.3.html) (the arguments are all passed to [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args)). ```js const count = 5; console.log('count: %d', count); // Prints: count: 5, to stdout console.log('count:', count); // Prints: count: 5, to stdout ``` See [`util.format()`](https://nodejs.org/docs/latest-v22.x/api/util.html#utilformatformat-args) for more information.

log
(`Number of cache misses: ${
const misses: number
misses
}`)
34
})
35
36
import Effect
Effect
.
const runPromise: <void, never>(effect: Effect.Effect<void, never, never>, options?: { readonly signal?: AbortSignal; } | undefined) => Promise<void>

Executes an effect and returns a `Promise` that resolves with the result. Use `runPromise` when working with asynchronous effects and you need to integrate with code that uses Promises. If the effect fails, the returned Promise will be rejected with the error.

runPromise
(
const program: Effect.Effect<void, never, never>
program
)
37
/*
38
Output:
39
Result of parallel execution of three effects with the same key: 4,4,4
40
Number of cache hits: 2
41
Number of cache misses: 1
42
*/

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:

MethodDescription
refreshTriggers a recomputation of the value for a key without removing the old value, allowing continued access.
sizeReturns the current size of the cache. The size is approximate under concurrent conditions.
containsChecks 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.
invalidateEvicts the value associated with a specific key.
invalidateAllEvicts all values from the cache.