Harnessing the Power of Caching in ASP.NET
Caching is essential for optimizing ASP.NET applications. This article explores how to use MemoryCacheManager to implement effective caching strategies, improving performance and scalability.
Development Series — 24 articles
- Decorator Design Pattern - Adding Telemetry to HttpClient
- NuGet Packages: Benefits and Challenges
- Mastering Git Repository Organization
- CancellationToken for Async Programming
- Git Flow Rethink: When Process Stops Paying Rent
- Understanding System Cache: A Comprehensive Guide
- Guide to Redis Local Instance Setup
- Creating a PHP Website with ChatGPT
- TaskListProcessor - Enterprise Async Orchestration for .NET
- Harnessing the Power of Caching in ASP.NET
- Fire and Forget for Enhanced Performance
- Building Resilient .NET Applications with Polly
- The Singleton Advantage: Managing Configurations in .NET
- Troubleshooting and Rebuilding My JS-Dev-Env Project
- Generate Wiki Documentation from Your Code Repository
- My Journey as a NuGet Gallery Developer and Educator
- Architecting Agentic Services in .NET 9: Semantic Kernel
- The Building of React-native-web-start
- TailwindSpark: Ignite Your Web Development
- Evolving PHP Development
- Modernizing Client Libraries in a .NET 4.8 Framework Application
- Building Git Spark: My First npm Package Journey
- Dave's Top Ten: Git Stats You Should Never Track
- Getting to Know Your Repository with git-spark
Filed under Pragmatic architecture and modernization
Topic cluster
Software Development Patterns.NET development, API integration, implementation patterns, and the trade-offs that show up in working code.
Harnessing the Power of Caching in ASP.NET with MemoryCacheManager
While working on the AsyncSpark demonstration in my WebSpark project, I encountered a practical challenge: managing the request limits of the OpenWeather API. As most developers know, API request limits can be a tricky aspect, especially when working with limited resources. In search of a straightforward solution that wouldn't rely heavily on third-party libraries, I developed the MemoryCacheManager class. It's a no-fuss, efficient way to cache API calls – a simple yet effective tool that aligns well with the needs of a project like WebSpark. This article is a walk-through of how MemoryCacheManager fits into the picture, offering a modest peek into the world of caching in ASP.NET development. https://github.com/markhazleton/webspark.
WeatherServiceCachingDecorator
A Real-World Use Case for MemoryCacheManager

Purpose of WeatherServiceCachingDecorator
Enhances an existing weather service with caching capabilities, providing optimized performance and reduced API calls.
Caching Current Weather Data
Unique cache keys are created for each location to store and retrieve weather data distinctly. The class first checks the cache before making external service calls.
Storing and Retrieving Data
If weather data for a location isn't in the cache, it's fetched from the weather service, logged, and stored in the cache with an expiration time.
Caching Weather Forecast Data
Follows a similar caching approach for weather forecasts, reducing service calls and improving response times.
Benefits in a Web Application Context
Offers reduced latency, decreased load on external services, and improved reliability by serving cached data.
Conclusion
Demonstrates the effectiveness of MemoryCacheManager in enhancing service performance and reliability in web applications.
The Role of MemoryCacheManager
The MemoryCacheManager class serves as a robust solution for managing memory caches in ASP.NET applications, offering an efficient approach to data storage and retrieval. It's particularly valuable for applications that demand quick data access with minimal latency.
Concurrent Dictionary for Cache Keys
Utilizes a thread-safe ConcurrentDictionary for storing cache keys, ensuring data integrity across multiple threads.
Cancellation Token Support
Implements CancellationTokenSource for managing cache lifespan and controlled cancellation of cache operations.
Eviction Handling
Intelligent handling of cache eviction with the PostEviction method, catering to different eviction reasons.
Cache Entry Options Customization
Offers customizable cache entry options, including expiration tokens and eviction callbacks.
Clear and Dispose Functionality
Features methods for clearing the cache and proper disposal of resources, essential for maintaining application health.
Benefits
By caching frequently accessed data, MemoryCacheManager enhances application performance through reduced data retrieval operations. It aids in scalability and ensures consistent data access in multi-threaded environments, common in modern web applications.
Code Walkthrough of MemoryCacheManager
The MemoryCacheManager class is a finely-tuned component designed to enhance the performance of ASP.NET applications through efficient caching. Let's take a closer look at its core parts:
Initialization and Setup
The class initializes a static ConcurrentDictionary, _allKeys, to keep track of cache keys. This ensures thread safety and efficient management of cache keys. Additionally, an instance of IMemoryCache, provided by ASP.NET Core, is used for actual caching operations.
Cache Operations
- Adding to Cache: The
Setmethod adds items to the cache. It handles key management and sets cache options, including expiration. - Retrieving from Cache: The
Getmethod fetches items, leveraging the cache-first strategy. If an item is not in cache, it’s loaded and then stored in cache for subsequent requests. - Removing from Cache: The
Removemethod removes items from the cache and updates the key dictionary accordingly.
Eviction Policy
The class implements an eviction policy through the PostEviction callback. This method gets triggered when items are evicted from the cache, allowing for custom actions based on the eviction reason.
Cache Clearing and Disposal
- Clearing the Cache: The
Clearmethod iterates through all keys and removes each item from the cache, ensuring a clean state. - Disposal Pattern: While the current implementation of
Disposemethod is empty, it provides a structure for resource cleanup if needed in the future.
Concurrency and Lock Management
A unique feature is the PerformActionWithLock method, which ensures that actions are performed with exclusive access, preventing race conditions in multi-threaded scenarios.
This walkthrough provides a high-level understanding of the MemoryCacheManager class, emphasizing its role in efficient caching and application performance enhancement. To delve into the specifics, visit the demonstration code on GitHub. https://github.com/markhazleton/webspark
CancellationTokenSource in MemoryCacheManager
There is an instance of the CancellationTokenSource https://learn.microsoft.com/en-us/dotnet/api/system.threading.cancellationtokensource within the MemoryCacheManager class which plays a role in managing the lifecycle of cache entries. Let's delve into its purpose and functionality:
Purpose of CancellationTokenSource
The
CancellationTokenSource
is a powerful .NET construct used to signal cancellation in asynchronous and potentially long-running operations. In the context of the MemoryCacheManager, it serves to manage the expiration and clearing of cache entries.
Managing Cache Lifespan
When a cache entry is added using the
Set
method, the
CancellationTokenSource
is tied to the cache entry's options. This integration allows for controlled expiration of cache items based on specific conditions or time frames.
Controlled Cache Invalidation
In scenarios where there's a need to invalidate the cache prematurely, the
CancellationTokenSource
Recreating the Token After Clearing
Upon invoking the can be used to cancel all associated cache entries. This mechanism is especially useful for implementing dynamic cache clearing strategies, where cache consistency is critical.
Clear
method, the
CancellationTokenSource
is canceled and disposed of, effectively clearing the cache. It’s then crucial to recreate the
CancellationTokenSource
to ensure that new cache entries can be added with their cancellation tokens, maintaining the integrity of cache management.
Overall, the in MemoryCacheManager adds a layer of flexibility and control over how cache entries are managed, ensuring that they can be invalidated as per application requirements, contributing to both the efficiency and reliability of caching mechanisms.
Best Practices and Considerations
Cache Invalidation Strategy
Implement an effective strategy for cache invalidation to maintain data consistency.
Memory Management
Monitor memory usage to prevent potential leaks and optimize performance.
Testing and Optimization
Regularly test and adjust the cache configuration to meet the specific needs of your application.
Conclusion
The MemoryCacheManager class is an essential tool for ASP.NET developers aiming to enhance their application's performance. It offers a balance between simplicity and functionality, making it ideal for robust memory management in web applications.
Explore More
- Decorator Design Pattern - Adding Telemetry to HttpClient -- Adding Telemetry to HttpClient in ASP.NET Core
- My Journey as a NuGet Gallery Developer and Educator -- From Creation to Education in the NuGet Ecosystem
- NuGet Packages: Benefits and Challenges -- Exploring the Pros and Cons of NuGet Packages
- Mastering Git Repository Organization -- Enhance Collaboration and Project Management with Git
- Guide to Redis Local Instance Setup -- Master the Setup of Redis on Your Local Machine
Related project evidence

WebSpark Bootswatch Theme Integration Library
WebSpark.Bootswatch is a .NET Razor Class Library (NuGet package) enabling seamless Bootswatch theme integration into ASP.NET Core applications. Version 2.0+ targets .NET 10 exclusively, featuring dynamic theme switching, light/dark mode with auto-detection, tag helper support, and high-performance StyleCache caching.
Thousands of NuGet downloads

Frogsfolly.com Main
Frogsfolly.com is the original website I created in 1999 when learning web technologies.
ApiSpark
Modular ASP.NET Core backend platform consolidating multiple low-volume APIs into a single Azure-hosted service, serving as the centralized API layer for the Make Bold Spark portfolio.
Hosts my consolidated APIs and the API Test Spark live demo
Working through something similar?
If this maps to something you're working on, I'm glad to compare notes. Start with the system and the constraint that matters most.