Caching in ASP.NET Core using 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"