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.72k stars 159 forks source link

AsyncLazy runs IO-bound code on the Thread Pool #145

Open Edminsson opened 3 years ago

Edminsson commented 3 years ago

Hi, It seems to me that AsyncLazy always runs the factory on the Thread Pool even when the factory contains IO-bound code. Perhaps I'm missing something but this does not seem very efficient. I believe Stephen Cleary has an implementation of AsyncLazy with a flag (AsyncLazyFlags.ExecuteOnCallingThread) that gives you the option to run the factory on the calling thread thread without calling Task.Factory.StartNew or Task.Run.

alastairtree commented 3 years ago

Hi, thanks for the issue. Steven Cleary's stuff is great and has been useful inspiration for LazyCache. LazyCache has always been fast enough for my needs as I only really cache things like slow database queries or api calls so this kind of micro-optimisation has not been meaningful in my use case. Now there are some benchmarks in the project, if you would like to submit a PR that shows a clear improvement I would be happy to review it?