LowCodeHub.AdvancedHybridCache 0.0.11

There is a newer version of this package available.
See the version list below for details.
dotnet add package LowCodeHub.AdvancedHybridCache --version 0.0.11
                    
NuGet\Install-Package LowCodeHub.AdvancedHybridCache -Version 0.0.11
                    
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="LowCodeHub.AdvancedHybridCache" Version="0.0.11" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LowCodeHub.AdvancedHybridCache" Version="0.0.11" />
                    
Directory.Packages.props
<PackageReference Include="LowCodeHub.AdvancedHybridCache" />
                    
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 LowCodeHub.AdvancedHybridCache --version 0.0.11
                    
#r "nuget: LowCodeHub.AdvancedHybridCache, 0.0.11"
                    
#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 LowCodeHub.AdvancedHybridCache@0.0.11
                    
#: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=LowCodeHub.AdvancedHybridCache&version=0.0.11
                    
Install as a Cake Addin
#tool nuget:?package=LowCodeHub.AdvancedHybridCache&version=0.0.11
                    
Install as a Cake Tool

LowCodeHub.AdvancedHybridCache

A typed, region-aware wrapper over Microsoft.Extensions.Caching.Hybrid for .NET 10.

It keeps the native HybridCache behavior, including stampede protection, local cache, distributed cache support, tag invalidation, and serializer extensibility, while adding a small LowCodeHub-friendly API:

  • CacheKey for collision-safe region/key composition.
  • IAdvancedHybridCache for get, set, remove, and tag invalidation operations.
  • CacheGetResult<T> for explicit cache hit/miss results.
  • AddAdvancedHybridCache(...) for DI registration that still returns IHybridCacheBuilder.

NuGet License: MIT

Why This Library?

Feature LowCodeHub.AdvancedHybridCache
Region-aware keys CacheKey("products", productId.ToString())
Collision-safe formatting Length-prefixed region/key string
Explicit try-get CacheGetResult<T> separates misses from cached defaults
Tag invalidation Tags flow to HybridCache on get/set
Stampede protection Delegates to HybridCache GetOrCreateAsync
Local + distributed cache Uses the native HybridCache backend model
Serializer extensibility Returns IHybridCacheBuilder for serializer chaining
Package conventions Targets net10.0, ships README, MIT licensed

Installation

dotnet add package LowCodeHub.AdvancedHybridCache

Targets net10.0.

Quick Start

using LowCodeHub.AdvancedHybridCache.Abstractions;
using LowCodeHub.AdvancedHybridCache.Extensions;
using LowCodeHub.AdvancedHybridCache.Models;

builder.Services.AddAdvancedHybridCache();
public sealed class ProductService(IAdvancedHybridCache cache)
{
    public ValueTask<Product> GetProductAsync(Guid productId, CancellationToken ct = default) =>
        cache.GetOrCreateAsync(
            new CacheKey("products", productId.ToString(), tags: ["products"]),
            token => LoadProductAsync(productId, token),
            expiration: TimeSpan.FromMinutes(10),
            ct);

    public ValueTask InvalidateAllProductsAsync(CancellationToken ct = default) =>
        cache.RemoveByTagAsync("products", ct);
}

The TimeSpan overloads of GetOrCreateAsync / SetAsync apply the expiration to both the distributed and the local tier. Omit it to use HybridCacheOptions.DefaultEntryOptions, or pass a full HybridCacheEntryOptions for per-tier control.

Configure defaults

// Lambda
builder.Services.AddAdvancedHybridCache(options =>
{
    options.MaximumKeyLength = 1024;
    options.DefaultEntryOptions = new HybridCacheEntryOptions
    {
        Expiration = TimeSpan.FromMinutes(10),
        LocalCacheExpiration = TimeSpan.FromMinutes(2)
    };
});

// Or bind HybridCacheOptions from configuration (optionally followed by a lambda)
builder.Services.AddAdvancedHybridCache(builder.Configuration.GetSection("HybridCache"));
{
  "HybridCache": {
    "MaximumKeyLength": 1024,
    "MaximumPayloadBytes": 1048576,
    "DefaultEntryOptions": { "Expiration": "00:10:00", "LocalCacheExpiration": "00:02:00" }
  }
}

Try-Get Without Ambiguous Defaults

HybridCache primarily exposes GetOrCreateAsync. This package adds a try-get shape that does not confuse a cache miss with a cached default value:

var result = await cache.TryGetAsync<int>(
    new CacheKey("product-inventory", productId.ToString(), ["inventory"]),
    cancellationToken);

if (!result.Found)
{
    return Results.NotFound();
}

return Results.Ok(result.Value);

This matters for valid cached values such as:

  • 0
  • false
  • null
  • default(T)

Use result.Found to decide whether the entry exists. Use result.Value only after that check.

Cache Keys

CacheKey has three parts:

Property Required Purpose
Region Yes Logical namespace, such as products, users, or permissions
Key Yes Entry identity within the region
Tags No Tags passed to HybridCache for group invalidation
var cacheKey = new CacheKey(
    region: "tenant-settings",
    key: tenantId.ToString(),
    tags: [$"tenant:{tenantId}", "tenant-settings"]);

The generated cache key is length-prefixed, so regions and keys that contain : do not collide.

Set and Remove

Set a value directly:

await cache.SetAsync(
    new CacheKey("feature-flags", tenantId.ToString(), [$"tenant:{tenantId}", "feature-flags"]),
    flags,
    cancellationToken);

Set with per-entry expiration:

await cache.SetAsync(
    new CacheKey("reports", reportId.ToString(), ["reports"]),
    report,
    new HybridCacheEntryOptions
    {
        Expiration = TimeSpan.FromHours(1),
        LocalCacheExpiration = TimeSpan.FromMinutes(5)
    },
    cancellationToken);

Remove one entry:

await cache.RemoveAsync(new CacheKey("products", productId.ToString()), cancellationToken);

Remove entries by tag:

await cache.RemoveByTagAsync("products", cancellationToken);

Distributed Cache Backend

AddAdvancedHybridCache registers Microsoft HybridCache. If your application also registers an IDistributedCache implementation, HybridCache can use it as the distributed tier.

Redis example:

builder.Services.AddStackExchangeRedisCache(options =>
{
    options.Configuration = builder.Configuration.GetConnectionString("Redis");
});

builder.Services.AddAdvancedHybridCache();

Without a distributed cache registration, HybridCache still provides local in-process caching.

Serializers

The registration method returns IHybridCacheBuilder, so serializers are configured exactly as with native HybridCache:

builder.Services
    .AddAdvancedHybridCache()
    .AddSerializer<Product, ProductProtobufSerializer>()      // per-type serializer
    .AddSerializerFactory<MyHybridCacheSerializerFactory>(); // or a factory

Values are stored inside a small CacheGetResult<T> envelope so TryGetAsync can tell a miss from a cached null/0/false. The package registers a serializer factory that makes this transparent:

  • If you register a serializer (or a non-default factory) for T, it is used for the payload; the envelope adds a single marker byte.
  • Otherwise HybridCache's default System.Text.Json serializer handles the envelope ({"Found":true,"Value":...}).

Behavior Notes

  • GetOrCreateAsync uses HybridCache stampede protection for concurrent misses.
  • TryGetAsync never calls a source callback; it returns Found = false on a miss.
  • Tags are not part of the cache identity; they are passed to HybridCache for invalidation.
  • CacheKey.Region and CacheKey.Key must be non-empty and non-whitespace. Using default(CacheKey) throws a descriptive exception (check IsInitialized if you store keys).
  • Keys longer than HybridCacheOptions.MaximumKeyLength (default 1024, including the advanced-hybrid-cache: prefix and length markers) or containing control characters throw ArgumentException. HybridCache itself would only log and skip caching them, which hides the problem.
  • RemoveByTagAsync rejects empty or whitespace tags.
  • If underlying data retrieval is disabled through HybridCache entry options and the entry is missing, GetOrCreateAsync throws InvalidOperationException.
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  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
0.1.0 114 11/15/2026
0.0.11 111 10/22/2026
0.0.10 441 6/21/2026
0.0.3 129 6/1/2026
0.0.2 230 6/1/2026