Hyperlinkv0.8.0-beta.28

ScopedCache

ScopedCache.entriesconsteffect/ScopedCache.ts:799
<Key, A, E, R>(self: ScopedCache<Key, A, E, R>): Effect.Effect<
  Array<[Key, A]>
>

Retrieves all key-value pairs from the cache as an array. This function only returns entries with successfully resolved values, filtering out any failed lookups or expired entries.

When to use

Use to inspect the currently successful cached key-value pairs without running cache lookups.

Gotchas

Expired entries are removed and their scopes are closed while filtering.

combinatorskeysvalues
export const entries = <Key, A, E, R>(self: ScopedCache<Key, A, E, R>): Effect.Effect<Array<[Key, A]>> =>
  core.withFiber((fiber) => {
    if (self.state._tag === "Closed") return effect.succeed([])
    const state = self.state
    const now = fiber.getRef(effect.ClockRef).currentTimeMillisUnsafe()
    const fibers = Arr.empty<Fiber.Fiber<unknown, unknown>>()
    const arr: Array<[Key, A]> = []
    for (const [key, entry] of state.map) {
      if (entry.expiresAt === undefined || entry.expiresAt > now) {
        const exit = entry.deferred.effect
        if (core.isExit(exit) && !effect.exitIsFailure(exit)) {
          arr.push([key, exit.value as A])
        }
      } else {
        MutableHashMap.remove(state.map, key)
        fibers.push(effect.forkUnsafe(fiber, Scope.close(entry.scope, effect.exitVoid), true, true))
      }
    }
    return fibers.length === 0
      ? effect.succeed(arr)
      : effect.as(effect.fiberAwaitAll(fibers), arr)
  })
Referenced by 1 symbols