You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET瘦客户端动态配置ReadThrough/WriteThrough并创建多缓存

.NET环境下动态创建带ReadThrough/WriteThrough属性的Ignite缓存(无需重启服务)

Ignite瘦客户端由于轻量架构的限制,确实无法直接配置并创建带有ReadThrough、WriteThrough这类依赖服务器端组件的缓存——这类属性需要关联服务器端的ICacheStore实现,而瘦客户端无法将Store逻辑部署到服务器节点,也无法直接操作服务器端的缓存配置。以下是两种可行的实现方案:

方案一:使用.NET厚客户端(嵌入节点)直接创建

厚客户端本质是加入集群的完整Ignite节点,拥有与服务器节点一致的API权限,可以直接动态创建符合要求的缓存:

  1. 实现服务器端可访问的CacheStore
    编写自定义的ICacheStore实现类,确保该类的程序集同时部署到Ignite服务器节点的应用目录(或通过服务器节点的类加载路径加载):

    public class CustomCacheStore : ICacheStore<int, string>
    {
        public void LoadCache(Action<int, string> act, params object[] args)
        {
            // 实现批量加载缓存逻辑
        }
    
        public string Load(int key)
        {
            // 实现从数据源读取单条数据逻辑
            return $"Persisted data for key {key}";
        }
    
        public void Write(int key, string val)
        {
            // 实现将数据写入持久化数据源逻辑
        }
    
        public void Delete(int key)
        {
            // 实现从持久化数据源删除数据逻辑
        }
    
        public void SessionEnd(bool commit) { }
    }
    
  2. 通过厚客户端创建缓存
    在.NET程序中初始化厚客户端,然后传入配置了ReadThrough、WriteThrough属性的CacheConfiguration创建缓存:

    // 初始化厚客户端配置
    var igniteCfg = new IgniteConfiguration
    {
        ClientMode = true,
        DiscoverySpi = new TcpDiscoverySpi
        {
            IpFinder = new TcpDiscoveryStaticIpFinder
            {
                Endpoints = new[] { "127.0.0.1:47500..47509" }
            }
        }
    };
    
    using (var ignite = Ignition.Start(igniteCfg))
    {
        // 构建目标缓存的配置
        var cacheCfg = new CacheConfiguration("UserReadWriteCache")
        {
            ReadThrough = true,
            WriteThrough = true,
            CacheStoreFactory = new Factory<ICacheStore<int, string>>(() => new CustomCacheStore())
        };
    
        // 动态创建缓存,无需重启Ignite服务
        var cache = ignite.CreateCache<int, string>(cacheCfg);
    }
    

方案二:通过瘦客户端调用服务器端计算任务创建

如果必须使用瘦客户端,可以通过Ignite的Compute API提交一个服务器端执行的任务,在服务器上下文内完成缓存创建:

  1. 编写服务器端可执行的缓存创建任务
    创建实现IComputeFunc的任务类,确保该类的程序集部署到服务器节点:

    public class CreateCacheTask : IComputeFunc<bool>
    {
        private readonly CacheConfiguration _cacheConfig;
    
        public CreateCacheTask(CacheConfiguration cacheConfig)
        {
            _cacheConfig = cacheConfig;
        }
    
        public bool Invoke()
        {
            // 在服务器节点上下文执行缓存创建逻辑
            var ignite = Ignition.GetIgnite();
            ignite.CreateCache(_cacheConfig);
            return true;
        }
    }
    
  2. 瘦客户端提交任务
    在瘦客户端代码中,构建缓存配置并提交任务到服务器端执行:

    // 初始化瘦客户端配置
    var thinClientCfg = new IgniteConfiguration
    {
        ClientMode = true,
        ThinClientConfiguration = new ThinClientConfiguration
        {
            Servers = new[] { "127.0.0.1:10800" }
        }
    };
    
    using (var ignite = Ignition.Start(thinClientCfg))
    {
        // 构建缓存配置,StoreFactory指向服务器端可加载的类型
        var cacheCfg = new CacheConfiguration("ProductReadWriteCache")
        {
            ReadThrough = true,
            WriteThrough = true,
            CacheStoreFactory = new Factory<ICacheStore<int, string>>(() => 
                (ICacheStore<int, string>)Activator.CreateInstance(Type.GetType("YourNamespace.CustomCacheStore, YourAssemblyName")))
        };
    
        // 提交任务到服务器端执行缓存创建
        var compute = ignite.GetCompute();
        bool createSuccess = compute.Execute(new CreateCacheTask(cacheCfg));
    }
    

关键注意事项

  • Store类部署:所有ICacheStore实现类以及任务类必须部署到Ignite服务器节点的应用程序目录,确保服务器端可以加载这些类型。
  • 缓存复用:创建后的缓存会持久化在集群中,瘦客户端后续可以直接通过缓存名称获取使用,无需重复创建。
  • 多缓存创建:只需重复上述流程,传入不同的缓存名称和对应的CacheConfiguration,即可动态创建多个配置不同的缓存,全程无需重启Ignite服务。

内容的提问来源于stack exchange,提问作者zhouchen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 04:38:20