A Distributed Cache manager package for .Net Core Projects.
$ dotnet add package PolyCachePolyCache 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.
IStaticCacheManagerYou can install PolyCache via NuGet.
Package Manager:
Install-Package PolyCache
.NET CLI:
dotnet add package PolyCache
Register 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
DistributedCacheConfig
redis, memory, or sqlserver.true to enable distributed caching.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:
redis-docker-compose.yml from the sample project.redis-docker-compose.yml.docker-compose -f redis-docker-compose.yml up -d
docker ps
appsettings.json DistributedCacheConfig.ConnectionString to point to your Redis instance (e.g., 127.0.0.1:6379,ssl=False).memory is convenient and fast.redis is recommended for true distributed caching.SchemaName and TableName are set and that the application has proper permissions.CacheConfig to match your data volatility and freshness requirements.Enjoy faster, simpler caching with PolyCache in your .NET applications!