开发者

Cannot store a complex object in memcached

I'm working with Enyim.Caching the memcached client for C# the server is http://memcached.org on the last version of ubuntu

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

and myXml is null, why is there a way to Store my object. The purpose : I'm trying to replace HttpCache in my code for Memcached but with HttpCache I can add complex object to the cache.

Here XmlDocument is an example but with a simple class it doesn'开发者_开发问答t work too


In order for classes to be used with Memcached, they need to support binary serialization, this allows objects to be converted to a flat byte data-representation and then transmitted to and from the Memcached server.

In your example you use XmlDocument, which is not binary serializable. You can work around this by converting it to and from string which is binary serializable:

    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);

For your own custom classes, you need to add the [Serializable] attribute and follow guidelines for binary serialization: SerializableAttribute Class.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜