Hyperlinkv0.8.0-beta.28

ScopedCache

ScopedCache.setconsteffect/ScopedCache.ts:447
<Key, A>(key: Key, value: A): <E, R>(
  self: ScopedCache<Key, A, E, R>
) => Effect.Effect<void>
<Key, A, E, R>(
  self: ScopedCache<Key, A, E, R>,
  key: Key,
  value: A
): Effect.Effect<void>

Sets a successful value for a key without running the lookup function.

When to use

Use to seed or overwrite a scoped cache entry with an already available successful value.

Details

This replaces and closes any existing entry scope for the key, applies the cache's TTL using a successful exit for the value, and may evict older entries if the cache capacity is exceeded.

combinatorsgetrefresh
export const set: {
  <Key, A>(key: Key, value: A): <E, R>(self: ScopedCache<Key, A, E, R>) => Effect.Effect<void>
  <Key, A, E, R>(self: ScopedCache<Key, A, E, R>, key: Key, value: A): Effect.Effect<void>
} = dual(
  3,
  <Key, A, E, R>(self: ScopedCache<Key, A, E, R>, key: Key, value: A): Effect.Effect<void> =>
    effect.uninterruptible(
      core.withFiber((fiber) => {
        if (self.state._tag === "Closed") {
          return effect.interrupt
        }
        const oentry = MutableHashMap.get(self.state.map, key)
        const state = self.state
        const exit = core.exitSucceed(value)
        const deferred = Deferred.makeUnsafe<A, E>()
        Deferred.doneUnsafe(deferred, exit)
        const ttl = self.timeToLive(exit, key)
        MutableHashMap.set(state.map, key, {
          scope: Scope.makeUnsafe(),
          deferred,
          expiresAt: Duration.isFinite(ttl)
            ? fiber.getRef(effect.ClockRef).currentTimeMillisUnsafe() + Duration.toMillis(ttl)
            : undefined
        })
        const check = checkCapacity(fiber, state.map, self.capacity)
        return Option.isSome(oentry)
          ? effect.flatMap(Scope.close(oentry.value.scope, effect.exitVoid), () => check)
          : check
      })
    )
)