🚀 Getting Started¶
Welcome to CoreSystem.Cache, a distributed caching framework for .NET 8, .NET 9 and .NET 10.
In this guide you'll learn how to:
- Install the package
- Register the framework
- Perform your first cache operations
- Use the built-in Cache-Aside pattern
Estimated time: 5 minutes
📋 Prerequisites¶
Before getting started, ensure you have:
- .NET 8, .NET 9 or .NET 10 SDK
- An ASP.NET Core application
- (Optional) A Redis server
1️⃣ 📦 Install the Package¶
Install the NuGet package.
dotnet add package CoreSystem.Cache
2️⃣ ⚙️ Register the Framework¶
Register the framework in your application's dependency injection container.
builder.Services.AddCoreCache(options =>
{
});
By default, CoreSystem.Cache can operate using its in-memory cache provider.
External providers such as Redis can be configured when the corresponding provider packages are used.
3️⃣ 💉 Inject the Cache Service¶
Inject ICoreCache wherever caching is required.
public sealed class ProductService(
ICoreCache cache)
{
}
4️⃣ 💾 Store a Value¶
await cache.SetAsync(
"products:1",
product,
TimeSpan.FromMinutes(10));
5️⃣ 📖 Retrieve a Value¶
var product = await cache.GetAsync<Product>(
"products:1");
6️⃣ ⚡ Use the Cache-Aside Pattern¶
The recommended way to retrieve cached data is through GetOrAddAsync.
var product = await cache.GetOrAddAsync(
key: $"products:{id}",
factory: async ct =>
await repository.GetByIdAsync(id, ct),
expiration: TimeSpan.FromMinutes(10),
tags: ["products"]);
The framework automatically:
- Checks whether the item exists in the cache.
- Executes the data factory only on cache misses.
- Stores the result.
- Applies expiration.
- Executes the configured cache pipeline.
7️⃣ 🌐 Enable HTTP Response Caching (Optional)¶
If you want to cache HTTP responses, register the middleware.
var app = builder.Build();
app.UseCoreCache();
app.Run();
Then decorate your endpoints.
[HttpGet("{id}")]
[Cacheable(expirationSeconds:300)]
public async Task<IActionResult> Get(Guid id)
{
return Ok(await service.GetAsync(id));
}
For a complete guide to HTTP response caching, see HTTP Cache.
❓ Need Help?¶
If you encounter an issue or have a suggestion:
- Open a GitHub Issue.
- Start a Discussion.
- Submit a Pull Request.
Contributions are always welcome.