1.2. 配置和远程缓存管理器 API


使用 ConfigurationBuilder API 配置 Hot Rod .NET/C# 客户端连接和 RemoteCacheManager API,以获取和配置远程缓存。

基本配置

using Infinispan.HotRod;
using Infinispan.HotRod.Config;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace simpleapp
{
    class Program
    {
        static void Main(string[] args)
        {
            ConfigurationBuilder builder = new ConfigurationBuilder();
            // Connect to a server at localhost with the default port.
            builder.AddServer()
                .Host(args.Length > 1 ? args[0] : "127.0.0.1")
                .Port(args.Length > 2 ? int.Parse(args[1]) : 11222);
            Configuration config = builder.Build();
            // Create and start a RemoteCacheManager to interact with caches.
            RemoteCacheManager remoteManager = new RemoteCacheManager(conf);
            remoteManager.Start();
            IRemoteCache<string,string> cache=remoteManager.GetCache<string, string>();
            cache.Put("key", "value");
            Console.WriteLine("key = {0}", cache.Get("key"));
            remoteManager.Stop();
        }
    }
}
Copy to Clipboard Toggle word wrap

身份验证

ConfigurationBuilder builder = new ConfigurationBuilder();
// Add a server with specific connection timeouts
builder.AddServer().Host("127.0.0.1").Port(11222).ConnectionTimeout(90000).SocketTimeout(900);
// ConfigurationBuilder has fluent interface, options can be appended in chain.
// Enabling authentication with server name "node0",
// sasl mech "PLAIN", user "supervisor", password "aPassword", security realm "aRealm"
builder.Security().Authentication().Enable().ServerFQDN("node0")
    .SaslMechanism("PLAIN").SetupCallback("supervisor", "aPassword", "aRealm");
Configuration c = conf.Build();
Copy to Clipboard Toggle word wrap

Encryption

ConfigurationBuilder builder = new ConfigurationBuilder();
builder.AddServer().Host("127.0.0.1").Port(11222);
// Get configuration builder for encryption
SslConfigurationBuilder sslBuilder = conf.Ssl();
// Enable encryption and provide client certificate
sslBuilder.Enable().ClientCertificateFile("clientCertFilename");
// Provide server cert if server needs to be verified
sslBuilder.ServerCAFile("serverCertFilename");
Configuration c = conf.Build();
Copy to Clipboard Toggle word wrap

跨站点故障切换

ConfigurationBuilder builder = new ConfigurationBuilder();
  builder.AddServer().Host("127.0.0.1").Port(11222);
  // Configure a remote cluster and node when using cross-site failover.
  builder.AddCluster("nyc").AddClusterNode("192.0.2.0", 11322);
Copy to Clipboard Toggle word wrap

接近缓存

ConfigurationBuilder builder = new ConfigurationBuilder();
  builder.AddServer().Host("127.0.0.1").Port(11222);
  // Enable near-caching for the client.
  builder.NearCache().Mode(NearCacheMode.INVALIDATED).MaxEntries(10);
Copy to Clipboard Toggle word wrap

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat