Explain How to to use cache in asp.net for cache
How to to use cache in asp.net for cache?
626
03-Aug-2025
ICSM Computer
05-Aug-2025In ASP.NET (non-Core), caching is a powerful feature to improve performance by storing data temporarily. You can cache data, pages, or fragments of pages.
1. Using
HttpRuntime.Cache/HttpContext.Cache(In-Memory Cache)Best for caching data like query results or processed objects.
Example: Caching Data in Code
2. Cache Insert with Callback (Auto Remove Notification)
3. Page Output Caching (Full Page Cache)
Caches the entire HTML output of a page.
In ASPX Page:
Duration="60": cache for 60 seconds.VaryByParam="none": same cache for all query parameters.4. Fragment Caching (User Control or Partial Cache)
Useful if part of your page changes often, but other parts don’t.
Example in ASPX User Control:
Include this user control in your page:
5. Cache with Dependencies (Auto Invalidate on File/DB Change)
Cache is cleared if the XML file changes.
6. Remove from Cache
Note
Summary Table
HttpContext.Current.Cache<%@ OutputCache %>directive.aspxpages<%@ OutputCache %>in user control.ascxcontrolsCacheDependencyCache.Remove(key)