TheAdvDistributedOutputCache 1.0.10

There is a newer version of this package available.
See the version list below for details.
dotnet add package TheAdvDistributedOutputCache --version 1.0.10
                    
NuGet\Install-Package TheAdvDistributedOutputCache -Version 1.0.10
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="TheAdvDistributedOutputCache" Version="1.0.10" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TheAdvDistributedOutputCache" Version="1.0.10" />
                    
Directory.Packages.props
<PackageReference Include="TheAdvDistributedOutputCache" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TheAdvDistributedOutputCache --version 1.0.10
                    
#r "nuget: TheAdvDistributedOutputCache, 1.0.10"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package TheAdvDistributedOutputCache@1.0.10
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TheAdvDistributedOutputCache&version=1.0.10
                    
Install as a Cake Addin
#tool nuget:?package=TheAdvDistributedOutputCache&version=1.0.10
                    
Install as a Cake Tool

TheAdvDistributedOutputCache

TheAdvDistributedOutputCache là middleware Output Caching nâng cao cho ASP.NET Core (.NET 9), hỗ trợ:

Redis + Local Memory Cache
Distributed Lock (RedLock) tránh cache stampede
Per-route TTL Cache
Cache Busting tự động khi có PUT/POST/DELETE
Xóa cache theo pattern & nhiều key liên quan
Redis Key Prefix riêng cho từng project


🚀 Cài đặt

Bạn có thể cài trực tiếp từ NuGet.org:

dotnet add package TheAdvDistributedOutputCache

<PackageReference Include="TheAdvDistributedOutputCache" Version="1.0.*" />

1. Cấu hình appsettings.json 
{
  "CacheOptions": {
    "Prefix": "productA:cache:" 
  }
}

2. Đăng ký Redis Cache + Middleware Trong Program.cs:
using DistributedOutputCache.Cache;
using DistributedOutputCache.Models;
using DistributedOutputCache.Middleware;
using StackExchange.Redis;

var builder = WebApplication.CreateBuilder(args);

// ✅ Load Redis Key Prefix từ cấu hình
builder.Services.Configure<CacheOptions>(
    builder.Configuration.GetSection("CacheOptions")
);

// ✅ Đăng ký Redis & Cache
builder.Services.AddMemoryCache();
builder.Services.AddSingleton<IConnectionMultiplexer>(
    ConnectionMultiplexer.Connect("localhost:6379")
);
builder.Services.AddSingleton<ILocalCacheService, LocalCacheService>();
builder.Services.AddSingleton<IRedisLockHelper>(
    sp => new RedisLockHelper(new[] { "localhost:6379" })
);
builder.Services.AddSingleton<ICacheService, CacheService>();

builder.Services.AddControllers();

var app = builder.Build();

// TTL per-route
var cachePolicies = new List<CacheRoutePolicy>
{
    new CacheRoutePolicy { Pattern = "/api/products/*", Ttl = TimeSpan.FromHours(1) },
    new CacheRoutePolicy { Pattern = "/api/users/*", Ttl = TimeSpan.FromMinutes(5) }
};

// Cache busting rules
var bustingRules = new List<CacheBustingRule>
{
    new CacheBustingRule
    {
        TriggerPattern = "/api/products/*",
        BustPatterns = new[] { "httpcache:/api/products/*" }
    },
    new CacheBustingRule
    {
        TriggerPattern = "/api/users/*",
        BustPatterns = new[]
        {
            "httpcache:/api/users/*",
            "httpcache:/api/users/list*"
        }
    }
};

// ✅ Bật middleware Output Cache
app.UseOutputCache(TimeSpan.FromMinutes(10), cachePolicies, bustingRules);

app.MapControllers();
app.Run();

Cách hoạt động
GET /api/users/123 → cache response trong Redis + local memory
PUT /api/users/123 → tự động xóa cache httpcache:/api/users/123 + /api/users/list*
Distributed Lock tránh nhiều request cùng lúc hit DB
Per-route TTL → mỗi route có thời gian cache riêng

Yêu cầu
ASP.NET Core (.NET 9 trở lên)
Redis server (Standalone hoặc Cluster)

Tùy chọn
Prefix Redis Key → dễ quản lý nhiều project dùng chung Redis
Xóa cache theo pattern → busting nhiều key cùng lúc
Cache riêng từng user (future support)

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.16 114 7/16/2025
1.0.15 109 7/16/2025
1.0.14 107 7/16/2025
1.0.13 107 7/16/2025
1.0.12 106 7/16/2025
1.0.11 110 7/16/2025
1.0.10 114 7/16/2025
1.0.9 113 7/16/2025
1.0.8 112 7/16/2025