Hyperlinkv0.8.0-beta.28

ScopedCache

ScopedCache.refreshconsteffect/ScopedCache.ts:622
<Key, A>(key: Key): <E, R>(
  self: ScopedCache<Key, A, E, R>
) => Effect.Effect<A, E, R>
<Key, A, E, R>(self: ScopedCache<Key, A, E, R>, key: Key): Effect.Effect<
  A,
  E,
  R
>

Forces a refresh of the value associated with the specified key in the cache.

When to use

Use to recompute a scoped cache entry immediately, even when an unexpired value is already cached.

Details

It will always invoke the lookup function to construct a new value, overwriting any existing value for that key.

combinatorsgetinvalidate
export const refresh: {
  <Key, A>(key: Key): <E, R>(self: ScopedCache<Key, A, E, R>) => Effect.Effect<A, E, R>
  <Key, A, E, R>(self: ScopedCache<Key, A, E, R>, key: Key): Effect.Effect<A, E, R>
} = dual(
  2,
  <Key, A, E, R>(self: ScopedCache<Key, A, E, R>, key: Key): Effect.Effect<A, E, R> =>
    effect.uninterruptibleMask(effect.fnUntraced(function*(restore) {
      if (self.state._tag === "Closed") return yield* effect.interrupt
      const fiber = Fiber.getCurrent()!
      const scope = Scope.makeUnsafe()
      const deferred = Deferred.makeUnsafe<A, E>()
      const entry: Entry<A, E> = {
        scope,
        expiresAt: undefined,
        deferred
      }
      const newEntry = !MutableHashMap.has(self.state.map, key)
      if (newEntry) {
        MutableHashMap.set(self.state.map, key, entry)
        yield* checkCapacity(fiber, self.state.map, self.capacity)
      }
      const exit = yield* effect.exit(restore(Scope.provide(self.lookup(key), scope)))
      Deferred.doneUnsafe(deferred, exit)
      // @ts-ignore async gap
      if (self.state._tag === "Closed") {
        if (!newEntry) {
          yield* Scope.close(scope, effect.exitVoid)
        }
        return yield* effect.interrupt
      }
      const ttl = self.timeToLive(exit, key)
      entry.expiresAt = Duration.isFinite(ttl)
        ? fiber.getRef(effect.ClockRef).currentTimeMillisUnsafe() + Duration.toMillis(ttl)
        : undefined
      if (!newEntry) {
        const oentry = MutableHashMap.get(self.state.map, key)
        MutableHashMap.set(self.state.map, key, entry)
        if (Option.isSome(oentry)) {
          yield* Scope.close(oentry.value.scope, effect.exitVoid)
        }
      }
      return yield* exit
    }))
)