Skip to content
Effect 4.0 is here. One ecosystem. Zero dependencies.

Cache

14 exports Since v2.0.0 Source

Constructors

Since v2.0.0

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
Since v2.0.0

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
Since v2.0.0

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
Since v2.0.0

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
Since v2.0.0

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
Since v2.0.0

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

Signature

interface EntryStats {
readonly loadedMillis: number;
}
Since v2.0.0

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

Since v2.0.0

Symbols

Signature

declare const CacheTypeId: unique symbol
Since v2.0.0

Signature

type CacheTypeId = typeof CacheTypeId

Signature

declare const ConsumerCacheTypeId: unique symbol

Signature

type ConsumerCacheTypeId = typeof ConsumerCacheTypeId