PolyCache is a lightweight, flexible distributed cache manager for .NET applications. It provides a simple, unified API for caching with multiple backends, helping you boost performance, reduce load on data sources, and simplify cache handling across your app.
- Supported cache providers: Redis, in-memory, and SQL Server
- Simple DI registration
- Centralized, configurable cache times
- Drop-in interface:
IStaticCacheManager
- Installation
- Quick Start
- Configuration
- Usage
- Setting Up Redis with Docker Compose
- Notes and Recommendations
You can install PolyCache via NuGet.
Package Manager:
Install-Package PolyCache.NET CLI:
dotnet add package PolyCacheRegister PolyCache in your application's dependency injection container.
ASP.NET Core (Startup.cs):
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddPolyCache(Configuration);
// ...
}Minimal hosting (Program.cs, .NET 6+):
var builder = WebApplication.CreateBuilder(args);
// ...
builder.Services.AddPolyCache(builder.Configuration);
// ...
var app = builder.Build();
app.Run();PolyCache supports multiple providers with a simple configuration model. Add the following sections to your appsettings.json:
"CacheConfig": {
"DefaultCacheTime": 60,
"ShortTermCacheTime": 3,
"BundledFilesCacheTime": 120
},
"DistributedCacheConfig": {
"DistributedCacheType": "redis",
"Enabled": true,
"ConnectionString": "127.0.0.1:6379,ssl=False",
"SchemaName": "dbo",
"TableName": "DistributedCache"
}-
CacheConfig
- DefaultCacheTime: Default cache lifetime (minutes) for most entries.
- ShortTermCacheTime: Shorter cache lifetime (minutes) for volatile entries.
- BundledFilesCacheTime: Cache lifetime (minutes) for bundled/static resources.
-
DistributedCacheConfig
- DistributedCacheType: One of
redis,memory, orsqlserver. - Enabled: Set to
trueto enable distributed caching. - ConnectionString: Provider-specific connection string (e.g., Redis endpoint).
- SchemaName (SQL Server only): Database schema for the cache table.
- TableName (SQL Server only): Table name used to store cache entries.
- DistributedCacheType: One of
Inject the IStaticCacheManager interface wherever you need caching.
public class MyService
{
private readonly IStaticCacheManager _cache;
public MyService(IStaticCacheManager cache)
{
_cache = cache;
}
// Use _cache to read/write cache entries, apply lifetimes, etc.
// See the sample project for concrete examples.
}A sample project demonstrates typical usage patterns, including configuration and integration.
If you plan to use Redis as your cache provider, you can spin it up quickly with Docker:
- Install Docker for your OS.
- Download
redis-docker-compose.ymlfrom the sample project. - Open a terminal with administrative privileges.
- Navigate to the folder containing
redis-docker-compose.yml. - Start Redis:
docker-compose -f redis-docker-compose.yml up -d
- Verify it's running:
docker ps
- Update your
appsettings.jsonDistributedCacheConfig.ConnectionStringto point to your Redis instance (e.g.,127.0.0.1:6379,ssl=False).
- For development or single-instance scenarios,
memoryis convenient and fast. - For multi-instance or production scenarios,
redisis recommended for true distributed caching. - When using SQL Server, ensure
SchemaNameandTableNameare set and that the application has proper permissions. - Adjust the cache times in
CacheConfigto match your data volatility and freshness requirements.
Enjoy faster, simpler caching with PolyCache in your .NET applications!