Code2night
  • Home
  • Guest Posts
  • Tutorial
  • Languages
    • Angular
    • C
    • C#
    • HTML/CSS
    • Java
    • JavaScript
    • Node.js
    • Python
    • React
    • SQL Server
    • TypeScript
  • Post Blog
  • Tools
    • JSON Beautifier
    • HTML Beautifier
    • XML Beautifier
    • CSS Beautifier
    • JS Beautifier
    • PDF Editor
    • Word Counter
    • Base64 Encode/Decode
    • Diff Checker
    • JSON to CSV
    • Password Generator
  • Register
  • Login
  1. Home
  2. Blogpost

Caching in ASP.NET Core using Redis Cache

Date- Nov 12,2022

8119

Free Download Pay & Download
Redis Cache

Distributed Caching

When your cached data is distributed, the data is consistent across server restarts and application deployments. The IDistributedcache interface pertaining to Microsoft.Extensions.Caching.Distributed namespace represents a distributed cache. To manipulate the data stored in the distributed cache, you can use the following methods:

  • Get or GetAsync: To retrieve items from the distributed cache based on its key
  • Set or SetAsync: To store items into the distributed cache
  • Refresh or RefreshAsync: To refresh an item in the distributed cache
  • Remove or RemoveAsync: To delete an item from the distributed cache based on its key


Step for download the Redis-server.exe

  • Download the ZIP Folder from this Github Repo. You could also use the MSI Executable. OR you download the running code and  the redis-server.exe file from pay and download at the top of the blog 
  • The database script file is also in the folder 

"https://github.com/microsoftarchive/redis/releases/tag/win-3.0.504"

  • Extract the Zip Folder and Open up redis-server.exe. That’s it, The Redis Server is now running on your machine. DO NOT CLOSE this Window.

First, let’s install a package that helps you communicate with the Redis server.


https://www.nuget.org/packages/Microsoft.Extensions.Caching.StackExchangeRedis

NuGet\Install-Package Microsoft.Extensions.Caching.StackExchangeRedis -Version 7.0.0

After setting up our local database, we need to update the appsettings.json file

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=DESKTOP-K2Q2EAS; Database=DbEmployee; Trusted_Connection=True; MultipleActiveResultSets=true"
  },
  "RedisCacheServerUrl": "127.0.0.1:6379",
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*"
}

Create a new file called Product.cs and write the following code in there:

namespace RedisCache.Models
{
    public class Product
    {
        public int ProductID { get; set; }

        public string ProductCategory{ get; set; }
       
        public string SubCategory { get; set; }
        
        public string ProductName { get; set; }
      
        public string ProductDescription { get; set; }

        public decimal ProductPrice { get; set; }
 
        public decimal ProductWeight { get; set; }
 
        public int Units { get; set; }
 
        public decimal Total { get; set; }
 
    }
}

Create class DbEmployeeContext

using Microsoft.EntityFrameworkCore;

namespace RedisCache.Models
{
    public class DbEmployeeContext : DbContext
    {
        public DbEmployeeContext(DbContextOptions<DbEmployeeContext> options)
            : base(options)
        {
        }

        public DbSet<Product> Products { get; set; }
    }
}

You can use the following code snippet to specify the Redis connection string in the Program class.

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddControllersWithViews();

            services.AddDbContext<DbEmployeeContext>(options =>options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
            services.AddStackExchangeRedisCache(options =>
            {
                options.Configuration = Configuration["RedisCacheServerUrl"];
            });
        }

The ProductController Class

Create a new API controller class named ProductController

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Caching.Distributed;
using Newtonsoft.Json;
using RedisCache.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace RedisCache.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ProductController : ControllerBase
    {
        private readonly DbEmployeeContext _context;
        private readonly IDistributedCache _cache;

        public ProductController(DbEmployeeContext context, IDistributedCache cache)
        {
            _context = context;
            _cache = cache;
        }

        [HttpGet]
        public async Task<IActionResult> GetAll()
        {
            var products = await _context.Products.ToListAsync();

            return Ok(products);
        }

        [HttpGet]
        [Route("/GetAllByRedis")]
        public async Task<IActionResult> GetAllByRedis()
        {
            var cacheKey = "GET_ALL_PRODUCTS";
            List<Product> products = new List<Product>();

            // Get data from cache
            var cachedData = await _cache.GetAsync(cacheKey);
            if (cachedData != null)
            {
                // If data found in cache, encode and deserialize cached data
                var cachedDataString = Encoding.UTF8.GetString(cachedData);
                products = JsonConvert.DeserializeObject<List<Product>>(cachedDataString);
            }
            else
            {
                // If not found, then fetch data from database
                products = await _context.Products.ToListAsync();

                // serialize data
                var cachedDataString = JsonConvert.SerializeObject(products);
                var newDataToCache = Encoding.UTF8.GetBytes(cachedDataString);

                // set cache options 
                var options = new DistributedCacheEntryOptions()
                    .SetAbsoluteExpiration(DateTime.Now.AddMinutes(2))
                    .SetSlidingExpiration(TimeSpan.FromMinutes(1));

                // Add data in cache
                await _cache.SetAsync(cacheKey, newDataToCache, options);
            }

            return Ok(products);
        }
    }
}

Open up postman and send a GET request to "https://localhost:44386/api/product"


Open up postman and send a GET request to "https://localhost:44386/GetAllByRedis"


S
Shubham Batra
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Comments

Tags

AspNet
C#
programming
AspNet MVC
c programming
AspNet Core
C
software development
tutorial
MVC
memory management
Paypal
coding
coding best practices
data structures
programming tutorial
tutorials
object oriented programming
Slick Slider
StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1760
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp Join Us On Facebook
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blogs
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
Free Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Diff Checker
  • Base64 Encode/Decode
  • Word Counter
By Language
  • Angular
  • C
  • C#
  • HTML/CSS
  • Java
  • JavaScript
  • Node.js
  • Python
  • React
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page