A small ASP.NET Core Web API learning demo that shows how to use Redis caching (cache-aside pattern) with:
- Entity Framework Core + SQL Server for persistence
- Redis for caching API responses
- Simple cache invalidation on create/update/delete
- Cache-aside flow:
- On GET: check Redis first → if miss, load from DB → store in Redis
- On POST/PUT/DELETE: update DB → remove related cache keys
- Cache keys for:
- A paginated list of books
- A single book by ISBN
X-Cacheresponse header (HIT/MISS) so you can see when Redis is used
- ASP.NET Core Web API
- EF Core + SQL Server
- Redis
IDistributedCache+StackExchange.Redis
- .NET SDK (a recent version that supports minimal hosting)
- SQL Server (LocalDB or full SQL Server)
- Redis (local or Docker)
Set connection strings (example below). You can use either:
appsettings.Development.json, or- .NET User Secrets (recommended for local dev)
Example appsettings.Development.json:
{
"ConnectionStrings": {
"conStr": "Server=localhost;Database=RedisCacheLabDb;Trusted_Connection=True;TrustServerCertificate=True;",
"Redis": "localhost:6379"
}
}From the repo root (or the project folder), run:
dotnet ef database updateIf EF tools aren’t installed:
dotnet tool install --global dotnet-efdotnet run --project RedisCacheLabIf Swagger / OpenAPI is enabled in Development, open the browser at the Swagger/OpenAPI endpoint printed in the console.
Base route: /api/book
GET /api/book?page=1&pageSize=10
- On cache hit: returns header
X-Cache: HIT - On cache miss: returns header
X-Cache: MISS
GET /api/book/{isbn}
POST /api/book
Example body:
{
"id": 1,
"title": "Clean Code",
"author": "Robert C. Martin",
"isbn": "9780132350884",
"isAvailable": true
}PUT /api/book/{isbn}
DELETE /api/book/{isbn}
- This is intended as a learning lab, not a production-ready caching strategy.
- Pattern-based cache deletion is convenient for demos but can be expensive at scale.
Add a license if you plan to share/reuse this code.