缓存连接
配置成功后 NCache,您可以开始开发应用程序 NCache 通过嵌入 NCache API 调用。为此,您需要确保缓存连接。单个应用程序中可以连接单个以及多个缓存。此外,缓存可以与安全凭证连接。
备注
此功能也可用于 NCache Professional.
缓存连接的先决条件
连接到单个缓存
NCache 提供 ICache
获取实例的接口 NCache的缓存。 此外, CacheManager
类允许您通过 GetCache
方法。
以下示例连接到一个名为 demoCache 的缓存,该缓存处于运行状态。
// Specify the cache name
string cacheName = "demoCache";
// Connect to cache
ICache cache = CacheManager.GetCache(cacheName);
String cacheName = "demoCache";
// Connect to cache
cache = CacheManager.getCache(cacheName);
// Specify the cache name
let cacheName = "demoCache";
// Connect to cache
let cache = await ncache.CacheManager.getCache(cacheName);
# Specify the cache name
cache_name = "demoCache"
# Connect to cache
cache = ncache.CacheManager.get_cache(cache_name)
备注
为确保操作是故障安全的,建议处理应用程序中的任何潜在异常,如中所述 处理故障.
连接到多个缓存
使用 GetCache
方法,您可以连接到单个应用程序中的多个缓存。 这将帮助您使用一个应用程序管理多个缓存的数据。
以下示例从同一客户端连接到两个缓存:demoCache 和 demoClusteredCache。
// Specify cache names
string cacheName1 = "demoCache";
string cacheName2 = "demoClusteredCache";
// Connect to the caches
ICache cache1 = CacheManager.GetCache(cacheName1);
ICache cache2 = CacheManager.GetCache(cacheName2);
String cacheName1 = "demoCache";
String cacheName2 = "demoClusteredCache";
// Connect to the caches
Cache cache1 = CacheManager.getCache(cacheName1);
Cache cache2 = CacheManager.getCache(cacheName2);
// Specify cache names
let cacheName1 = "demoCache";
let cacheName2 = "demoClusteredCache";
// Connect to the caches
let cache1 = await ncache.CacheManager.getCache(cacheName1);
let cache2 = await ncache.CacheManager.getCache(cacheName2);
# Specify cache names
cache_name_1 = "demoCache"
cache_name_2 = "demoClusteredCache"
# Connect to the caches
cache_1 = ncache.CacheManager.get_cache(cache_name_1)
cache_2 = ncache.CacheManager.get_cache(cache_name_2)
使用 CacheConnectionOptions 连接到缓存
CacheConnectionOptions
允许在建立到缓存的连接时指定缓存属性的值。 这些值是相同的,可以通过 客户端.ncconf 文件中。
备注
通过指定的任何配置 CacheConnectionOptions
将覆盖中的值 客户端.ncconf 对于那个特定的客户。
在此示例中,值 RetryInterval
, ConnectionRetries
, EnableKeepAlive
及 KeepAliveInterval
属性可以改变。 对于此应用程序,将使用这些值而不是客户端配置文件中指定的值。
// Create new CacheConnectionOptions instance
var options = new CacheConnectionOptions();
// Specify the cache connection options to be set
options.RetryInterval = TimeSpan.FromSeconds(5);
options.ConnectionRetries = 2;
options.EnableKeepAlive = true;
options.KeepAliveInterval = TimeSpan.FromSeconds(30);
// Specify the cache name
string cacheName = "demoCache";
// Connect to cache with CacheConnectionOptions
ICache cache = CacheManager.GetCache(cacheName, options);
// Create new CacheConnectionOptions instance
CacheConnectionOptions options = new CacheConnectionOptions();
// Specify the cache connection options to be set
TimeSpan retryInterval = new TimeSpan(0, 0, 5);
options.setRetryInterval(retryInterval);
options.setConnectionRetries(2);
options.setEnableClientLogs(true);
TimeSpan keepAliveInterval = new TimeSpan(0, 0, 30);
options.setKeepAliveInterval(keepAliveInterval);
// Specify the cache name
String cacheName = "democache";
// Connect to cache with CacheConnectionOptions
Cache cache = CacheManager.getCache(cacheName, options);
// Create new CacheConnectionOptions instance
let options = new ncache.CacheConnectionOptions();
// Specify the cache connection options to be set
let retryInterval = new ncache.TimeSpan(0, 0, 5);
options.setRetryInterval(retryInterval);
options.setConnectionRetries(2);
options.setEnableClientLogs(true);
let keepAliveInterval = new ncache.TimeSpan(0, 0, 30);
options.setKeepAliveInterval(keepAliveInterval);
// Specify the cache name
let cacheName = "demoCache";
// Connect to cache with CacheConnectionOptions
let cache = await ncache.CacheManager.getCache(cacheName, options);
# Create new CacheConnectionOptions instance
options = ncache.CacheConnectionOptions()
# Specify the cache connection options to be set
retry_interval = ncache.TimeSpan(0, 0, 5)
keep_alive_interval = ncache.TimeSpan(0, 0, 30)
options.set_retry_interval(retry_interval)
options.set_connection_retries(2)
options.set_enable_client_logs(True)
options.set_keep_alive_interval(keep_alive_interval)
# Specify the cache name
cache_name = "demoCache"
# Connect to cache with CacheConnectionOptions
cache = ncache.CacheManager.get_cache(cache_name, options)
使用负载均衡器的 CacheConnectionOptions 连接到缓存
通常,应用程序部署使用负载平衡器来防止来自压倒性服务器的请求,即防止客户端直接访问服务器。在这种情况下,为了确保您的集群连接到所有服务器, NCache 提供 IsLoadBalancer
API,如下所示:
CacheConnectionOptions cacheConnectionOptions = new CacheConnectionOptions();
cacheConnectionOptions.ServerList = new List<ServerInfo>()
{
new ServerInfo("20.200.20.40", 9800, true)
};
ICache cache = CacheManager.GetCache("demoCache", cacheConnectionOptions);
备注
了解更多有关如何 NCache 处理这些负载均衡器 此处.
连接到集群和客户端缓存
备注
使用 GetCache
不建议使用在单个调用中连接到集群和客户端缓存的方法。
可以使用以下命令在一次调用中连接到集群和客户端缓存: GetCache
方法。 这将在单个应用程序中初始化两个缓存,从而帮助您通过一个应用程序管理多个缓存的数据。
备注
如果本地缓存连接到客户端缓存,则序列化必须与集群缓存相同。 两个缓存都必须事先配置并且必须处于运行状态。
以下示例使用以下命令连接到两个缓存 GetCache
方法。
// Specify the cache names
string clusteredCache = "demoCache";
string clientCache = "myClientCache";
CacheConnectionOptions cacheConnectionOptions = new CacheConnectionOptions();
cacheConnectionOptions.LoadBalance = true;
cacheConnectionOptions.ConnectionRetries = 5;
CacheConnectionOptions clientCacheConnectionOptions = new CacheConnectionOptions();
clientCacheConnectionOptions.LoadBalance = true;
clientCacheConnectionOptions.ConnectionRetries = 5;
clientCacheConnectionOptions.Mode = IsolationLevel.OutProc;
// Connect to the caches in a single call
// CacheConnectionOptions which can be null if not required
ICache cache = CacheManager.GetCache(clusteredCache, cacheConnectionOptions, clientCache, clientCacheConnectionOptions);
// Specify cache names
String clusteredCache = "demoCache";
String clientCache = "myClientCache";
// Create and set CacheConnectionOptions for clustered cache
CacheConnectionOptions cacheConnectionOptions = new CacheConnectionOptions();
cacheConnectionOptions.setLoadBalance(true);
cacheConnectionOptions.setConnectionRetries(5);
// Create and set CacheConnectionOptions for Client Cache
CacheConnectionOptions clientCacheConnectionOptions = new CacheConnectionOptions();
clientCacheConnectionOptions.setLoadBalance(true);
clientCacheConnectionOptions.setConnectionRetries(5);
clientCacheConnectionOptions.setIsolationLevel(IsolationLevel.OutProc);
// Connect to caches in single call
// CacheConnectionOptions which can be null if not required
Cache cache = CacheManager.getCache(clusteredCache, cacheConnectionOptions, clientCache, clientCacheConnectionOptions);
// Specify cache names
let clusteredCache = "demoCache";
let clientCache = "myClientCache";
// Create and set CacheConnectionOptions for clustered cache
let cacheConnectionOptions = new ncache.CacheConnectionOptions();
cacheConnectionOptions.setLoadBalance(true);
cacheConnectionOptions.setConnectionRetries(5);
// Create and set CacheConnectionOptions for Client Cache
let clientCacheConnectionOptions = new ncache.CacheConnectionOptions();
clientCacheConnectionOptions.setLoadBalance(true);
clientCacheConnectionOptions.setConnectionRetries(5);
clientCacheConnectionOptions.setIsolationLevel(ncache.IsolationLevel.OutProc);
// Connect to caches in single call
// CacheConnectionOptions which can be null if not required
let cache = await ncache.CacheManager.getCache(clusteredCache, cacheConnectionOptions, clientCache, clientCacheConnectionOptions);
# Specify cache names
clustered_cache = "demoCache"
client_cache = "myClientCache"
# Create and set CacheConnectionOptions for clustered cache
cache_connection_options = ncache.CacheConnectionOptions()
cache_connection_options.set_load_balance(True)
cache_connection_options.set_connection_retries(5)
# Create and set CacheConnectionOptions for Client Cache
client_cache_connection_options = ncache.CacheConnectionOptions()
client_cache_connection_options.set_load_balance(True)
client_cache_connection_options.set_connection_retries(5)
client_cache_connection_options.set_isolation_level(ncache.IsolationLevel.OUT_PROC)
# Connect to both of the caches in a single call
# CacheConnectionOptions can be null if not required
cache = ncache.CacheManager.get_cache(clustered_cache, cache_connection_options, client_cache, client_cache_connection_options)
使用安全凭证连接到缓存
如果启用了安全性,您需要在连接到缓存时提供安全凭证,以便授权用户可以执行操作。 有关使用安全性的更多详细信息 NCache,请参阅 NCache 安全。
// Specify cache name and user credentials
string cacheName = "demoCache";
string userId = "userid";
string password = "mypassword";
// Initialize the CacheConnectionOptions
var options = new CacheConnectionOptions();
// Enter the credentials
options.UserCredentials = new Credentials(userId, password);
// Connect to the cache using the security credentials
ICache cache = CacheManager.GetCache(cacheName, options);
// Initialize credentials
String userId = "UserId";
String password = "UserPassword";
String cacheName = "demoCache";
// Use CacheConnectionOptions to provide credentials
CacheConnectionOptions options = new CacheConnectionOptions();
// Use UserCredentials property to assign credentials
Credentials credentials = new Credentials(userId, password);
options.setUserCredentials(credentials);
// Connecting to cache with security credentials
Cache cache = CacheManager.getCache(cacheName, options);
// Initialize credentials
let userId = "UserId";
let password = "UserPassword";
let cacheName = "demoCache";
// Use CacheConnectionOptions to provide credentials
let options = new ncache.CacheConnectionOptions();
// Use UserCredentials property to assign credentials
let credentials = new ncache.Credentials(userId, password);
options.setUserCredentials(credentials);
// Connecting to cache with security credentials
let cache = await ncache.CacheManager.getCache(cacheName, options);
# Initialize credentials
user_id = "UserId"
password = "UserPassword"
cache_name = "demoCache"
# Use CacheConnectionOptions to provide credentials
options = ncache.CacheConnectionOptions()
# Use UserCredentials property to assign credentials
credentials = ncache.Credentials(user_id, password) options.set_user_credentials(credentials)
# Connecting to cache with security credentials
cache = ncache.CacheManager.get_cache(cache_name, options)
故障排除
端口不可访问
有时,由于您的防火墙配置,客户端可能无法连接到缓存。
解决方法
使用我们的详细信息 入门指南 更改您的防火墙设置。
无法找到 client.ncconf
有时客户可能无法找到 客户端.ncconf 连接到缓存时的文件。 发生这种情况时 NCache 目录(%NCHOME%
) 未设置。
解决方法
If %NCHOME%
未设置,请按照以下步骤为其设置环境变量:
- 用鼠标右键单击 Start 开始 按钮并选择 系统.
- 在右侧窗格中,单击 高级系统设置.
- 在 系统属性 窗口,转到 高级 标签并点击 环境变量 按钮。
- 在 环境变量 窗下,下 系统变量, 点击 全新.
- 针对 变量名称 进入
%NCHOME%
.
- 针对 变量值 进入
C:\Program Files\NCache
.
- 点击 OK.
- 现在更新系统路径相同 环境变量 窗口中,找到 途径 变量并单击 编辑.
- 在 编辑环境变量 窗口中,单击 全新 并添加
%NCHOME%\bin
到列表中。 这确保了这些工具在全球范围内可用。
- 点击 OK 关闭每个窗口。
没有可用于处理请求的服务器
有时,客户端可能无法连接到缓存,因为客户端/服务器端口被防火墙阻止。
解决方法
确保客户端/服务器端口(在哪个 NCache 服务启动并接受不同的客户端连接)不会被防火墙阻止。 该端口的默认值为“9800”。 您可以更改默认值 Alachisoft.NCache.Service.exe.config or Alachisoft.NCache.Service.dll.config 文件中。
无法与服务器节点通信
由于群集端口被防火墙阻止,群集节点也可能无法相互通信。
Error: "Unable to communicate with server node"
解决方法
查看您的防火墙是否允许客户端/服务器和集群端口。 集群端口是集群中的节点进行通信的端口。 您可以更改这些端口的默认值 NCache 管理中心。 如果您在中指定了端口范围 NCache 管理中心,解除对该范围内所有端口的封锁。
客户端套接字死锁
由于长时间等待来自缓存服务器的响应,您可能会在客户端套接字上遇到死锁情况。 这也可能导致大量等待线程,从而导致性能问题。
解决方法
Step1:服务器端修改
进入服务配置文件:
- 视窗 .NET 4.8: Alachisoft.NCache.Service.exe.config 位于 %NCHOME%\bin\服务
- 视窗.NET: Alachisoft.NCache.Service.dll.config 位于 %NCHOME%\bin\服务
- Linux.NET: Alachisoft.NCache.Daemon.dll.config 位于 /选择/ncache/bin/服务
- 视窗Java: Alachisoft.NCache.Service.dll.config 位于 %NCHOME%\bin\服务
- Linux Java: Alachisoft.NCache.Daemon.dll.config 位于 /选择/ncache/bin/服务
在配置文件中添加以下行:
<add key ="NCacheServer.EnableBadClientDetection" value = "true" />
值“true”表示在您的客户端套接字上启用了 BadClientDetection。 这意味着现在如果您的客户端套接字出现死锁情况,套接字将被重置。 此属性的默认值为“false”。
<add key ="NCacheServer.ClientSocketSendTimeout" value = "10" />
当您启用 BadClientDetection
,您可以指定在死锁的情况下重置套接字的时间间隔。 该属性的默认值为“10”,该值不能小于 1。
还引入了一些其他属性 Alachisoft.NCache.Service.exe.config 帮助解决这个问题。 请按如下方式配置这些属性:
<add key ="NCacheServer.EventPriorityRatio" value="30"/>
在服务器端,优先级队列用于事件和缓存操作。 您可以通过名为的属性配置此队列的事件和缓存操作的比率 EventPriorityRatio
。 该属性的默认值为 30,表示事件和缓存操作的比率为 30:70。 该属性的值不能小于 1。
<add key ="NCacheServer.EventBulkCount" value="50"/>
缓存服务器现在批量向客户端发送事件。 通过该属性,您可以设置批量发送的项目数量。 默认情况下,该值为 50,并且该值不能小于 1。此功能在客户端版本 4124 及以上版本中可用。
<add key ="NCacheServer.EventBulkCollectionInterval" value="2"/>
此属性用于设置时间间隔(以秒为单位),在此时间间隔后,即使批量计数未达到指定值,也会触发批量事件。 EventBulkCount
用户指定的。 该属性的默认值为 2 秒,并且该值不能小于 1。
重新启动 NCache 服务以使这些更改生效。
Step2:客户端的变化
请在您的 应用配置/网页配置 文件:
<configuration>
<appSettings>
<add key ="NCacheClient.AsynchronousEventNotification" value="true"/>
<add key ="NCacheClient.NumberofEventProccesingThreads" value="2"/>
</appSettings>
</configuration>
AsynchronousEventNotification
:指定事件是在客户端异步还是同步触发。 默认值为“true”,表示事件将异步触发。 值“false”表示事件将在客户端同步触发。
NumberofEventProcessingThreads
:当用户配置要同步触发的事件时,此标志用于指定将在客户端处理这些事件的线程数。 非常大的值可能会导致性能问题。
参见
.NET: Alachisoft.NCache。客户 命名空间。
Java的: COM。alachisoft.ncache。客户 命名空间。
节点.js: 缓存 类。
Python: ncache。客户 类。