In ASP.NET (non-Core), caching is a powerful feature to improve performance by storing data temporarily. You can cachedata, 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
// Store in cache
HttpContext.Current.Cache.Insert(
key: "UserList",
value: GetUsersFromDatabase(),
dependencies: null,
absoluteExpiration: DateTime.Now.AddMinutes(10),
slidingExpiration: System.Web.Caching.Cache.NoSlidingExpiration
);
// Retrieve from cache
var users = HttpContext.Current.Cache["UserList"] as List<User>;
2. Cache Insert with Callback (Auto Remove Notification)
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In 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)