FGutierrez.Core.DistributedCache
1.1.2
See the version list below for details.
dotnet add package FGutierrez.Core.DistributedCache --version 1.1.2
NuGet\Install-Package FGutierrez.Core.DistributedCache -Version 1.1.2
<PackageReference Include="FGutierrez.Core.DistributedCache" Version="1.1.2" />
<PackageVersion Include="FGutierrez.Core.DistributedCache" Version="1.1.2" />
<PackageReference Include="FGutierrez.Core.DistributedCache" />
paket add FGutierrez.Core.DistributedCache --version 1.1.2
#r "nuget: FGutierrez.Core.DistributedCache, 1.1.2"
#:package FGutierrez.Core.DistributedCache@1.1.2
#addin nuget:?package=FGutierrez.Core.DistributedCache&version=1.1.2
#tool nuget:?package=FGutierrez.Core.DistributedCache&version=1.1.2
⚡ FGutierrez.Core.DistributedCache
🚀 Overview
FGutierrez.Core.DistributedCache is a high-performance distributed caching library for .NET 8 designed to provide a unified abstraction over multiple cache providers.
The library simplifies cache integration by providing:
- In-memory caching
- Redis distributed caching
- Automatic resilience and fallback strategies
- HTTP response caching middleware
- OpenTelemetry metrics
- Health checks
- Provider-based extensibility
Built following a cloud-native approach, the library allows applications to consume caching capabilities without being coupled to a specific infrastructure provider.
✨ Key Features
| Feature | Description |
|---|---|
| 🧩 Unified API | Single abstraction for multiple cache providers |
| ⚡ High Performance | Optimized cache access patterns |
| 🔄 Resilience | Automatic fallback when distributed cache is unavailable |
| 🧠 Cache Aside Pattern | Built-in GetOrAddAsync workflow |
| 🌐 HTTP Middleware | Response caching support for APIs |
| 📊 Observability | OpenTelemetry metrics integration |
| 🩺 Health Checks | Cache provider availability monitoring |
🏗 Architecture
graph TD
App[Application] --> Cache[ICoreCacheService]
Cache --> Decorator[Resilient Cache Decorator]
Decorator --> Redis[RedisCacheStorage]
Decorator --> Memory[MemoryCacheStorage]
Redis --> RedisServer[(Redis Server)]
Memory --> Runtime[(Application Memory)]
🛡️ Resilience Flow
flowchart TD
Request([Cache Request])
Request --> RedisCheck{Redis Available?}
RedisCheck -- Yes --> RedisAccess[Attempt Redis Access]
RedisAccess --> Success{Operation Successful?}
Success -- Yes --> Return([Return Cached Data])
Success -- No --> Fallback[Log Error + Use Memory Cache]
RedisCheck -- No --> Fallback
Fallback --> MemoryAccess[Query Memory Cache]
MemoryAccess --> Return
⚡ GetOrAddAsync Cache-Aside Pattern
sequenceDiagram
participant App as Application
participant Cache as ICoreCacheService
participant DB as Data Source
App->>Cache: GetOrAddAsync(key, factory)
Cache->>Cache: Check cache entry
alt Cache Hit
Cache-->>App: Return cached value
else Cache Miss
Cache->>DB: Execute factory()
DB-->>Cache: Return data
Cache->>Cache: Store value
Cache-->>App: Return new value
end
🌐 HTTP Cache Middleware Lifecycle
sequenceDiagram
participant Client as HTTP Client
participant MW as Cache Middleware
participant API as API Endpoint
Client->>MW: GET /api/resource
MW->>MW: Check cache
alt Cache Hit
MW-->>Client: Cached Response
else Cache Miss
MW->>API: Execute Request
API-->>MW: Response
MW->>MW: Store Response
MW-->>Client: Original Response
end
📦 Installation
dotnet add package FGutierrez.Core.DistributedCache
⚙️ Configuration
Redis Enabled
builder.Services.AddCoreDistributedCache(options =>
{
options.InstanceName = "MyApplication";
options.DefaultExpiration = TimeSpan.FromMinutes(30);
options.Redis.Enabled = true;
options.Redis.Configuration = redis =>
{
redis.EndPoints.Add("localhost", 6379);
redis.AbortOnConnectFail = false;
};
});
🧑💻 Usage Example
public class ProductService
{
private readonly ICoreCacheService _cache;
public ProductService(ICoreCacheService cache)
{
_cache = cache;
}
public async Task<Product> GetAsync(Guid id)
{
return await _cache.GetOrAddAsync(
$"product:{id}",
async () =>
{
return await LoadFromDatabase(id);
});
}
}
📊 Observability
The library exposes cache metrics through OpenTelemetry.
| Metric | Description |
|---|---|
cache.distributed.hits |
Successful cache retrievals |
cache.distributed.misses |
Cache lookup failures |
cache.distributed.errors |
Provider errors |
cache.distributed.fallbacks |
Resilience fallback executions |
Compatible with any OpenTelemetry backend:
- Grafana
- Prometheus
- Jaeger
- Azure Monitor
- Elastic Observability
- Other OTLP-compatible platforms
🩺 Health Checks
The library integrates with:
builder.Services.AddHealthChecks();
Allowing monitoring of:
- Redis availability
- Memory cache status
- Provider connectivity
🛠️ Requirements
- .NET 8 SDK
- Microsoft.Extensions.Caching.Memory
- StackExchange.Redis
- OpenTelemetry
- Microsoft.Extensions.Diagnostics.HealthChecks
🏗 Design Principles
FGutierrez.Core.DistributedCache follows:
- Clean Architecture principles
- Provider-based extensibility
- High cohesion / low coupling
- Cloud-native design
- Resilient infrastructure patterns
- Observability-first development
📌 Roadmap
- Memory Cache provider
- Redis provider
- Cache Aside pattern
- Resilience fallback
- OpenTelemetry metrics
- Health checks
Future:
- SQL Server cache provider
- PostgreSQL cache provider
- Multi-level distributed cache
- Cache invalidation events
🤝 Contributing
Contributions are welcome.
- Fork the repository
- Create a feature branch
- Commit your changes
- Open a Pull Request
📄 License
MIT License © Federin Pastor Gutierrez Ortiz
See the LICENSE file for details.
⭐ Support
If this ecosystem helps you, consider giving the repository a star on GitHub.
Building modern .NET distributed systems, one reusable component at a time.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- AspNetCore.HealthChecks.Redis (>= 8.0.1)
- FGutierrez.Core.Observability.Abstractions (>= 1.0.0)
- Microsoft.AspNetCore.Http.Abstractions (>= 2.3.10)
- Microsoft.Extensions.Caching.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Caching.Memory (>= 8.0.1)
- OpenTelemetry (>= 1.15.3)
- OpenTelemetry.Extensions.Hosting (>= 1.15.3)
- StackExchange.Redis (>= 2.13.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.