---
title: "How to to use cache in asp.net for cache?"  
description: "How to to use cache in asp.net for cache?"  
author: "Anubhav Sharma"  
published: 2025-08-03  
updated: 2025-08-05  
canonical: https://www.mindstick.com/forum/161845/how-to-to-use-cache-in-asp-dot-net-for-cache  
category: "cache"  
tags: ["cache"]  
reading_time: 2 minutes  

---

# How to to use cache in asp.net for cache?

**[Explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) How to to use [cache in asp.net](https://www.mindstick.com/forum/157336/how-to-clear-cache-in-asp-dot-net-mvc) for cache**

## Replies

### Reply by ICSM Computer

In **[ASP.NET](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) (non-Core)**, caching is a powerful feature to improve performance by storing data temporarily. You can [cache](https://www.mindstick.com/blog/222/clearing-cache-in-asp-dot-net) **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

```cs
// 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)

```cs
HttpContext.Current.Cache.Insert(
    "ProductList",
    GetProducts(),
    null,
    DateTime.Now.AddMinutes(5),
    Cache.NoSlidingExpiration,
    CacheItemPriority.Normal,
    (key, value, reason) => {
        // Optional: log or refresh cache here
    }
);
```

## 3. Page Output Caching (Full Page Cache)

Caches the **entire HTML output** of a page.

### In ASPX Page:

```cs
<%@ OutputCache Duration="60" VaryByParam="none" %>
```

- `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:

```cs
<%@ Control Language="C#" %>
<%@ OutputCache Duration="120" VaryByParam="none" %>
```

Include this user control in your page:

```cs
<uc:PopularPosts runat="server" id="PopularPostsControl" />
```

## 5. Cache with Dependencies (Auto Invalidate on File/DB Change)

```cs
CacheDependency fileDependency = new CacheDependency(Server.MapPath("~/App_Data/data.xml"));

HttpContext.Current.Cache.Insert(
    "MyData",
    LoadData(),
    fileDependency
);
```

Cache is cleared if the XML file changes.

## 6. Remove from Cache

```cs
HttpContext.Current.Cache.Remove("UserList");
```

## Note

- This is **in-memory** per **AppDomain**, so if your app restarts or is scaled across servers, the cache is reset.
- For distributed cache (like Redis), you'd need external providers or ASP.NET Core.

## Summary Table

| Use Case | API / Directive | Where Used |
| --- | --- | --- |
| Store and get data | `HttpContext.Current.Cache` | In code |
| Full page caching | `<%@ OutputCache %>` directive | On `.aspx` pages |
| Partial page caching | `<%@ OutputCache %>` in user control | On `.ascx` controls |
| Auto-invalidate | `CacheDependency` | On file or DB change |
| Cache removal | `Cache.Remove(key)` | Programmatically |


---

Original Source: https://www.mindstick.com/forum/161845/how-to-to-use-cache-in-asp-dot-net-for-cache

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
