2010-06-26 78 views
0

我有以下代码:Memcached的复制到多台服务器

private bool CheckForDuplicatesInCache(Message theMessage) 
{ 
    var cacheClient = MemcachedClient.GetInstance("IntegrationCache"); 
    var messageHash = theMessage.GetHashCode(); 
    if (cacheClient.Get(messageHash.ToString()).IsNotNull()) 
    { 
     IntegrationContext.WriteLog("Warning: This message is a duplicate. Will Not Process."); 
     return false; 
    }   

    cacheClient.Set(messageHash.ToString(), "nothing", new TimeSpan(2, 0, 0)); 

    return true; 
} 

的问题是,当我这个扩展到多台服务器...我需要有多个memcached的intances共享数据冗余。有没有人有任何见解?

回答

1

有2种方式理论上你就可以做到这一点:

有一个叫repcached产品,但它只能运行在Linux上现在。它有能力将缓存复制到不同的服务器。

其他选项在代码中:您可以将值写入两个缓存并在两个chaches中检查它。如果你添加了2台以上的服务器,你可以在for循环中进行。

 private bool CheckForDuplicatesInCache(Message theMessage) 
    { 
     var cacheClient = MemcachedClient.GetInstance("IntegrationCache"); 
     var cacheClient2 = MemcachedClient.GetInstance("IntegrationCache2"); 
     var messageHash = theMessage.GetHashCode(); 
     if (cacheClient.Get(messageHash.ToString()).IsNotNull()) 
     { 
      IntegrationContext.WriteLog("Warning: This message is a duplicate. Will Not Process."); 
      return false; 
     } 

     if (cacheClient2.Get(messageHash.ToString()).IsNotNull()) 
     { 
      IntegrationContext.WriteLog("Warning: This message is a duplicate. Will Not Process."); 
      return false; 
     } 

     cacheClient.Set(messageHash.ToString(), "nothing", new TimeSpan(2, 0, 0)); 
     cacheClient2.Set(messageHash.ToString(), "nothing", new TimeSpan(2, 0, 0)); 
     return true; 
    } 
+0

我喜欢那样......看起来像是一种黑客,我会试一试。 – 2010-06-26 22:04:36

相关问题