Skip to main content Link Search Menu Expand Document (external link)

ScopedCache overview

Added in v2.0.0


Table of contents


constructors

make

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

Signature

export declare const make: <Key, Environment, Error, Value>(options: {
  readonly lookup: Lookup<Key, Environment, Error, Value>
  readonly capacity: number
  readonly timeToLive: Duration.DurationInput
}) => Effect.Effect<ScopedCache<Key, Error, Value>, never, Scope.Scope | Environment>

Added in v2.0.0

makeWith

Constructs a new cache with the specified capacity, time to live, and lookup function, where the time to live can depend on the Exit value returned by the lookup function.

Signature

export declare const makeWith: <Key, Environment, Error, Value>(options: {
  readonly capacity: number
  readonly lookup: Lookup<Key, Environment, Error, Value>
  readonly timeToLive: (exit: Exit.Exit<Value, Error>) => Duration.DurationInput
}) => Effect.Effect<ScopedCache<Key, Error, Value>, never, Scope.Scope | Environment>

Added in v2.0.0

models

Lookup (type alias)

Similar to Cache.Lookup, but executes the lookup function within a Scope.

Signature

export type Lookup<Key, Environment, Error, Value> = (
  key: Key
) => Effect.Effect<Value, Error, Environment | Scope.Scope>

Added in v2.0.0

ScopedCache (interface)

Signature

export interface ScopedCache<in Key, out Error, out Value> extends ScopedCache.Variance<Key, Error, Value>, Pipeable {
  /**
   * Retrieves the value associated with the specified key if it exists.
   * Otherwise returns `Option.none`.
   */
  getOption(key: Key): Effect.Effect<Option.Option<Value>, Error, Scope.Scope>

  /**
   * Retrieves the value associated with the specified key if it exists and the
   * lookup function has completed. Otherwise returns `Option.none`.
   */
  getOptionComplete(key: Key): Effect.Effect<Option.Option<Value>, never, Scope.Scope>

  /**
   * Returns statistics for this cache.
   */
  readonly cacheStats: Effect.Effect<Cache.CacheStats>

  /**
   * Return whether a resource associated with the specified key exists in the
   * cache. Sometime `contains` can return true if the resource is currently
   * being created but not yet totally created.
   */
  contains(key: Key): Effect.Effect<boolean>

  /**
   * Return statistics for the specified entry.
   */
  entryStats(key: Key): Effect.Effect<Option.Option<Cache.EntryStats>>

  /**
   * Gets the value from the cache if it exists or otherwise computes it, the
   * release action signals to the cache that the value is no longer being used
   * and can potentially be finalized subject to the policies of the cache.
   */
  get(key: Key): Effect.Effect<Value, Error, Scope.Scope>

  /**
   * Invalidates the resource associated with the specified key.
   */
  invalidate(key: Key): Effect.Effect<void>

  /**
   * Invalidates all values in the cache.
   */
  readonly invalidateAll: Effect.Effect<void>

  /**
   * Force the reuse of the lookup function to compute the returned scoped
   * effect associated with the specified key immediately. Once the new resource
   * is recomputed, the old resource associated to the key is cleaned (once all
   * fiber using it are done with it). During the time the new resource is
   * computed, concurrent call the .get will use the old resource if this one is
   * not expired.
   */
  refresh(key: Key): Effect.Effect<void, Error>

  /**
   * Returns the approximate number of values in the cache.
   */
  readonly size: Effect.Effect<number>
}

Added in v2.0.0

symbols

ScopedCacheTypeId

Signature

export declare const ScopedCacheTypeId: typeof ScopedCacheTypeId

Added in v2.0.0

ScopedCacheTypeId (type alias)

Signature

export type ScopedCacheTypeId = typeof ScopedCacheTypeId

Added in v2.0.0

utils

ScopedCache (namespace)

Added in v2.0.0

Variance (interface)

Signature

export interface Variance<in Key, out Error, out Value> {
  readonly [ScopedCacheTypeId]: {
    _Key: Types.Contravariant<Key>
    _Error: Types.Covariant<Error>
    _Value: Types.Covariant<Value>
  }
}

Added in v2.0.0