Abstractions for polling-based refresh functionality in ItemsCache - provides interfaces for periodic cache refresh operations.
$ dotnet add package ItemsCache.Refresh.Polling.AbstractionA high-performance, flexible caching library for ASP.NET Core applications that provides automatic data loading, background refresh, and retry logic with SOLID principles.
For the complete experience, install the main package:
dotnet add package ItemsCache.All
Or install individual packages based on your needs:
dotnet add package ItemsCache
dotnet add package ItemsCache.Refresh.Polling
dotnet add package ItemsCache.RetryPolicy
Program.cs:using ItemsCache.Extensions;
var builder = WebApplication.CreateBuilder(args);
// Add ItemsCache services
builder.Services.AddItemsCache()
.AddRefreshPolling()
.AddRetryPolicy();
var app = builder.Build();
public class ProductDataSource : IDataSource<Product>
{
private readonly HttpClient _httpClient;
public ProductDataSource(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<Product> LoadAsync(string key)
{
var response = await _httpClient.GetFromJsonAsync<Product>($"/api/products/{key}");
return response ?? throw new InvalidOperationException("Product not found");
}
}
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
private readonly IItemsCacheService<Product> _cache;
public ProductsController(IItemsCacheService<Product> cache)
{
_cache = cache;
}
[HttpGet("{id}")]
public async Task<ActionResult<Product>> GetProduct(string id)
{
var product = await _cache.GetAsync(id);
return Ok(product);
}
}
Configure automatic cache refresh:
builder.Services.AddItemsCache()
.AddRefreshPolling(options =>
{
options.RefreshInterval = TimeSpan.FromMinutes(5);
options.Enabled = true;
});
Configure retry behavior:
builder.Services.AddItemsCache()
.AddRetryPolicy(options =>
{
options.MaxRetryAttempts = 3;
options.DelayBetweenRetries = TimeSpan.FromSeconds(1);
options.UseExponentialBackoff = true;
});
Implement your own data source:
public class DatabaseDataSource : IDataSource<User>
{
private readonly AppDbContext _context;
public DatabaseDataSource(AppDbContext context)
{
_context = context;
}
public async Task<User> LoadAsync(string key)
{
var user = await _context.Users.FindAsync(key);
return user ?? throw new InvalidOperationException("User not found");
}
}
ItemsCache follows SOLID principles with a clean, modular architecture:
ItemsCache.Core.Abstraction
├── IDataSource<T>
├── IItemsCacheService<T>
└── IItemsCacheServiceWithModifications<T>
ItemsCache.Core
├── ItemsCacheService<T>
├── ItemsCacheLoader<T>
└── CacheInitHostedService
ItemsCache.Refresh.Abstraction
├── IRefreshItemCacheHandler<T>
└── IRefreshItemCacheHandlerFactory<T>
ItemsCache.Refresh.Core
├── RefreshItemCacheHandler<T>
└── RefreshItemCacheHandlerFactory<T>
ItemsCache.Refresh.Polling
├── PollingRefreshService<T>
└── PollingRefreshHostedService<T>
ItemsCache.RetryPolicy.Abstraction
├── IRetryPolicy<T>
└── RetryPolicyOptions<T>
ItemsCache.RetryPolicy.Polly
└── PollyRetryPolicy<T>
We welcome contributions! Please see our Contributing Guidelines for details.
dotnet restoredotnet builddotnet testThis project is licensed under the MIT License - see the LICENSE file for details.
See CHANGELOG.md for a list of changes and version history.
Made with ❤️ by the ItemsCache Contributors