This project demonstrates how to implement efficient in-memory caching using IMemoryCache in .NET applications. Originally built for .NET Core and progressively upgraded to .NET 9.0, this implementation showcases modern caching patterns specifically designed for monolithic environments.
For distributed caching approaches, check out my other project using Redis.
I have also blogged with a full explanation of the core concepts.
This project has evolved through several .NET versions:
- Started with .NET Core 3.1
- Updated to .NET 6 with minimal hosting model
- Recently upgraded to .NET 9.0 with modern C# features
- Modern C# Language Features: File-scoped namespaces, nullable reference types, and target-typed new expressions
- Improved Architecture: Interface-based design following SOLID principles
- Enhanced HTTP Implementation: Using modern patterns with
GetFromJsonAsync<T>()
and improved error handling - Thread Safety: Synchronized cache access using SemaphoreSlim
- Comprehensive Test Suite: Complete test coverage with xUnit and Moq
- Decorator Pattern: Properly implemented DI registration for the caching service
- User A makes a request to our web service
- In-memory cache doesn't have a value in place, it enters into lock state and makes a request to the Users Service
- User B makes a request to our web service and waits till the lock is released
- This way, we can reduce the number of calls being made to the external web service. returns the response to our web service and the value is cached
- Lock is released, User A gets the response
- User B enters the lock and the cache provides the value (as long it's not expired)
- User B gets the response
InMemoryCachingSample/ # Main application
βββ Infrastructure/ # Core infrastructure components
β βββ CacheProvider.cs # IMemoryCache wrapper
β βββ HttpClient.cs # HTTP client implementation
βββ Services/ # Business logic services
β βββ CachedUserService.cs # Caching decorator for user service
β βββ CacheService.cs # Cache management service
β βββ UsersService.cs # User data service
βββ Utils/ # Utilities
βββ CacheKeys.cs # Cache key constants
InMemoryCachingSample.Tests/ # Test project
βββ CachedUserServiceTests.cs # Tests for caching decorator
βββ CacheProviderTests.cs # Tests for cache provider
βββ CacheServiceTests.cs # Tests for cache service
βββ HttpClientTests.cs # Tests for HTTP client
- .NET 9.0 SDK or later
# Clone the repository
git clone https://github.com/sahansera/InMemoryCacheNetCore.git
# Navigate to the project directory
cd InMemoryCacheNetCore
# Build the project
dotnet build
# Run the application
dotnet run --project InMemoryCachingSample/InMemoryCachingSample.csproj
# Run the tests
dotnet test
Having any issues or troubles getting started? Get in touch with me
Has this Project helped you learn something new? or helped you at work? Please consider giving a βοΈ if this project helped you!
Please share this Repository within your developer community, if you think that this would make a difference! Cheers.
PRs are welcome! Thank you