An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

# โšก Caching Solution โ€“ High-Performance Caching in .NET 9 ๐Ÿš€

![.NET 9](https://img.shields.io/badge/.NET%209-blue?style=for-the-badge)
![Redis](https://img.shields.io/badge/Redis-%E2%9D%A4-red?style=for-the-badge)
![Performance Boost](https://img.shields.io/badge/Performance%20Boost-%E2%9C%85-green?style=for-the-badge)
![Scalability](https://img.shields.io/badge/Scalability-%F0%9F%9A%80-purple?style=for-the-badge)
![Memory Caching](https://img.shields.io/badge/Memory%20Caching-%F0%9F%92%AB-orange?style=for-the-badge)

## ๐ŸŽฏ 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!
```