Hyperlinkv0.8.0-beta.28

ScopedCache

ScopedCache.keysconsteffect/ScopedCache.ts:739
<Key, A, E, R>(self: ScopedCache<Key, A, E, R>): Effect.Effect<Array<Key>>

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

When to use

Use to inspect currently cached unexpired keys without running cache lookups.

Gotchas

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

combinatorsentriesvalues
export const keys = <Key, A, E, R>(self: ScopedCache<Key, A, E, R>): Effect.Effect<Array<Key>> =>
  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 keys: Array<Key> = []
    for (const [key, entry] of state.map) {
      if (entry.expiresAt === undefined || entry.expiresAt > now) {
        keys.push(key)
      } 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(keys) : effect.as(effect.fiberAwaitAll(fibers), keys)
  })