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

About Cache availability mechanism #175

Open phadtrapong opened 2 years ago

phadtrapong commented 2 years ago

Hi guys, Does anyone know if currently this library can allow multiple threads to keep read data from cache, while some other threads update cache existing key with new value ? or multiple threads will be blocked when try to read before new value become available.

alastairtree commented 2 years ago

Sorry for the slow reply. It depends on how you add the new value to the cache. Say you have Version 1 of Item in the cache. Other threads can read Version 1. If you generate Version 2 on another thread outside of LazyCache and then cache the result with CachingService.Add then the update would be almost instant and no thread would be blocked - they just get Version 1 or Version 2 depending on the time of the read. If you use the expiration to trigger repopulation then threads that read between expiration and regeneration completion would then be blocked, more info at https://github.com/alastairtree/LazyCache/wiki/API-documentation-(v-2.x)#using-immediateexpiration-and-registerpostevictioncallback-to-refresh-a-cached-item-automatically

Hope that helps. Please close the issue if so.

Thanks

phadtrapong commented 2 years ago

Thank you for your clarification Small question about expiration trigger, It means item need to be expired first to retrigger or It will be triggered just before data will be thrown away from cache. ( As I understand cahce updated callback will be triggered before item is removed https://docs.microsoft.com/en-us/dotnet/api/system.web.caching.cacheitemupdatecallback?view=netframework-4.8 )