/MemoryCacheLabs

Cache in-memory in ASP.NET Core | tests | immutable

Primary LanguageC#MIT LicenseMIT

Memory Cache Lab

Buy Me a Coffee at ko-fi.com

Medium Article: IMemoryCache: Immutable Collections and Unit Tests

There are many introductory articles talking about using the IMemoryCache to store data in the memory of the webserver. However, few of them have mentioned how to ensure the consistency of cached values at runtime. In other words, if not designed carefully, the cached values could be mutated accidentally in code.

In this article, I will first present an example cache service that allows us to mutate a cached entry, which is an undesired outcome in most cases. To improve the implementation, next, I will show you a way to create immutable collections as cache objects by using IReadOnlyList<T> and record.

immutable-cache

License

Feel free to use the code in this repository as it is under MIT license.

Buy Me a Coffee at ko-fi.com