https://github.com/mreshboboyev/caching
A .NET 9 solution demonstrating various caching techniques in ASP.NET Core, including in-memory caching, distributed caching with Redis, and output caching. The project aims to improve application performance and scalability through effective caching strategies.
https://github.com/mreshboboyev/caching
asp-net-core caching distributed-caching dotnet-9 in-memory-caching output-caching performance redis scalability web-api
Last synced: 19 days ago
JSON representation
A .NET 9 solution demonstrating various caching techniques in ASP.NET Core, including in-memory caching, distributed caching with Redis, and output caching. The project aims to improve application performance and scalability through effective caching strategies.
- Host: GitHub
- URL: https://github.com/mreshboboyev/caching
- Owner: MrEshboboyev
- Created: 2025-02-22T08:30:19.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2025-11-12T18:50:38.000Z (7 months ago)
- Last Synced: 2025-11-12T20:27:04.413Z (7 months ago)
- Topics: asp-net-core, caching, distributed-caching, dotnet-9, in-memory-caching, output-caching, performance, redis, scalability, web-api
- Language: C#
- Homepage:
- Size: 40 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# โก Caching Solution โ High-Performance Caching in .NET 9 ๐





## ๐ฏ Overview
This **.NET 9 Caching Solution** demonstrates multiple caching techniques using **ASP.NET Core**, including **In-Memory Caching, Distributed Caching (Redis), Output Caching, and Response Caching**. The goal is to **optimize API performance, reduce database load, and enhance scalability** for modern applications.
> **Why Caching?**
> - ๐ **Speed Up Responses** โ Reduce API response time with fast data retrieval.
> - ๐ **Reduce Server Load** โ Minimize unnecessary database queries.
> - ๐ **Scalability** โ Handle high-traffic applications efficiently.
---
## ๐ Features
โ
**In-Memory Caching** โ Fast, efficient caching within the application.
โ
**Distributed Caching (Redis)** โ Scalable caching solution for cloud and large-scale apps.
โ
**Output Caching** โ Caches entire HTTP responses to boost performance.
โ
**Response Caching** โ Adds caching headers for optimized client-side caching.
โ
**Advanced Caching Strategies**:
- ๐น **Sliding Expiration** โ Keeps cache alive if frequently accessed.
- ๐น **Post-Eviction Callbacks** โ Logs when cache entries expire.
- ๐น **Cache Dependencies** โ Invalidates cache when related data changes.
---
## ๐๏ธ Architecture & Project Structure
๐ **src/Caching.Api** โ Main API project with caching endpoints.
๐ **src/Caching.Api/Services** โ Business logic for caching and product retrieval.
๐ **src/Caching.Api/Endpoints** โ Defines API routes for caching functionalities.
### ๐๏ธ **Implemented Caching Techniques**
๐น **Memory Cache** โ Stores frequently accessed data in-memory.
๐น **Redis Cache** โ Provides distributed caching across multiple servers.
๐น **Output Cache** โ Caches HTTP responses at the server level.
๐น **Response Cache** โ Implements client-side cache control headers.
---
## ๐ Getting Started
### **๐ Prerequisites**
โ
[.NET 9 SDK](https://dotnet.microsoft.com/download/dotnet/9.0)
โ
[Redis](https://redis.io/download) (for distributed caching)
โ
[Docker](https://www.docker.com/) (optional for Redis container)
### **Step 1: Clone the Repository**
```bash
git clone https://github.com/yourusername/caching-solution.git
cd caching-solution
```
### **Step 2: Install Dependencies**
```bash
dotnet restore
```
### **Step 3: Configure Redis (Optional, for Distributed Caching)**
Update `appsettings.json`:
```json
{
"ConnectionStrings": {
"Redis": "your_redis_connection_string"
}
}
```
### **Step 4: Run Redis with Docker (Optional)**
```bash
docker run -d -p 6379:6379 redis
```
### **Step 5: Run the Application**
```bash
dotnet run --project src/Caching.Api
```
---
## ๐ API Endpoints
๐น **Basic Product Retrieval**
| Method | Endpoint | Description |
|--------|------------------------|-------------|
| **GET** | `/products` | Fetches products **without caching** |
๐น **Memory Caching**
| Method | Endpoint | Description |
|--------|----------------------------------|-------------|
| **GET** | `/absolute-cache-products` | Memory cache with **absolute expiration** |
| **GET** | `/sliding-cache-products` | Memory cache with **sliding expiration** |
| **GET** | `/eviction-callback-cache-products` | Cache with **post-eviction callback** |
| **GET** | `/priority-cache-products` | Cache with **priority handling** |
| **GET** | `/dependent-cache-products` | Cache with **dependencies** |
๐น **Distributed Caching (Memory + Redis)**
| Method | Endpoint | Description |
|--------|----------------------------------------|-------------|
| **GET** | `/memory-redis-cache-products` | Products with **in-memory + Redis caching** |
| **GET** | `/memory-redis-cache-products-sliding` | Products with **sliding expiration** |
| **GET** | `/memory-redis-cache-products-eviction-callback` | Products with **post-eviction callback** |
| **GET** | `/memory-redis-cache-products-dependency` | Products with **cache dependency** |
๐น **Output Caching**
| Method | Endpoint | Description |
|--------|------------------------|-------------|
| **GET** | `/output-cache-products` | Caches **entire HTTP response** |
| **GET** | `/output-cache` | Output cache with **timestamp** |
๐น **Response Caching**
| Method | Endpoint | Description |
|--------|-----------------------|-------------|
| **GET** | `/response-cache-products` | Adds **cache control headers** |
| **GET** | `/response-cache` | Response cache with **vary header** |
---
## ๐งช Testing
### **Unit Tests**
Run unit tests for caching and API responses:
```bash
dotnet test
```
### **Manual API Testing**
๐ **Use Postman or Swagger UI** to:
โ
**Test without caching** โ `/products`
โ
**Enable caching** โ `/memory-cache-products`
โ
**Test Redis caching** โ `/memory-redis-cache-products`
โ
**Check output caching** โ `/output-cache-products`
---
## ๐ฏ Why Use This Project?
โ
**Blazing-Fast API Responses** โ Implements multiple caching strategies.
โ
**Scalable & Cloud-Ready** โ Uses Redis for **distributed caching**.
โ
**Performance-Oriented** โ Reduces unnecessary **database queries**.
โ
**Flexible & Configurable** โ Supports **multiple expiration policies**.
โ
**Enterprise-Grade Architecture** โ Built using **.NET 9** best practices.
---
## ๐ License
This project is licensed under the **MIT License**. See [LICENSE](LICENSE) for details.
---
## ๐ Contact
For feedback, contributions, or questions:
๐ง **Email**: mreshboboyev@gmail.com
๐ป **GitHub**: [MrEshboboyev](https://github.com/MrEshboboyev)
---
๐ **Supercharge your .NET APIs with advanced caching!** Clone the repo & start optimizing today!
```