2011-08-29 50 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/。 –