NCache as CacheManager.Core Provider
The NCache CacheManager.Core integration allows CacheManager.Core applications to use NCache as a distributed cache handle and as a backplane for cache invalidation. The integration provides NCacheCacheHandle<T> for cache operations and NCacheCacheBackplane for cache change notifications across application instances.
NCache is configured in CacheManager.Core by passing an NCacheOptions instance during cache handle and backplane registration.
Note
This feature is supported only in the NCache OpenSource edition.
Important
Tag-based eviction is not supported in the CacheManager.Core integration with NCache.
Prerequisites
Before configuring NCache with CacheManager.Core, make sure the following prerequisites are fulfilled:
- Install the following NuGet package in your .NET application:
- OpenSource: NCache.OSS.CacheManager.Core
- Include the following namespaces in your application:
CacheManager.CoreNCache.OSS.CacheManager.CoreMicrosoft.Extensions.DependencyInjectionMicrosoft.Extensions.Logging
- The NCache cache must already exist and must be running.
- Configure
ServerListwhen the application connects to a remote or distributed NCache deployment. - Make sure that the data being added to the cache is serializable.
Configure NCache with CacheManager.Core
The following example configures CacheManager.Core with:
- A dictionary cache handle.
- An NCache distributed cache handle.
- An NCache backplane for cache invalidation.
Add the following code to your Program.cs file.
using CacheManager.Core;
using NCache.OSS.CacheManager.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
var services = new ServiceCollection();
services.AddLogging(cfg =>
{
cfg.AddConsole();
cfg.SetMinimumLevel(LogLevel.Information);
});
var provider = services.BuildServiceProvider();
var loggerFactory = provider.GetRequiredService<ILoggerFactory>();
var options = new NCacheOptions
{
CacheName = "demoCache",
ServerList = new List<NCacheOptions.ServerConfig>
{
new NCacheOptions.ServerConfig
{
Ip = "20.200.20.39",
Port = 9800
}
}
};
var cache = CacheFactory.Build<string>("myCache", settings =>
{
settings.WithDictionaryHandle();
settings.WithHandle(
typeof(NCacheCacheHandle<>),
"cache_handle_name",
true,
options);
settings.WithBackplane(
typeof(NCacheCacheBackplane),
"ncache_config_key",
"example_topic_name",
options);
},
loggerFactory);
In this configuration:
WithDictionaryHandle()configures an in-memory cache handle.WithHandle(...)configures NCache as a CacheManager.Core cache handle.WithBackplane(...)configures the NCache backplane for cache invalidation notifications.NCacheOptionsprovides the NCache cache name and server connectivity details.
Configuration Properties
The following configuration properties are available in the NCacheOptions class.
| Property | Description |
|---|---|
| CacheName* | Specifies the name of the NCache cache instance. The cache must already exist and must be running. This property is required for initialization. |
| ServerList | Specifies the list of NCache server nodes used for cache connectivity. Each entry contains an IP address and port. If the port is not specified, the default NCache client port 9800 is used. |
Note
The properties marked with an asterisk (*) are required.
ServerConfig Properties
Each ServerList entry uses the NCacheOptions.ServerConfig class.
| Property | Description |
|---|---|
| Ip* | Specifies the IP address of the NCache server node. The value must be a valid IPv4 or IPv6 address. |
| Port | Specifies the NCache client port. The value must be between 1 and 65535. The default value is 9800. |
Note
If CacheName is invalid, the cache is not running, or the server configuration is incorrect, provider initialization fails.