An open API service indexing awesome lists of open source software.

https://github.com/dotnet-labs/memorycachelabs

Cache in-memory in ASP.NET Core | tests | immutable
https://github.com/dotnet-labs/memorycachelabs

cache dotnet dotnet-core dotnetcore immutable memory-cache unit-testing unitest

Last synced: 20 days ago
JSON representation

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

Awesome Lists containing this project

README

          

# Memory Cache Lab

Buy Me a Coffee at ko-fi.com

## [Medium Article: IMemoryCache: Immutable Collections and Unit Tests](https://codeburst.io/imemorycache-immutable-collections-and-unit-tests-cfac7b389a5)

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` and `record`.

![immutable-cache](immutable-cache.png)

## License

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

Buy Me a Coffee at ko-fi.com