Skip to content

Cache

14 exports Added in v2.0.0 Source

Constructors

make

Added in v2.0.0 Source

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

Signature

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

Constructs a new CacheStats from the specified values.

Signature

declare const makeCacheStats: (options: {
  readonly hits: number;
  readonly misses: number;
  readonly size: number;
}) => CacheStats;

Constructs a new EntryStats from the specified values.

Signature

declare const makeEntryStats: (loadedMillis: number) => EntryStats;

makeWith

Added in v2.0.0 Source

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

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

Models

Cache interface

Added in v2.0.0 Source

A Cache is defined in terms of a lookup function that, given a key of type Key, can either fail with an error of type Error or succeed with a value of type Value. Getting a value from the cache will either return the previous result of the lookup function if it is available or else compute a new result with the lookup function, put it in the cache, and return it.

A cache also has a specified capacity and time to live. When the cache is at capacity the least recently accessed values in the cache will be removed to make room for new values. Getting a value with a life older than the specified time to live will result in a new value being computed with the lookup function and returned when available.

The cache is safe for concurrent access. If multiple fibers attempt to get the same key the lookup function will only be computed once and the result will be returned to all fibers.

Signature

interface Cache<in out Key, in out Value, out Error = never>
  extends ConsumerCache<Key, Value, Error>, Variance<Key, Value, Error> {
  get(key: Key): Effect<Value, Error>;
  getEither(key: Key): Effect<Either<Value, Value>, Error>;
  refresh(key: Key): Effect<void, Error>;
  set(key: Key, value: Value): Effect<void>;
}

CacheStats interface

Added in v2.0.0 Source

CacheStats represents a snapshot of statistics for the cache as of a point in time.

Signature

interface CacheStats {
  readonly hits: number;
  readonly misses: number;
  readonly size: number;
}

ConsumerCache interface

Added in v2.0.0 Source

A ConsumerCache models a portion of a cache which is safe to share without allowing to create new values or access existing ones.

It can be used safely to give over control for request management without leaking writer side details.

Signature

interface ConsumerCache<in out Key, out Value, out Error = never> extends ConsumerVariance<
  Key,
  Value,
  Error
> {
  readonly cacheStats: Effect<CacheStats>;
  readonly entries: Effect<Array<[Key, Value]>>;
  readonly invalidateAll: Effect<void>;
  readonly keys: Effect<Array<Key>>;
  readonly size: Effect<number>;
  readonly values: Effect<Array<Value>>;
  contains(key: Key): Effect<boolean>;
  entryStats(key: Key): Effect<Option<EntryStats>>;
  getOption(key: Key): Effect<Option<Value>, Error>;
  getOptionComplete(key: Key): Effect<Option<Value>>;
  invalidate(key: Key): Effect<void>;
  invalidateWhen(key: Key, predicate: Predicate<Value>): Effect<void>;
}

EntryStats interface

Added in v2.0.0 Source

Represents a snapshot of statistics for an entry in the cache.

Signature

interface EntryStats {
  readonly loadedMillis: number;
}

Lookup type

Added in v2.0.0 Source

A Lookup represents a lookup function that, given a key of type Key, can return an effect that will either produce a value of type Value or fail with an error of type Error using an environment of type Environment.

Signature

type Lookup<Key, Value, Error = never, Environment = never> = (
  key: Key,
) => Effect.Effect<Value, Error, Environment>;

Other

Cache

Added in v2.0.0 Source

Symbols

CacheTypeId

Added in v2.0.0 Source

Signature

declare const CacheTypeId: unique symbol;

CacheTypeId type

Added in v2.0.0 Source

Signature

type CacheTypeId = typeof CacheTypeId;

Signature

declare const ConsumerCacheTypeId: unique symbol;

ConsumerCacheTypeId type

Added in v3.6.4 Source

Signature

type ConsumerCacheTypeId = typeof ConsumerCacheTypeId;