Hyperlinkv0.8.0-beta.28

Cache

Cache.invalidateconsteffect/Cache.ts:882
<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)
})
combinators
Source effect/Cache.ts:8827 lines
export 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>
} = dual(2, <Key, A, E, R>(self: Cache<Key, A, E, R>, key: Key): Effect.Effect<void> =>
  effect.sync(() => {
    MutableHashMap.remove(self.map, key)
  }))