CacheFlow is a comprehensive caching solution that provides: - Thread-safe operations - Comprehensive logging - Flexible cache providers - Configurable serialization - Memory pressure handling - Cache stampede protection - Distributed caching support - Tag-based cache operations - Advanced monitoring and metrics - Multi-layer caching (memory and distributed) Perfect for high-performance applications requiring robust caching capabilities.
$ dotnet add package CacheFlowA high-performance, thread-safe, multi-layer caching library for .NET with advanced features including cache stampede protection and distributed caching support.
dotnet add package CacheFlow
// Configure services
services.AddCacheFlow(options =>
{
options.EnableLogging = true;
options.EnableMetrics = true;
options.DefaultDuration = TimeSpan.FromMinutes(10);
});
// Use in your code
public class ProductService
{
private readonly CacheFlowManager _cache;
public ProductService(CacheFlowManager cache)
{
_cache = cache;
}
public async Task<Product> GetProductAsync(string productId)
{
return await _cache.GetOrCreateAsync(
$"product:{productId}",
productId,
async (id, ct) => await FetchProductFromDatabase(id),
new CacheFlowEntryOptions
{
Duration = TimeSpan.FromHours(1),
Tags = new[] { "products" }
}
);
}
}
| Option | Description | Default |
|---|
| DefaultDuration | Default cache duration | 30 minutes |
| EnableLogging | Enable detailed logging | true |
| EnableMetrics | Enable performance metrics | true |
| MaxMemorySize | Maximum memory cache size | null (no limit) |
| SerializerType | Type of serializer to use | JSON |
// Set a value
await _cache.SetAsync("key", value);
// Get a value
var result = await _cache.GetOrCreateAsync("key", factory);
// Remove a value
await _cache.RemoveAsync("key");// Set with tags
await _cache.SetAsync(
"product:1",
product,
new CacheFlowEntryOptions
{
Tags = new[] { "products", "active" }
}
);
// Remove by tag
await _cache.RemoveByTagAsync("products");// Custom serialization
services.AddCacheFlow()
.WithCustomSerializer<MyCustomSerializer>();
// Memory pressure handling
services.AddCacheFlow(options =>
{
options.MemoryPressureMonitoringEnabled = true;
options.MaxMemorySize = 1024 * 1024 * 100; // 100MB
});
// Metrics collection
services.AddCacheFlow(options =>
{
options.EnableMetrics = true;
options.MetricsUpdateInterval = TimeSpan.FromSeconds(5);
});Performance Optimization
Data Management
Monitoring
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
private readonly CacheFlowManager _cache;
public ProductsController(CacheFlowManager cache)
{
_cache = cache;
}
[HttpGet("{id}")]
public async Task<IActionResult> GetProduct(string id)
{
var product = await _cache.GetOrCreateAsync(
$"product:{id}",
id,
async (pid, ct) => await _repository.GetProductAsync(pid),
new CacheFlowEntryOptions
{
Duration = TimeSpan.FromHours(1),
Tags = new[] { "products" }
}
);
return Ok(product);
}
}public class CacheMaintenanceService : BackgroundService
{
private readonly CacheFlowManager _cache;
private readonly ILogger<CacheMaintenanceService> _logger;
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
try
{
// Cleanup expired products
await _cache.RemoveByTagAsync("expired-products");
await Task.Delay(TimeSpan.FromHours(1), stoppingToken);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error during cache maintenance");
}
}
}
}services.AddCacheFlow()
.AddDistributedCache(options =>
{
options.InstanceName = "MyApp";
options.SyncTimeout = TimeSpan.FromSeconds(5);
});var options = new CacheFlowEntryOptions
{
SlidingExpiration = true,
Priority = CachePriority.High,
Duration = TimeSpan.FromHours(1),
Tags = new[] { "products", "featured" }
};Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the terms mentioned in the package.