Hyperlinkv0.8.0-beta.28

ScopedCache

ScopedCache.makeconsteffect/ScopedCache.ts:194
<
  Key,
  A,
  E = never,
  R = never,
  ServiceMode extends "lookup" | "construction" = never
>(options: {
  readonly lookup: (key: Key) => Effect.Effect<A, E, R | Scope.Scope>
  readonly capacity: number
  readonly timeToLive?: Duration.Input | undefined
  readonly requireServicesAt?: ServiceMode | undefined
}): Effect.Effect<
  ScopedCache<
    Key,
    A,
    E,
    "lookup" extends ServiceMode ? Exclude<R, Scope.Scope> : never
  >,
  never,
  ("lookup" extends ServiceMode ? never : R) | Scope.Scope
>

Creates a ScopedCache with a fixed time-to-live for every lookup result.

When to use

Use to create a scoped cache when every cached lookup result should share the same lifetime.

Details

This is the constant-TTL variant of makeWith: values are acquired by the lookup effect in per-entry scopes, capacity can evict older entries, and entry scopes are closed when entries expire, are invalidated, are evicted, or when the cache's owning scope closes.

constructorsmakeWith
export const make = <
  Key,
  A,
  E = never,
  R = never,
  ServiceMode extends "lookup" | "construction" = never
>(
  options: {
    readonly lookup: (key: Key) => Effect.Effect<A, E, R | Scope.Scope>
    readonly capacity: number
    readonly timeToLive?: Duration.Input | undefined
    readonly requireServicesAt?: ServiceMode | undefined
  }
): Effect.Effect<
  ScopedCache<Key, A, E, "lookup" extends ServiceMode ? Exclude<R, Scope.Scope> : never>,
  never,
  ("lookup" extends ServiceMode ? never : R) | Scope.Scope
> =>
  makeWith<Key, A, E, R, ServiceMode>({
    ...options,
    timeToLive: options.timeToLive ? () => options.timeToLive! : defaultTimeToLive
  })