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

如何在C#与Node.js客户端中使用read/write-through 求相关代码示例

Apache Ignite Read/Write-Through 客户端使用说明

Read-Through指客户端读取缓存中不存在的key时,Ignite服务端自动调用预先配置的外部存储接口拉取数据,写入缓存后再返回给客户端;Write-Through指客户端写入缓存时,服务端同步将数据写入绑定的外部存储,保证缓存和底层存储数据一致性。

这两个功能的核心配置都在Ignite服务端完成,你需要提前在服务端实现CacheStore接口、开启readThrough和writeThrough开关,客户端侧不需要额外开发存储交互逻辑,正常调用缓存读写API即可自动触发透传能力。

C# 客户端使用示例

首先安装NuGet依赖:Install-Package Apache.Ignite

using Apache.Ignite.Core;
using Apache.Ignite.Core.Cache;

// 连接Ignite集群
var ignite = Ignition.Start(new IgniteConfiguration
{
    Endpoints = new[] { "127.0.0.1:10800" } // 替换为实际集群地址
});

// 获取已配置Read/Write-Through的目标缓存
ICache<int, string> demoCache = ignite.GetCache<int, string>("demo_cache");

// 触发Read-Through:key不存在时服务端自动从外部存储拉取
string value = demoCache.Get(1);
Console.WriteLine($"读取到的值:{value}");

// 触发Write-Through:写入缓存的同时服务端同步写入外部存储
demoCache.Put(2, "test_value");
Node.js 客户端使用示例

首先安装npm依赖:npm install apache-ignite-client

const IgniteClient = require('apache-ignite-client');
const IgniteClientConfiguration = IgniteClient.IgniteClientConfiguration;

async function runReadWriteThroughDemo() {
    const igniteClient = new IgniteClient();
    try {
        // 连接Ignite集群
        await igniteClient.connect(new IgniteClientConfiguration('127.0.0.1:10800'));
        
        // 获取已配置Read/Write-Through的目标缓存
        const demoCache = igniteClient.getCache('demo_cache');
        
        // 触发Read-Through:key不存在时自动从外部存储拉取
        const value = await demoCache.get(1);
        console.log('读取到的值:', value);
        
        // 触发Write-Through:同步写入外部存储
        await demoCache.put(2, 'test_value');
    } catch (err) {
        console.log('操作失败:', err.message);
    } finally {
        igniteClient.disconnect();
    }
}

runReadWriteThroughDemo();
注意事项
  • 透传过程中如果外部存储读写失败,客户端API会抛出对应异常,你可以捕获CacheException(C#)或者普通Error对象(Node.js)获取错误详情
  • 如果服务端同时开启了Write-Behind配置,写操作会变成异步刷入外部存储,不会阻塞客户端调用,和Write-Through的同步写入逻辑不同
  • 所有CacheStore的实现逻辑都在服务端执行,客户端不需要实现任何存储相关接口,只需要保证操作的缓存已经在服务端配置了透传开关即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 16:06:00