---
title: "How do you cache data in ASP.NET Core? What are the options?"  
description: "How do you cache data in ASP.NET Core? What are the options?"  
author: "ICSM Computer"  
published: 2025-06-16  
updated: 2025-06-17  
canonical: https://www.mindstick.com/forum/161720/how-do-you-cache-data-in-asp-dot-net-core-what-are-the-options  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# How do you cache data in ASP.NET Core? What are the options?

**How do you [cache](https://www.mindstick.com/blog/222/clearing-cache-in-asp-dot-net) [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) in [ASP.NET Core](https://www.mindstick.com/articles/12946/get-started-with-asp-dot-net-core-mvc-and-visual-studio)? What are the [options](https://www.mindstick.com/articles/43878/making-the-best-use-of-the-options-trade-ideas)?**

## Replies

### Reply by ICSM Computer

In **[ASP.NET](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) Core**, caching improves application performance by temporarily storing frequently accessed data. There are **three primary caching options**:

## 1. In-Memory Caching

### Best for:

- Small- to medium-sized data
- Single-server scenarios (not shared across instances)

### Example:

```cs
public class MyService
{
    private readonly IMemoryCache _cache;

    public MyService(IMemoryCache cache)
    {
        _cache = cache;
    }

    public string GetData()
    {
        return _cache.GetOrCreate("myKey", entry =>
        {
            entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5);
            return "cached value";
        });
    }
}
```

### Register in `Startup.cs` (for .NET Core 3.x) or `Program.cs` (.NET 6+):

```cs
builder.Services.AddMemoryCache();
```

## 2. Distributed Caching

Used in **multi-server or cloud environments**.

### Options:

- SQL Server
- Redis
- NCache

### Best for:

- Shared data across multiple app instances
- Session data in scalable environments

### Redis Example:

```cs
# Install Redis package
dotnet add package Microsoft.Extensions.Caching.StackExchangeRedis
```

```cs
builder.Services.AddStackExchangeRedisCache(options =>
{
    options.Configuration = "localhost:6379"; // or use Azure Redis Cache endpoint
});
```

```cs
public class MyService
{
    private readonly IDistributedCache _cache;

    public MyService(IDistributedCache cache)
    {
        _cache = cache;
    }

    public async Task<string> GetDataAsync()
    {
        var data = await _cache.GetStringAsync("myKey");
        if (data == null)
        {
            data = "cached value";
            await _cache.SetStringAsync("myKey", data, new DistributedCacheEntryOptions
            {
                AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5)
            });
        }
        return data;
    }
}
```

## 3. Response Caching

### Best for:

- Caching full HTTP responses
- Static or rarely changing output

### Setup:

```cs
builder.Services.AddResponseCaching();
```

```cs
app.UseResponseCaching();
```

### Annotate Controller/Action:

```cs
[HttpGet]
[ResponseCache(Duration = 60, Location = ResponseCacheLocation.Client)]
public IActionResult Get()
{
    return Ok("cached response");
}
```

## Summary Comparison

| Caching Type | Scope | Shared Across Servers? | Use Case |
| --- | --- | --- | --- |
| In-Memory Cache | Application | No | Small, fast-access, per-server data |
| Distributed Cache | External | Yes | Web farms, cloud-scale apps |
| Response Cache | HTTP Layer | Depends on setup | Caching API/page responses |


---

Original Source: https://www.mindstick.com/forum/161720/how-do-you-cache-data-in-asp-dot-net-core-what-are-the-options

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
