Hyperlinkv0.8.0-beta.28

Cache

Cache.keysconsteffect/Cache.ts:1221
<Key, A, E, R>(self: Cache<Key, A, E, R>): Effect.Effect<Iterable<Key>>

Retrieves all active keys from the cache, automatically filtering out expired entries.

Example (Reading active keys)

import { Cache, Effect } from "effect"

// Basic key enumeration
const program = Effect.gen(function*() {
  const cache = yield* Cache.make({
    capacity: 10,
    lookup: (key: string) => Effect.succeed(key.length)
  })

  // Add some entries to the cache
  yield* Cache.get(cache, "hello")
  yield* Cache.get(cache, "world")
  yield* Cache.get(cache, "cache")

  // Retrieve all active keys
  const keys = yield* Cache.keys(cache)

  console.log(Array.from(keys).sort()) // ["cache", "hello", "world"]
})
combinators
Source effect/Cache.ts:122111 lines
export const keys = <Key, A, E, R>(self: Cache<Key, A, E, R>): Effect.Effect<Iterable<Key>> =>
  core.withFiber((fiber) => {
    const now = fiber.getRef(effect.ClockRef).currentTimeMillisUnsafe()
    return effect.succeed(Iterable.filterMap(self.map, ([key, entry]) => {
      if (entry.expiresAt === undefined || entry.expiresAt > now) {
        return Result.succeed(key)
      }
      MutableHashMap.remove(self.map, key)
      return Result.failVoid
    }))
  })