将Memcached复制到多个服务器

时间:2010-06-26 21:48:10

标签: c# asp.net-mvc 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机制来共享数据以实现冗余。有没有人有任何见解?

1 个答案:

答案 0 :(得分:1)

理论上有两种方法可以做到:

有一种名为repcached的产品,但它现在只在linux上运行。它能够将缓存复制到不同的服务器。

另一个选项是代码:您可以将值写入BOTH缓存并在两个列中检查它。如果添加的服务器数量超过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;
    }