Implementing Caching in Web API: Memory Cache, Redis, and IDistributedCache

Hemant SinghHemant Singh
5 min read

Caching is a crucial optimization technique for improving the performance and scalability of Web APIs. In this article, we'll explore various caching options available in .NET Web API, including in-memory caching, Redis caching, and using IDistributedCache for distributed caching. We'll provide examples for each approach.

Prerequisites

Before we dive into caching, make sure you have a basic understanding of .NET Web API development and have created a Web API project.

Option 1: In-Memory Caching

In-memory caching is a simple and effective way to cache data within your application's memory. It's suitable for scenarios where you want to cache data temporarily and have it shared across requests within the same application instance.

Step 1: Install Required NuGet Package

To use in-memory caching, you need to install the Microsoft.Extensions.Caching.Memory NuGet package.

dotnet add package Microsoft.Extensions.Caching.Memory

Step 2: Configure Caching in Startup.cs

In your Startup.cs file, configure the in-memory cache in the ConfigureServices method:

csharpCopy codepublic void ConfigureServices(IServiceCollection services)
{
    // ...

    services.AddMemoryCache();
}

Step 3: Implement Caching in Your Controller

Here's an example of how to use in-memory caching in your controller:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System;

[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
    private readonly IMemoryCache _cache;

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

    [HttpGet("{id}")]
    public IActionResult GetProduct(int id)
    {
        // Check if the item is already in the cache
        if (_cache.TryGetValue($"product_{id}", out Product cachedProduct))
        {
            return Ok(cachedProduct); // Return cached data
        }

        // If not in cache, fetch data from your data source (e.g., database)
        var product = FetchProductFromDatabase(id);

        // Cache the data for a specified duration (e.g., 5 minutes)
        var cacheEntryOptions = new MemoryCacheEntryOptions
        {
            AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5)
        };

        _cache.Set($"product_{id}", product, cacheEntryOptions);

        return Ok(product); // Return fetched data
    }

    // Simulate fetching product data from a database
    private Product FetchProductFromDatabase(int id)
    {
        // Your database access code here
        return new Product { Id = id, Name = $"Product {id}", Price = 19.99m };
    }
}

Option 2: Redis Caching

Redis is a high-performance, distributed caching solution that can be used to cache data across multiple servers or even in a distributed environment. We talked about Redis in a previous article here, you can check it out for a deep dive into Redis.

Step 1: Install Required NuGet Package

To use Redis caching, you need to install the StackExchange.Redis NuGet package.

dotnet add package StackExchange.Redis

Step 2: Configure Redis in Startup.cs

In your Startup.cs file, configure Redis caching in the ConfigureServices method:

public void ConfigureServices(IServiceCollection services)
{
    // ...

    services.AddStackExchangeRedisCache(options =>
    {
        options.Configuration = "localhost"; // Replace with your Redis server connection string
        options.InstanceName = "SampleCache";
    });
}

Step 3: Implement Redis Caching in Your Controller

Here's an example of how to use Redis caching in your controller:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using System;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
    private readonly IDistributedCache _cache;

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

    [HttpGet("{id}")]
    public async Task<IActionResult> GetProduct(int id)
    {
        // Check if the item is already in the cache
        var cachedProduct = await _cache.GetStringAsync($"product_{id}");
        if (cachedProduct != null)
        {
            return Ok(JsonSerializer.Deserialize<Product>(cachedProduct)); // Return cached data
        }

        // If not in cache, fetch data from your data source (e.g., database)
        var product = FetchProductFromDatabase(id);

        // Serialize and cache the data for a specified duration (e.g., 5 minutes)
        var cacheEntryOptions = new DistributedCacheEntryOptions
        {
            AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5)
        };

        await _cache.SetStringAsync($"product_{id}", JsonSerializer.Serialize(product), cacheEntryOptions);

        return Ok(product); // Return fetched data
    }

    // Simulate fetching product data from a database
    private Product FetchProductFromDatabase(int id)
    {
        // Your database access code here
        return new Product { Id = id, Name = $"Product {id}", Price = 19.99m };
    }
}

Option 3: Using IDistributedCache

IDistributedCache is an abstraction that allows you to use various distributed caching providers, including Redis, Azure Cache for Redis, and more, with a consistent API.

Step 1: Install Required NuGet Package (if not already installed)

Ensure you have the Microsoft.Extensions.Caching.Abstractions NuGet package installed.

dotnet add package Microsoft.Extensions.Caching.Abstractions

Step 2: Configure IDistributedCache in Startup.cs

In your Startup.cs file, configure IDistributedCache in the ConfigureServices method:

public void ConfigureServices(IServiceCollection services)
{
    // ...

    services.AddDistributedRedisCache(options =>
    {
        options.Configuration = "localhost"; // Replace with your Cache server connection string
        options.InstanceName = "SampleCache";
    });
}

Step 3: Implement IDistributedCache in Your Controller

Here's an example of how to use IDistributedCache in your controller:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using System;
using System.Text.Json;
using System.Threading.Tasks;

[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
    private readonly IDistributedCache _cache;

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

    [HttpGet("{id}")]
    public async Task<IActionResult> GetProduct(int id)
    {
        // Check if the item is already in the cache
        var cachedProduct = await _cache.GetStringAsync($"product_{id}");
        if (cachedProduct != null)
        {
            return Ok(JsonSerializer.Deserialize<Product>(cachedProduct)); // Return cached data
        }

        // If not in cache, fetch data from your data source (e.g., database)
        var product = FetchProductFromDatabase(id);

        // Serialize and cache the data for a specified duration (e.g., 5 minutes)
        var cacheEntryOptions = new DistributedCacheEntryOptions
        {
            AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5)
        };

        await _cache.SetStringAsync($"product_{id}", JsonSerializer.Serialize(product), cacheEntryOptions);

        return Ok(product); // Return fetched data
    }

    // Simulate fetching product data from a database
    private Product FetchProductFromDatabase(int id)
    {
        // Your database access code here
        return new Product { Id = id, Name = $"Product {id}", Price = 19.99m };
    }
}

Conclusion

Caching is a powerful technique for improving the performance of your Web API. In this article, we explored three caching options in ASP.NET Core Web API: in-memory caching, Redis caching, and using IDistributedCache for distributed caching. Each option has its use cases and advantages. Choose the caching strategy that best suits your application's requirements and scalability needs. By implementing caching, you can reduce response times, minimize database or external service load, and provide a better user experience for your API consumers.

1
Subscribe to my newsletter

Read articles from Hemant Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Hemant Singh
Hemant Singh