LazyCache is a library that makes it easy for developers to add in-memory caching to dotnet apps. Version 2 is a major rewrite to change from the .Net Framework 4.5 to Netstandard 2.0, and is now available from nuget.org/packages/LazyCache.
Getting started
Install from nuget:
dotnet add package LazyCache --version 2.0.0
And then cache object that are slow or expensive to produce:
IAppCache cache = new CachingService();
Func<ComplexObjects> complexObjectFactory = () => methodThatTakesTimeOrResources();
ComplexObjects cachedResults = cache.GetOrAdd("uniqueKey", complexObjectFactory);
If you have used verion 0.7 before, that should look very familiar.
What’s new in version 2.0?
The main change is that we now target netstandard 2, which means the library now works on both framework 4.6.1+ and dotnet core 2.0+. To do this LazyCache no longer builds on top of on System.Runtime.Caching, and instead we now use the much more modern Microsoft.Extensions.Caching.Memory, which is a full rewrite of Microsofts caching infrastructure. However, I have tried as much as possible to maintain backwards compatiblity. See below for upgrade instructions.
We also now have packages that support two of the most popular dependency injection frameworks:
- The default aspnet core DI framework Microsoft.Extensions.DependencyInjection is supported by LazyCache.AspNetCore
- Ninject is supported by LazyCache.Ninject
How do I upgrade?
Because it is a major rewrite you cannot just upgrade the package - depending on your use you may need to tweak a few bits of code as you may have some compiler errors. As far as possible however, I have maintained the existing API.
For deatiled instructions have a look at the upgrade guide.
What took so long?
This has been in “beta” (pre-release on nuget) for many months - sorry to anyone inconvenienced by that. This is a side project for me, and so sometimes other stuff takes priority. But that is not to say others are not welcome to look at the Github issues and send me a Pull Request. Help is always welcome :-)
What happened to version 1.0?
LazyCache has been stable at version 0.7 for a long time, and really it was pretty much done. It felt like version 1.0, even if I never got round to incrementing the counter. This latest work was a large breaking change from 0.7, and alighed with dotnet core 2+ and netstandard 2, so version 2.0 just felt right.
Where can I get more information?
Have a look at