Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 354
Christophe Debove Avatar asked Aug 29 '11 09:08

Christophe Debove


People also ask

Can Memcached store objects?

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering. Memcached is simple yet powerful. Its simple design promotes quick deployment, ease of development, and solves many problems facing large data caches.

How does memcache store data?

How does Memcached work? Unlike databases that store data on disk or SSDs, Memcached keeps its data in memory. By eliminating the need to access disks, in-memory key-value stores such as Memcached avoid seek time delays and can access data in microseconds.

What happens when memcache is full?

First, when memcached gets full, it will start removing items from the cache using the Least Recently Used algorithm. Second, you can have multiple instances of memcached running, adding them gets complicated because it will change the hashing algorithm used to determine which cache the data is in.

Where does memcache store data?

Memcached stores the data in the memory and when the data is needed the application checks for the data in the memcache via its daemon. Memcached has the ability to store SQL queries, that way the next time the query is ran, it can return the result from memory.


1 Answers

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.

like image 160
Tim Lloyd Avatar answered Sep 30 '22 06:09

Tim Lloyd