5

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

Christophe Debove
  • 6,088
  • 20
  • 73
  • 124
  • what is the memcache build you use? I recommend you to use [this](http://www.couchbase.com/products-and-services/memcached) on windows. – Dasun Aug 29 '11 at 11:17
  • I working with http://memcached.org on ubuntu and Before I change all my server can you confirm me that with your solution I will be able to store complexe object? – Christophe Debove Aug 29 '11 at 12:40
  • `XmlDocument` does not appear to be binary `[Serializable]`. Use the string representation of the XML instead i.e. `xmlDocument.OuterXml`. – Tim Lloyd Aug 29 '11 at 12:44
  • @chibacity however XmlDocument is not binary serialisable I tried with another custom class it doesn't work too, if I try with String or int... it work – Christophe Debove Aug 29 '11 at 12:48
  • @Christophe I know `XmlDocument` is not binary serializable, hence my comment. I suggested `string` becuase that is binary serializable. Your classes must support `[Serializable]` to work. I suggest string for this as you can convert back to `XmlDocument`. For serializing your custom class, see: http://msdn.microsoft.com/en-us/library/system.serializableattribute.aspx. – Tim Lloyd Aug 29 '11 at 12:52
  • @chibacity ok all right I arrive to serialize my object but how HTTPCache can cache XmlDocument if it is not serializable? – Christophe Debove Aug 29 '11 at 12:57
  • @Christophe The HTTPCache is in-process, therefore objects do not need to be transmitted over the network. Whereas Memcached is a server application, so you need to flatten your objects to bytes (i.e. serialize) for transmission to and from the server. – Tim Lloyd Aug 29 '11 at 13:03
  • I'm ok with that but my objects are bytes in RAM so there are already binary "serialised" the idea is to retreive byte array in RAM to build a new instance of my class – Christophe Debove Aug 29 '11 at 13:06
  • @Christophe They are not binary serialized - serialized hints that they are a sequence of bytes. Your objects are in fact splattered all over the heap\memory, and joined up with pointers. For transmission over the network the object needs to be a byte[] array. Your object at runtime is not organized like this. It will also have pointers to data structures such as tables of type information, which may point to non-existent memory location if they were to come back later. Serialization is about flattening all of this, and removing memory references, etc. – Tim Lloyd Aug 29 '11 at 13:14

1 Answers1

9

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.

Tim Lloyd
  • 37,954
  • 10
  • 100
  • 130
  • I can't add the memory print of the XmlDocument? because, the idea is to avoid serialisation/deserialisation that cost proc time – Christophe Debove Aug 29 '11 at 13:04
  • 2
    @Christophe You cannot avoid serialization, as this is required so that your "object" can be transmitted to the Memcached server. The object graph needs to be converted to a flat byte oriented data stream. If you need to make this process more efficient, I would look at using protobuf.net which is much more efficient than .Net's binary serialization - faster and less bandwidth. You're still going to have problems with `XmlDocument` as it is not binary serializable. Create your own class and follow protobuf.net's guidelines. – Tim Lloyd Aug 29 '11 at 13:08
  • @Christophe Protobuf.net is a very efficient **binary** serializer, json is **text**. Json is still less verbose than xml, but Protobuf is very quick and produces much smaller payloads. – Tim Lloyd Aug 29 '11 at 13:25
  • Looks like memcached requires Serializable to be on custom classes being saved into the cache, thanks! – RandomUs1r Oct 31 '17 at 17:08