Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Add getCached function to Cache module #443

Closed
evelant opened this issue May 30, 2023 · 7 comments
Closed

Add getCached function to Cache module #443

evelant opened this issue May 30, 2023 · 7 comments

Comments

@evelant
Copy link

evelant commented May 30, 2023

@IMax153

It would be useful to have a utility function on Cache for "get cached value if it exists in the cache, otherwise none"

Pseudocode for workaround to do this:

const getCached = <T>(someCache, key): Effect<never, never, Option<T>> =>
 pipe(
   someCache.contains(key),
   T.ifEffect(
     //remove E since we know it's cached, the lookup fn won't get called so E is impossible
     pipe(cache.get(key), T.map(O.some), T.catchTags(...)), 
     T.succeedNone()
   )
)
@IMax153
Copy link
Member

IMax153 commented May 31, 2023

Closing as completed by #445

@IMax153 IMax153 closed this as completed May 31, 2023
@evelant
Copy link
Author

evelant commented Jun 8, 2023

@IMax153 Why does the signature for getOption include Error? Since it will never hit the resolver function shouldn't it be Effect<never, never, Option.Option<Value>> instead?

 /**
     * Retrieves the value associated with the specified key if it exists.
     * Otherwise returns `Option.none`.
     */
    getOption(key: Key): Effect.Effect<never, Error, Option.Option<Value>>;

@IMax153
Copy link
Member

IMax153 commented Jun 8, 2023

cc @tim-smart

@IMax153 IMax153 reopened this Jun 8, 2023
@tim-smart
Copy link
Member

The error is there because a cache entry can still be in a pending state, in the case where the lookup effect is async.

If the cache entry is in a pending state, then it will await the underlying Deferred and pass any errors back to the caller.

@evelant
Copy link
Author

evelant commented Jun 9, 2023

@tim-smart OK makes sense. Perhaps there's a need for another function then. A sort of getImmediateOption that will run sync without any Error only returning the entry if it's actually already cached, ignoring any pending.

I think that may be the more useful behavior. The current getOption is basically just a shorthand for the following.

pipe(
   someCache.contains(key),
   T.ifEffect(
     pipe(cache.get(key), T.map(O.some)), 
     T.succeedNone()
   )

AFAIK there's no way to do a getImmediateOption with the current Cache api. I think it would be useful for situations where you want to take different actions based on whether a value is immediately available because a pending cache request could take an arbitrary amount of time to complete.

@tim-smart
Copy link
Member

tim-smart commented Jun 9, 2023

Yes I don't see an issue with another API for getting a completed cache value.

readonly getOptionComplete: (key: Key) => Effect<never, never, Option<Value>>

@IMax153
Copy link
Member

IMax153 commented Jun 20, 2023

Closing as completed by #448

@IMax153 IMax153 closed this as completed Jun 20, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants