alastairtree/LazyCache

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

EnricoMassone opened this issue · 4 comments

Just a quick question related to the IAppCache interface 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.

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.

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.

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!