<Key, A>(key: Key): <E, R>(
self: Cache<Key, A, E, R>
) => Effect.Effect<void>
<Key, A, E, R>(self: Cache<Key, A, E, R>, key: Key): Effect.Effect<void>Invalidates the entry associated with the specified key in the cache.
Example (Invalidating cached entries)
import { Cache, Effect } from "effect"
const program = Effect.gen(function*() {
const cache = yield* Cache.make({
capacity: 10,
lookup: (key: string) => Effect.succeed(key.length)
})
// Add a value to the cache
yield* Cache.get(cache, "hello")
console.log(yield* Cache.has(cache, "hello")) // true
// Invalidate the entry
yield* Cache.invalidate(cache, "hello")
console.log(yield* Cache.has(cache, "hello")) // false
// Invalidating non-existent keys doesn't error
yield* Cache.invalidate(cache, "nonexistent")
// Get after invalidation will invoke lookup again
let lookupCount = 0
const cache2 = yield* Cache.make({
capacity: 10,
lookup: (key: string) =>
Effect.sync(() => {
lookupCount++
return key.length
})
})
yield* Cache.get(cache2, "test") // lookupCount = 1
yield* Cache.invalidate(cache2, "test")
yield* Cache.get(cache2, "test") // lookupCount = 2 (lookup called again)
})export const const invalidate: {
<Key, A>(key: Key): <E, R>(
self: Cache<Key, A, E, R>
) => Effect.Effect<void>
<Key, A, E, R>(
self: Cache<Key, A, E, R>,
key: Key
): Effect.Effect<void>
}
Invalidates the entry associated with the specified key in the cache.
Example (Invalidating cached entries)
import { Cache, Effect } from "effect"
const program = Effect.gen(function*() {
const cache = yield* Cache.make({
capacity: 10,
lookup: (key: string) => Effect.succeed(key.length)
})
// Add a value to the cache
yield* Cache.get(cache, "hello")
console.log(yield* Cache.has(cache, "hello")) // true
// Invalidate the entry
yield* Cache.invalidate(cache, "hello")
console.log(yield* Cache.has(cache, "hello")) // false
// Invalidating non-existent keys doesn't error
yield* Cache.invalidate(cache, "nonexistent")
// Get after invalidation will invoke lookup again
let lookupCount = 0
const cache2 = yield* Cache.make({
capacity: 10,
lookup: (key: string) =>
Effect.sync(() => {
lookupCount++
return key.length
})
})
yield* Cache.get(cache2, "test") // lookupCount = 1
yield* Cache.invalidate(cache2, "test")
yield* Cache.get(cache2, "test") // lookupCount = 2 (lookup called again)
})
invalidate: {
<function (type parameter) Key in <Key, A>(key: Key): <E, R>(self: Cache<Key, A, E, R>) => Effect.Effect<void>Key, function (type parameter) A in <Key, A>(key: Key): <E, R>(self: Cache<Key, A, E, R>) => Effect.Effect<void>A>(key: Keykey: function (type parameter) Key in <Key, A>(key: Key): <E, R>(self: Cache<Key, A, E, R>) => Effect.Effect<void>Key): <function (type parameter) E in <E, R>(self: Cache<Key, A, E, R>): Effect.Effect<void>E, function (type parameter) R in <E, R>(self: Cache<Key, A, E, R>): Effect.Effect<void>R>(self: Cache<Key, A, E, R>(parameter) self: {
map: MutableHashMap.MutableHashMap<Key, Entry<A, E>>;
capacity: number;
lookup: (key: Key) => Effect.Effect<A, E, R>;
timeToLive: (exit: Exit.Exit<A, E>, key: Key) => Duration.Duration;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
self: interface Cache<in out Key, in out A, in out E = never, out R = never>A cache interface that provides a mutable key-value store with automatic TTL management,
capacity limits, and lookup functions for cache misses.
Example (Creating a basic cache)
import { Cache, Effect } from "effect"
// Basic cache with string keys and number values
const program = Effect.gen(function*() {
const cache = yield* Cache.make<string, number>({
capacity: 100,
lookup: (key: string) => Effect.succeed(key.length)
})
// Cache operations
const value1 = yield* Cache.get(cache, "hello") // 5
const value2 = yield* Cache.get(cache, "world") // 5
const value3 = yield* Cache.get(cache, "hello") // 5 (cached)
return [value1, value2, value3]
})
Example (Handling lookup failures)
import { Cache, Effect } from "effect"
// Cache with error handling
const program = Effect.gen(function*() {
const cache = yield* Cache.make<string, number, string>({
capacity: 10,
lookup: (key: string) =>
key === "error"
? Effect.fail("Lookup failed")
: Effect.succeed(key.length)
})
// Handle successful and failed lookups
const success = yield* Cache.get(cache, "test") // 4
const failure = yield* Effect.exit(Cache.get(cache, "error")) // Exit.fail
return { success, failure }
})
Example (Using complex keys with TTL)
import { Cache, Data, Duration, Effect } from "effect"
// Cache with complex key types and TTL
class UserId extends Data.Class<{ id: number }> {}
const program = Effect.gen(function*() {
const userCache = yield* Cache.make<UserId, string>({
capacity: 1000,
lookup: (userId: UserId) => Effect.succeed(`User-${userId.id}`),
timeToLive: Duration.minutes(5)
})
const userId = new UserId({ id: 123 })
const userName = yield* Cache.get(userCache, userId)
return userName // "User-123"
})
Cache<function (type parameter) Key in <Key, A>(key: Key): <E, R>(self: Cache<Key, A, E, R>) => Effect.Effect<void>Key, function (type parameter) A in <Key, A>(key: Key): <E, R>(self: Cache<Key, A, E, R>) => Effect.Effect<void>A, function (type parameter) E in <E, R>(self: Cache<Key, A, E, R>): Effect.Effect<void>E, function (type parameter) R in <E, R>(self: Cache<Key, A, E, R>): Effect.Effect<void>R>) => import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<void>
<function (type parameter) Key in <Key, A, E, R>(self: Cache<Key, A, E, R>, key: Key): Effect.Effect<void>Key, function (type parameter) A in <Key, A, E, R>(self: Cache<Key, A, E, R>, key: Key): Effect.Effect<void>A, function (type parameter) E in <Key, A, E, R>(self: Cache<Key, A, E, R>, key: Key): Effect.Effect<void>E, function (type parameter) R in <Key, A, E, R>(self: Cache<Key, A, E, R>, key: Key): Effect.Effect<void>R>(self: Cache<Key, A, E, R>(parameter) self: {
map: MutableHashMap.MutableHashMap<Key, Entry<A, E>>;
capacity: number;
lookup: (key: Key) => Effect.Effect<A, E, R>;
timeToLive: (exit: Exit.Exit<A, E>, key: Key) => Duration.Duration;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
self: interface Cache<in out Key, in out A, in out E = never, out R = never>A cache interface that provides a mutable key-value store with automatic TTL management,
capacity limits, and lookup functions for cache misses.
Example (Creating a basic cache)
import { Cache, Effect } from "effect"
// Basic cache with string keys and number values
const program = Effect.gen(function*() {
const cache = yield* Cache.make<string, number>({
capacity: 100,
lookup: (key: string) => Effect.succeed(key.length)
})
// Cache operations
const value1 = yield* Cache.get(cache, "hello") // 5
const value2 = yield* Cache.get(cache, "world") // 5
const value3 = yield* Cache.get(cache, "hello") // 5 (cached)
return [value1, value2, value3]
})
Example (Handling lookup failures)
import { Cache, Effect } from "effect"
// Cache with error handling
const program = Effect.gen(function*() {
const cache = yield* Cache.make<string, number, string>({
capacity: 10,
lookup: (key: string) =>
key === "error"
? Effect.fail("Lookup failed")
: Effect.succeed(key.length)
})
// Handle successful and failed lookups
const success = yield* Cache.get(cache, "test") // 4
const failure = yield* Effect.exit(Cache.get(cache, "error")) // Exit.fail
return { success, failure }
})
Example (Using complex keys with TTL)
import { Cache, Data, Duration, Effect } from "effect"
// Cache with complex key types and TTL
class UserId extends Data.Class<{ id: number }> {}
const program = Effect.gen(function*() {
const userCache = yield* Cache.make<UserId, string>({
capacity: 1000,
lookup: (userId: UserId) => Effect.succeed(`User-${userId.id}`),
timeToLive: Duration.minutes(5)
})
const userId = new UserId({ id: 123 })
const userName = yield* Cache.get(userCache, userId)
return userName // "User-123"
})
Cache<function (type parameter) Key in <Key, A, E, R>(self: Cache<Key, A, E, R>, key: Key): Effect.Effect<void>Key, function (type parameter) A in <Key, A, E, R>(self: Cache<Key, A, E, R>, key: Key): Effect.Effect<void>A, function (type parameter) E in <Key, A, E, R>(self: Cache<Key, A, E, R>, key: Key): Effect.Effect<void>E, function (type parameter) R in <Key, A, E, R>(self: Cache<Key, A, E, R>, key: Key): Effect.Effect<void>R>, key: Keykey: function (type parameter) Key in <Key, A, E, R>(self: Cache<Key, A, E, R>, key: Key): Effect.Effect<void>Key): import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<void>
} = dual<(...args: Array<any>) => any, <Key, A, E, R>(self: Cache<Key, A, E, R>, key: Key) => Effect.Effect<void>>(arity: 2, body: <Key, A, E, R>(self: Cache<Key, A, E, R>, key: Key) => Effect.Effect<void>): ((...args: Array<any>) => any) & (<Key, A, E, R>(self: Cache<Key, A, E, R>, key: Key) => Effect.Effect<void>) (+1 overload)Creates a function that can be called in data-first style or data-last
(pipe-friendly) style.
When to use
Use to expose one implementation through both direct and pipe-friendly
call styles.
Details
Pass either the arity of the uncurried function or a predicate that decides
whether the current call is data-first. Arity is the common case. Use a
predicate when optional arguments make arity ambiguous.
Example (Selecting data-first or data-last style by arity)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(2, (self, that) => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Defining overloads with call signatures)
import { Function, pipe } from "effect"
const sum: {
(that: number): (self: number) => number
(self: number, that: number): number
} = Function.dual(2, (self: number, that: number): number => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Selecting data-first or data-last style with a predicate)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(
(args) => args.length === 2,
(self, that) => self + that
)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
dual(2, <function (type parameter) Key in <Key, A, E, R>(self: Cache<Key, A, E, R>, key: Key): Effect.Effect<void>Key, function (type parameter) A in <Key, A, E, R>(self: Cache<Key, A, E, R>, key: Key): Effect.Effect<void>A, function (type parameter) E in <Key, A, E, R>(self: Cache<Key, A, E, R>, key: Key): Effect.Effect<void>E, function (type parameter) R in <Key, A, E, R>(self: Cache<Key, A, E, R>, key: Key): Effect.Effect<void>R>(self: Cache<Key, A, E, R>(parameter) self: {
map: MutableHashMap.MutableHashMap<Key, Entry<A, E>>;
capacity: number;
lookup: (key: Key) => Effect.Effect<A, E, R>;
timeToLive: (exit: Exit.Exit<A, E>, key: Key) => Duration.Duration;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
self: interface Cache<in out Key, in out A, in out E = never, out R = never>A cache interface that provides a mutable key-value store with automatic TTL management,
capacity limits, and lookup functions for cache misses.
Example (Creating a basic cache)
import { Cache, Effect } from "effect"
// Basic cache with string keys and number values
const program = Effect.gen(function*() {
const cache = yield* Cache.make<string, number>({
capacity: 100,
lookup: (key: string) => Effect.succeed(key.length)
})
// Cache operations
const value1 = yield* Cache.get(cache, "hello") // 5
const value2 = yield* Cache.get(cache, "world") // 5
const value3 = yield* Cache.get(cache, "hello") // 5 (cached)
return [value1, value2, value3]
})
Example (Handling lookup failures)
import { Cache, Effect } from "effect"
// Cache with error handling
const program = Effect.gen(function*() {
const cache = yield* Cache.make<string, number, string>({
capacity: 10,
lookup: (key: string) =>
key === "error"
? Effect.fail("Lookup failed")
: Effect.succeed(key.length)
})
// Handle successful and failed lookups
const success = yield* Cache.get(cache, "test") // 4
const failure = yield* Effect.exit(Cache.get(cache, "error")) // Exit.fail
return { success, failure }
})
Example (Using complex keys with TTL)
import { Cache, Data, Duration, Effect } from "effect"
// Cache with complex key types and TTL
class UserId extends Data.Class<{ id: number }> {}
const program = Effect.gen(function*() {
const userCache = yield* Cache.make<UserId, string>({
capacity: 1000,
lookup: (userId: UserId) => Effect.succeed(`User-${userId.id}`),
timeToLive: Duration.minutes(5)
})
const userId = new UserId({ id: 123 })
const userName = yield* Cache.get(userCache, userId)
return userName // "User-123"
})
Cache<function (type parameter) Key in <Key, A, E, R>(self: Cache<Key, A, E, R>, key: Key): Effect.Effect<void>Key, function (type parameter) A in <Key, A, E, R>(self: Cache<Key, A, E, R>, key: Key): Effect.Effect<void>A, function (type parameter) E in <Key, A, E, R>(self: Cache<Key, A, E, R>, key: Key): Effect.Effect<void>E, function (type parameter) R in <Key, A, E, R>(self: Cache<Key, A, E, R>, key: Key): Effect.Effect<void>R>, key: Keykey: function (type parameter) Key in <Key, A, E, R>(self: Cache<Key, A, E, R>, key: Key): Effect.Effect<void>Key): import EffectEffect.interface Effect<out A, out E = never, out R = never>The Effect interface defines a value that lazily describes a workflow or
job. The workflow requires some context R, and may fail with an error of
type E, or succeed with a value of type A.
When to use
Use when you need to represent a lazy, composable workflow that can require
services, fail with a typed error, or succeed with a typed value.
Details
Effect values model resourceful interaction with the outside world,
including synchronous, asynchronous, concurrent, and parallel interaction.
They use a fiber-based concurrency model, with built-in support for
scheduling, fine-grained interruption, structured concurrency, and high
scalability.
To run an Effect value, you need a Runtime, which is a type that is
capable of executing Effect values.
Effect<void> =>
import effecteffect.const sync: <A>(
thunk: LazyArg<A>
) => Effect.Effect<A>
sync(() => {
import MutableHashMapMutableHashMap.const remove: {
<K>(key: K): <V>(
self: MutableHashMap<K, V>
) => MutableHashMap<K, V>
<K, V>(
self: MutableHashMap<K, V>,
key: K
): MutableHashMap<K, V>
}
remove(self: Cache<Key, A, E, R>(parameter) self: {
map: MutableHashMap.MutableHashMap<Key, Entry<A, E>>;
capacity: number;
lookup: (key: Key) => Effect.Effect<A, E, R>;
timeToLive: (exit: Exit.Exit<A, E>, key: Key) => Duration.Duration;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
}
self.Cache<Key, A, E, R>.map: MutableHashMap.MutableHashMap<Key, Entry<A, E>>(property) Cache<Key, A, E, R>.map: {
backing: Map<K, V>;
buckets: Map<number, NonEmptyArray<K>>;
pipe: { <A>(this: A): A; <A, B = never>(this: A, ab: (_: A) => B): B; <A, B = never, C = never>(this: A, ab: (_: A) => B, bc: (_: B) => C): C; <A, B = never, C = never, D = never>(this: A, ab: (_: A) => B, bc: (_: B) => C, cd: (_: C) => D): D; <…;
toString: () => string;
toJSON: () => unknown;
}
map, key: Keykey)
}))