Asp net core redis cache


ASP.NET is a popular used for building web applications. It provides a framework for dynamic websites and web services. One of the key features of ASP.NET is its ability to work with various data solutions, including Redis cache.

What is Redis Cache?

Redis is an open-, in- data structure store that can be used as a cache, database, or message broker. It provides high-performance data storage and retrieval capabilities, making it an ideal choice for caching frequently accessed data in web applications.

Using Redis Cache in ASP.NET Core

ASP.NET Core is a cross-platform, open-source framework for building web applications. It provides built-in support for using Redis cache as a distributed caching solution.

To use Redis cache in ASP.NET Core, you need to add the necessary NuGet packages to your project. You can do this by adding the following lines to your project's csproj file:


Once you have added the required packages, you can start using Redis cache in your ASP.NET Core application. First, you need to configure the Redis cache in the Startup.cs file. You can do this by adding the following code to the ConfigureServices method:

services.AddStackExchangeRedisCache(options =>
    options.Configuration = "localhost";
    options.InstanceName = "SampleInstance";

In the above code, we are configuring the Redis cache to use the local Redis server with the name “SampleInstance”. You can replace the configuration values with your own Redis server details.

Using Redis Cache in ASP.NET Core

Once the Redis cache is configured, you can start using it in your ASP.NET Core controllers. Here's an example of how you can use Redis cache to cache the result of an expensive database query:

public IActionResult GetProducts()
    var cacheKey = "Products";
    var cachedProducts = _cache.Get(cacheKey);

    if (cachedProducts != null)

    var products = _dbContext.Products.ToList();
    _cache.Set(cacheKey, products);

    return Ok(products);

In the above code, we first check if the products are already cached in Redis cache. If they are, we return the cached products. Otherwise, we fetch the products from the database, cache them in Redis, and return them.


Using Redis cache in ASP.NET Core can greatly improve the performance of your web applications by caching frequently accessed data. By following the steps mentioned in this article, you can easily integrate Redis cache into your ASP.NET Core projects and leverage its benefits.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents