NCache.Caching.Hybrid
5.3.800
dotnet add package NCache.Caching.Hybrid --version 5.3.800
NuGet\Install-Package NCache.Caching.Hybrid -Version 5.3.800
<PackageReference Include="NCache.Caching.Hybrid" Version="5.3.800" />
<PackageVersion Include="NCache.Caching.Hybrid" Version="5.3.800" />
<PackageReference Include="NCache.Caching.Hybrid" />
paket add NCache.Caching.Hybrid --version 5.3.800
#r "nuget: NCache.Caching.Hybrid, 5.3.800"
#:package NCache.Caching.Hybrid@5.3.800
#addin nuget:?package=NCache.Caching.Hybrid&version=5.3.800
#tool nuget:?package=NCache.Caching.Hybrid&version=5.3.800
NCache.Caching.Hybrid
NCache implementation of ASP.NET Core HybridCache. NCacheHybridCache implements Microsoft.Extensions.Caching.Hybrid.HybridCache on top of an NCache client cache: a local in-process cache (L1) that NCache keeps synchronized with a clustered cache (L2).
Features
- Drop-in
HybridCache: register once, injectHybridCacheanywhere. - L1/L2 caching through an NCache client cache: reads are served from L1 when possible; synchronization with the cluster is handled by NCache.
- Tag-based removal: attach tags on write, remove by one tag or by any of several tags.
- Bulk removal: remove many keys in one call.
- Cache stampede protection: a per-key lock ensures concurrent misses run the factory once.
- Expiration: a default absolute expiration in configuration, overridable per entry.
- Logging: uses
ILogger<NCacheHybridCache>from the standard .NET logging pipeline.
Prerequisites
- A running NCache server or cluster with a clustered cache.
- A client cache enabled for that cache, on every machine that runs your application. This is mandatory. If it is disabled, the constructor throws
InvalidOperationException("Client cache is not configured"). - Connection settings for the cache in the
ncachesection ofappsettings.json, keyed by the cache name. - .NET 8 or later.
See the NCache documentation for how to create and enable a client cache for your cluster.
Installation
dotnet add package NCache.Caching.Hybrid
Install-Package NCache.Caching.Hybrid
Quick Start
1. Configure appsettings.json
{
"ncache": {
"defaultCache": {
"server": "192.168.1.100,192.168.1.101",
"port": 9800
}
},
"NCacheHybridCache": {
"CacheName": "defaultCache",
"Expiration": "00:05:00"
}
}
ncache: specific to NCache, it supplies the server address(es) and port (default 9800) for each cache name.NCacheHybridCache: read by this library (see Configuration options).
2. Register services in Program.cs
var builder = WebApplication.CreateBuilder(args);
// From a configuration section (recommended)
builder.Services.AddNCacheHybridCache(
builder.Configuration.GetSection("NCacheHybridCache"));
// Or from the root configuration (uses the "NCacheHybridCache" section if present)
// builder.Services.AddNCacheHybridCache(builder.Configuration);
// Or from code
// builder.Services.AddNCacheHybridCache(options =>
// {
// options.CacheName = "defaultCache";
// options.Expiration = TimeSpan.FromMinutes(5);
// });
3. Inject and use HybridCache
public class ProductService
{
private readonly HybridCache _cache;
private readonly IProductRepository _database;
public ProductService(HybridCache cache, IProductRepository database)
{
_cache = cache;
_database = database;
}
public async Task<Product> GetProductAsync(int productId, int categoryId)
{
return await _cache.GetOrCreateAsync(
key: $"product:{productId}",
state: productId,
factory: async (id, ct) => await _database.GetProductAsync(id, ct),
tags: new[] { "products", $"category:{categoryId}" });
}
}
Configuration options
NCacheHybridCache section (NCacheHybridCacheOptions)
| Property | Type | Required | Default | Description |
|---|---|---|---|---|
CacheName |
string |
Yes | none | Name of the NCache cache to use. Startup fails if empty or if the cache is not a client cache. |
Expiration |
TimeSpan? |
No | none | Default absolute expiration for entries written by this library. If not set, entries have no expiration and follow the cache's own policy. |
ServerList |
list of { Ip, Port } |
No | empty | Currently not used for connecting. Entries are validated but connection details come from the ncache section / NCache client configuration. Leave empty. |
Expiration uses the .NET TimeSpan string format, for example "00:04:00" for four minutes.
Per-call HybridCacheEntryOptions
| Property | Supported | Behavior |
|---|---|---|
Expiration |
Yes | Absolute expiration for that entry. Overrides NCacheHybridCache:Expiration. |
Flags |
Partly | See below. |
HybridCacheEntryFlags
L1 and L2 share a single NCache handle, so the "local" and "distributed" flags cannot be applied separately:
| Flag | Behavior |
|---|---|
DisableLocalCacheRead or DisableDistributedCacheRead |
Either one skips the cache lookup in GetOrCreateAsync. The factory is called directly and the result is not stored. |
DisableLocalCacheWrite or DisableDistributedCacheWrite |
Either one skips the insert. GetOrCreateAsync still runs the factory and returns the value; SetAsync does nothing. |
Operations
| Method | Behavior |
|---|---|
GetOrCreateAsync |
Returns the cached value, or runs the factory once per key (per process), stores the result with tags and expiration, and returns it. An empty key or a read-disabling flag bypasses the cache. |
SetAsync |
Stores a value. Throws ArgumentNullException for an empty key or null value. Failures during the insert are logged, not thrown. |
RemoveAsync(key) |
Removes one key. No-op for an empty key. |
RemoveAsync(keys) |
Removes many keys in one call. No-op for a null or empty collection. |
RemoveByTagAsync(tag) |
Removes all entries with the tag. The tag "*" clears the whole cache. |
RemoveByTagAsync(tags) |
Removes all entries that carry any of the tags. |
Bulk removal
await _cache.RemoveAsync(products.Select(p => $"product:{p.Id}"));
await _cache.RemoveByTagAsync(new[] { "category:1", "category:2", "category:3" });
Behavior and limitations
- Stampede protection is per process. Multiple application instances can each run the factory once for the same key at the same time.
- Synchronization is NCache's responsibility. The HybridCache library does not provide L1 - L2 synchronization, that is done natively by NCache
- Operations run on the thread pool. The NCache calls are synchronous
- Disposal disposes the underlying NCache cache handle.
Troubleshooting
| Symptom | Cause |
|---|---|
CacheName is required. |
CacheName is empty, or the configuration section name does not match NCacheHybridCache. |
Client cache is not configured (wrapped in "An error occurred while initializing the cache...") |
The named cache is not a client cache on this machine. Create and enable a client cache for the cluster cache. |
IP address '...' is invalid. |
A ServerList entry has a non-IP value (hostnames are rejected). |
| Cannot connect | Check the server and port under ncache:<CacheName>, and that the NCache service is reachable (default port 9800). |
Diagnostic logging
The library logs through ILogger<NCacheHybridCache> (category NCache.Caching.Hybrid.NCacheHybridCache). Hits, misses, factory calls and removals are logged at Information; lock and insert details at Debug.
{
"Logging": {
"LogLevel": {
"NCache.Caching.Hybrid": "Debug"
}
}
}
Use Warning or higher in production, since every call is logged at Information.
License
Copyright © 2005-2026 Alachisoft. All rights reserved.
Resources
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. 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. |
-
net8.0
- Alachisoft.NCache.SDK (>= 5.3.800)
- Microsoft.Extensions.Caching.Hybrid (>= 10.10.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.12)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 10.0.12)
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 |
|---|---|---|
| 5.3.800 | 73 | 11/5/2026 |