Hyperlinkv0.8.0-beta.28

ScopedCache

ScopedCache.getOptionconsteffect/ScopedCache.ts:343
<Key, A>(key: Key): <E, R>(
  self: ScopedCache<Key, A, E, R>
) => Effect.Effect<Option.Option<A>, E>
<Key, A, E, R>(self: ScopedCache<Key, A, E, R>, key: Key): Effect.Effect<
  Option.Option<A>,
  E
>

Reads an existing unexpired cache entry without running the lookup function.

When to use

Use to read a scoped value only when it is already cached, without starting the lookup for missing or expired keys.

Details

Returns Option.none when the key is absent or expired. If an entry exists, the effect waits for its cached result and returns Option.some(value) on success, or fails with the cached lookup error.

combinatorsgetgetSuccess
export const getOption: {
  <Key, A>(key: Key): <E, R>(self: ScopedCache<Key, A, E, R>) => Effect.Effect<Option.Option<A>, E>
  <Key, A, E, R>(self: ScopedCache<Key, A, E, R>, key: Key): Effect.Effect<Option.Option<A>, E>
} = dual(
  2,
  <Key, A, E, R>(self: ScopedCache<Key, A, E, R>, key: Key): Effect.Effect<Option.Option<A>, E> =>
    effect.uninterruptibleMask((restore) =>
      core.withFiber((fiber) =>
        effect.flatMap(
          getImpl(self, key, fiber),
          (entry) => entry ? effect.asSome(restore(Deferred.await(entry.deferred))) : effect.succeedNone
        )
      )
    )
)