在Windows10上使用C#,ApacheIgnite2.4,我的目标是拥有多个具有自定义CacheStoreFactory的复制节点。每当缓存项更改时,我都希望调用每个节点的CacheStoreFactory。下面的代码示例将输出到我预期的CacheMode.Partitioned行为。对于CacheMode.Replicated,我希望每个cache.Put操作都有两个输出行(因为两个节点正在运行)。我怎样才能做到这一点?
输出
Node count: 2
Started..
Write 0-0 by 63642613
Write 1-1 by 12036987
Write 2-2 by 63642613
Write 3-3 by 12036987
Write 4-4 by 63642613
Write 5-5 by 63642613
Write 6-6 by 12036987
Write 7-7 by 63642613
Write 8-8 by 12036987
Write 9-9 by 12036987
Finished count1:10 count2:10样本代码
class Program
{
static void Main(string[] args)
{
var config = new IgniteConfiguration()
{
IgniteInstanceName = "A",
CacheConfiguration = new List<CacheConfiguration>
{
new CacheConfiguration("mycache",new QueryEntity(typeof(int), typeof(int)))
{
CacheMode = CacheMode.Replicated,
WriteThrough = true,
CacheStoreFactory = new StoreFactory(),
},
},
};
var ignite1 = Ignition.Start(config);
config.IgniteInstanceName = "B";
var ignite2 = Ignition.Start(config);
Console.WriteLine("Node count: " + ignite1.GetCluster().GetNodes().Count);
ignite1.GetCluster().SetActive(true);
var mycache1 = ignite1.GetCache<int, int>("mycache");
var mycache2 = ignite2.GetCache<int, int>("mycache");
Console.WriteLine("Started..");
for (int i = 0; i < 10; i++)
{
mycache1.Put(i, i);
}
Console.WriteLine($"Finished count1:{mycache1.Count()} count2:{mycache2.Count()}");
Console.ReadLine();
ignite1.Dispose();
ignite2.Dispose();
}
private class StoreFactory : IFactory<ICacheStore>
{
public ICacheStore CreateInstance() { return new Store(); }
}
private class Store : CacheStoreAdapter<int, int>
{
public override void Delete(int key) { }
public override int Load(int key) { return 1; }
public override void Write(int key, int val)
{
Console.WriteLine($"Write {key}-{val} by {this.GetHashCode()}");
}
}
}发布于 2018-04-26 13:35:50
即使使用复制的缓存,每个键也有自己的主节点,只有这个主节点才会将值写入存储区。need期望所有节点都具有相同的底层CacheStore,在这种情况下,只向其写入值一次是有意义的,因为不需要从备份再次写入它。
如果您想拥有分布式底层存储,我建议您尝试Ignite本地持久性:https://apacheignite.readme.io/docs/distributed-persistent-store
https://stackoverflow.com/questions/50036192
复制相似问题