2011-08-29 54 views
5

一個複雜的對象,我與Enyim.Caching memcached的客戶端C#服務器的工作是http://memcached.org ubuntu上的最後一個版本不能存儲在memcached的

MemcachedClient mc = new MemcachedClient(); 
XmlDocument xmlDocument = new XmlDocument(); 
mc.Store(StoreMode.Set, "foo", xmlDocument); 
object myXml= mc.Get("foo"); 

和myXml爲空,爲什麼有辦法存儲我的對象。 目的: 我試圖用我的代碼替換HttpCache中的Memcached,但使用HttpCache,我可以將複雜對象添加到緩存中。

這裏XmlDocument的是一個例子,但用簡單的類它不工作太

+0

什麼是您使用的memcache版本?我建議你在Windows上使用[this](http://www.couchbase.com/products-and-services/memcached)。 – Dasun

+0

我在Ubuntu上和http://memcached.org一起工作,在更換我所有的服務器之前,您是否可以確認我的解決方案,我將能夠存儲complexe對象? –

+0

'XmlDocument'似乎不是二進制的[[Serializable]]。使用XML的字符串表示形式,即「xmlDocument.OuterXml」。 –

回答

9

爲了與Memcached的使用類,它們需要支持二進制序列,這允許對象被轉換爲平面然後將數據傳輸到Memcached服務器。

在您的示例中,您使用的是XmlDocument,它不是二進制可序列化的。您可以變通的作法是將其轉換爲從string二進制序列化:

MemcachedClient mc = new MemcachedClient(); 
    XmlDocument xmlDocument = new XmlDocument(); 
    mc.Store(StoreMode.Set, "foo", xmlDocument.OuterXml); 
    string myXml = mc.Get("foo"); 
    XmlDocument xmlDocumentOut = new XmlDocument(); 
    xmlDocumentOut.LoadXml(myXml); 

對於自己的自定義類,你需要添加[Serializable]屬性,並按照二進制序列化準則:SerializableAttribute Class

+0

我不能添加XmlDocument的內存打印?因爲,這個想法是爲了避免序列化/反序列化,這需要花費處理時間 –

+2

@Christophe您無法避免序列化,因爲這是必需的,因此您的「對象」可以傳輸到Memcached服務器。對象圖需要轉換爲面向字節的扁平數據流。如果你需要使這個過程更有效率,我會考慮使用protobuf.net,它比.Net的二進制序列化更有效 - 更快,更少的帶寬。你仍然會遇到'XmlDocument'問題,因爲它不是二進制序列化的。創建你自己的班級,並遵循protobuf.net的指導方針。 –

+0

@Christophe Protobuf.net:http://code.google.com/p/protobuf-net/。 –