Forem

Cover image for πŸš€ Redis Caching in .NET (Memurai on Windows + Docker Recommended)
Hardik Jariwala
Hardik Jariwala

Posted on

πŸš€ Redis Caching in .NET (Memurai on Windows + Docker Recommended)

Caching is essential for improving performance and scalability in modern applications. This guide shows how to use Redis caching in .NET, including how to run it on Windows using Memurai (without Docker).

πŸ”΄ What is Redis?

Redis is an in-memory data store used to cache frequently accessed data, making applications faster by reducing database calls. It serves data instantly on a cache hit and stores new data on a cache miss, improving performance and scalability.

Why use it?
⚑ Fast (RAM-based)
πŸ“‰ Reduces DB load
🌍 Works across multiple servers
⏱ Supports expiration (TTL)

🧠 How It Works (Cache Aside Pattern)

Request β†’ Check Cache β†’ Return (if found) 
                     ↓ 
                  DB β†’ Save in Cache β†’ Return
Enter fullscreen mode Exit fullscreen mode

πŸ“¦ Required Packages (.NET)

dotnet add package Microsoft.Extensions.Caching.StackExchangeRedis 
dotnet add package Newtonsoft.Json
Enter fullscreen mode Exit fullscreen mode

βš™οΈ Redis Configuration

builder.Services.AddStackExchangeRedisCache(options => 
{ 
     options.Configuration = "localhost:6379"; 
     options.InstanceName =  "MyApp:";
});
Enter fullscreen mode Exit fullscreen mode

πŸ”₯ Caching Implementation (Service Layer)

public class ProductService 
{ 
     private readonly IDistributedCache _cache; 
     public ProductService(IDistributedCache cache) 
     { 
          _cache = cache; 
     } 
     public async Task<List<Product>> GetProducts() 
     { 
         string cacheKey = "product_list"; 
         var cacheData = await _cache.GetStringAsync(cacheKey);
         if (cacheData != null) 
         { 
             Console.WriteLine("Data from Redis");
             return JsonConvert.DeserializeObject<List<Product>>   
(cacheData);
         } 
         Console.WriteLine("Data from DB"); 
         var data = FakeDb.GetProducts(); // Replace with real DB 
         var options = new DistributedCacheEntryOptions 
         { 
             AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5) 
         }; 
         await _cache.SetStringAsync( cacheKey, JsonConvert.SerializeObject(data), options ); 
          return data; 
     } 
}
Enter fullscreen mode Exit fullscreen mode

Run Redis WITHOUT Docker (Windows)

Use Memurai:

  1. Install Memurai
  2. Start service:

Win + R β†’ services.msc β†’ Start "Memurai"

  1. Verify:
memurai-cli ping
Output: `PONG`
Enter fullscreen mode Exit fullscreen mode

Runs on:localhost:6379

🐳 Recommended: Use Docker

Docker (best for production)

docker run -d -p 6379:6379 redis

Enter fullscreen mode Exit fullscreen mode

πŸ”„ Cache Invalidation (Important)

Remove cache manually:

await _cache.RemoveAsync("product_list");
Enter fullscreen mode Exit fullscreen mode

Use expiration:

AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5)
Enter fullscreen mode Exit fullscreen mode

Clear all cache (dev):

memurai-cli flushall
Enter fullscreen mode Exit fullscreen mode

🧠 Best Practices With...

  • Always set expiry
  • Use meaningful keys (product_101)
  • Invalidate cache on update/delete
  • Avoid caching sensitive data

Top comments (0)