Generic Cache Manager Application is a simple ASP.NET Core Web API project that uses Redis for caching. This project demonstrates how to use Redis as a caching layer in a web application, providing basic CRUD operations on the cache.
- Get a cached item by key
- Get all cached items
- Add a new item to the cache
- Delete an item from the cache by key
- Delete all items from the cache
- .NET 8 SDK
- Docker (for running Redis)
- Redis
To run Redis locally using Docker, execute the following command:
docker run --name redis -d -p 6379:6379 redis
Ensure your appsettings.json
is configured to connect to your Redis instance. The connection string should look something like this:
{
"ConnectionStrings": {
"Redis": "localhost:6379"
}
}
Navigate to the project directory and restore the dependencies:
dotnet restore
To run the application, use the following command:
dotnet run
The API will be available at https://localhost:5001
.
- URL:
GET /api/Redis/Get
- Parameters:
key
(string) - The key of the cached item - Response: A JSON object containing the cached item
curl -X GET "https://localhost:5001/api/Redis/Get?key=YOUR_CACHE_KEY"
- URL:
GET /api/Redis/GetAll
- Response: A JSON array containing all cached items
curl -X GET "https://localhost:5001/api/Redis/GetAll"
- URL:
POST /api/Redis/Add
- Body:
{
"key": "yourKey",
"value": "yourValue"
}
- Response: A JSON object indicating success or failure
curl -X POST "https://localhost:5001/api/Redis/Add" -H "Content-Type: application/json" -d '{"key":"YOUR_CACHE_KEY","value":"YOUR_CACHE_VALUE}'
- URL:
POST /api/Redis/Delete
- Parameters:
key
(string) - The key of the cached item to delete - Response: A JSON object indicating success or failure
curl -X POST "https://localhost:5001/api/Redis/Delete?key=YOUR_CACHE_KEY"
- URL:
POST /api/Redis/DeleteAll
- Response: A JSON object indicating success or failure
curl -X POST "https://localhost:5001/api/Redis/DeleteAll"
- Controllers
RedisController.cs
- Handles API requests for cache operations
- Models
CacheItemModel.cs
- Represents a cache itemCacheResponseModel.cs
- Represents the response model for cache operations
- Services
- Abstract
IGlobalCacheService.cs
- Interface for cache service
- Concrete
RedisCacheService.cs
- Implementation of the cache service using Redis
- Abstract
- Messages.cs - Contains constant messages used in responses
Represents a general response model for cache operations.
public class CacheResponseModel
{
public bool Success { get; set; }
public string Message { get; set; }
}
public class CacheResponseModel<T> : CacheResponseModel
{
public string Machine { get; set; }
public List<CacheItemModel<T>> Items { get; set; } = new List<CacheItemModel<T>>();
}
Represents a cache item.
public class CacheItemModel<T>
{
public string Key { get; set; }
public T Value { get; set; }
public DateTimeOffset Expiration { get; set; }
}
This project is licensed under the MIT License. See the license file for details.
Please use the Issue > New Issue button to submit issues, feature requests or support issues directly to me. You can also send an e-mail to [email protected].