Package: effect
Module: Effect
Returns an effect that caches its result for a specified Duration,
known as “timeToLive” (TTL).
When to use
Use when you need a costly effect result to be reused for a bounded duration before being recomputed.
Details
This function is used to cache the result of an effect for a specified amount of time. This means that the first time the effect is evaluated, its result is computed and stored.
If the effect is evaluated again within the specified timeToLive, the
cached result will be used, avoiding recomputation.
After the specified duration has passed, the cache expires, and the effect will be recomputed upon the next evaluation.
Example (Memoizing an effect with TTL)
import { Console, Effect } from "effect"
let i = 1
const expensiveTask = Effect.promise<string>(() => {
console.log("expensive task...")
return new Promise((resolve) => {
setTimeout(() => {
resolve(`result ${i++}`)
}, 100)
})
})
const program = Effect.gen(function*() {
const cached = yield* Effect.cachedWithTTL(expensiveTask, "150 millis")
yield* cached.pipe(Effect.andThen(Console.log))
yield* cached.pipe(Effect.andThen(Console.log))
yield* Effect.sleep("100 millis")
yield* cached.pipe(Effect.andThen(Console.log))
})
Effect.runFork(program)
// Output:
// expensive task...
// result 1
// result 1
// expensive task...
// result 2
See
cached for a similar function that caches the result
indefinitely.cachedInvalidateWithTTL for a similar function that includes an
additional effect for manually invalidating the cached value.Signature
declare const cachedWithTTL: { (timeToLive: Duration.Input): <A, E, R>(self: Effect<A, E, R>) => Effect<Effect<A, E, R>>; <A, E, R>(self: Effect<A, E, R>, timeToLive: Duration.Input): Effect<Effect<A, E, R>>; }
Since v2.0.0