alastairtree / LazyCache

An easy to use thread safe in-memory caching service with a simple developer friendly API for c#
https://nuget.org/packages/LazyCache
MIT License
1.71k stars 159 forks source link

Clarification on the difference between IAppCache.GetAsync and IAppCache.Get #154

Closed EnricoMassone closed 3 years ago

EnricoMassone commented 3 years ago

Just a quick question related to the IAppCacheinterface and the intended usage of its public API.

What is the difference between IAppCache.GetAsync and IAppCache.Get ? When should I use IAppCache.GetAsync instead of IAppCache.Get ?

My understanding is that IAppCache.Get must be used to read from the cache a value added to it synchronously, for instance by means of IAppCache.Add or IAppCache.GetOrAdd. On the other hand, IAppCache.GetAsync should be used to read from cache a value added asynchronously by means of IAppCache.GetOrAddAsync. Is my understanding correct ? Can you please explain the intended usage of the two APIs ?

Thanks in advance.

alastairtree commented 3 years ago

If you put the item into the cache wrapped in a task, say you used GetOrAddAsync, then you should use GetAsync as it will be more performant. If you use Get the code unwraps the task for you but needs to do some type checking and so there will be a boxing cost.

But it's usually best to use the GetOrAdd flavour rather than just the Get incase your item needs to be regenerated, and I find myself using Get or GetAsync very rarely.

EnricoMassone commented 3 years ago

If you put the item into the cache wrapped in a task, say you used GetOrAddAsync, then you should use GetAsync as it will be more performant. If you use Get the code unwraps the task for you but needs to do some type checking and so there will be a boxing cost.

But it's usually best to use the GetOrAdd flavour rather than just the Get incase your item needs to be regenerated, and I find myself using Get or GetAsync very rarely.

Thanks, now it's clearer to me.

So, if I add something to the cache by using Add<T>, fetching it later from the cache by using Get<T> is the correct thing to do, right ?

Thnaks again for the great work done on this library.

alastairtree commented 3 years ago

Yeah that's correct.

On Sun, 13 Jun 2021, 21:37 Enrico Massone, @.***> wrote:

If you put the item into the cache wrapped in a task, say you used GetOrAddAsync, then you should use GetAsync as it will be more performant. If you use Get the code unwraps the task for you but needs to do some type checking and so there will be a boxing cost.

But it's usually best to use the GetOrAdd flavour rather than just the Get incase your item needs to be regenerated, and I find myself using Get or GetAsync very rarely.

Thanks, now it's clear to me.

So, if I add something to the cache by using Add, fetching it later from the cache by using Get is the correct thing to do, right ?

Thnaks again for the great work done on this library.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/alastairtree/LazyCache/issues/154#issuecomment-860267053, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABP3TFL72XHK7NBRWRCKPR3TSUJJPANCNFSM46S2R2KQ .

EnricoMassone commented 3 years ago

Yeah that's correct. On Sun, 13 Jun 2021, 21:37 Enrico Massone, @.***> wrote: If you put the item into the cache wrapped in a task, say you used GetOrAddAsync, then you should use GetAsync as it will be more performant. If you use Get the code unwraps the task for you but needs to do some type checking and so there will be a boxing cost. But it's usually best to use the GetOrAdd flavour rather than just the Get incase your item needs to be regenerated, and I find myself using Get or GetAsync very rarely. Thanks, now it's clear to me. So, if I add something to the cache by using Add, fetching it later from the cache by using Get is the correct thing to do, right ? Thnaks again for the great work done on this library. — You are receiving this because you commented. Reply to this email directly, view it on GitHub <#154 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABP3TFL72XHK7NBRWRCKPR3TSUJJPANCNFSM46S2R2KQ .

Thanks for the answers!