Solved

Binary Serialization memcache C# .NET

Posted on 2014-03-22
2
1,325 Views
Last Modified: 2014-03-24
Hi All,

I am really struggling here. I have memcached installed as a background service. What I am trying to do is put a binary serialised object into memcache. Then retrieve it from memcached and then deserialise it. Does anyone have any idea on the best approach for this.

I can do this with a string. Just not a serialised object. Have had a good search. Here is an example of the C# Console Application I am trying to build it in.
using (MemcachedClient writeClient = new MemcachedClient())
            {
                
                writeClient.FlushAll();
                string myVal = "hello world";
        
                using (var ms = new MemoryStream())
                {
                    BinaryFormatter my = new BinaryFormatter();
                    my.Serialize(ms, myVal);
                    
                    writeClient.Store(StoreMode.Set, "obj1", my);
                    my = writeClient.Get<BinaryFormatter>("obj1");
                }              
            }

Open in new window



The .api I am using is ...
using Enyim.Caching.Memcached;
using log4net;
using MemcachedProviders;

Thank you for any help.
0
Comment
Question by:meetsys
2 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39948853
You would need to use a Get operation for the same data type that you use the Store.  A MemoryStream is a byte array, and a BinaryFormatter is a byte array, so I would try that with the Get, and then use a BinaryFormatter to deserialize the byte array.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39949000
I think what TheLearnedOne is trying to say is that you need to be storing (and retrieving) the bytes that are stored in the MemoryStream, not the BinaryFormatter. A BF is just a utility to execute the conversion of serialized data--it does not actually store the data. The thing you write to--a MemoryStream in this case--has the actual data. I would tweak your code thus:

using (MemcachedClient writeClient = new MemcachedClient())
{

    writeClient.FlushAll();
    string myVal = "hello world";

    using (var ms = new MemoryStream())
    {
        BinaryFormatter my = new BinaryFormatter();
        my.Serialize(ms, myVal);
        ms.Position = 0;

        writeClient.Store(StoreMode.Set, "obj1", ms.ToArray());
        byte[] data = writeClient.Get<byte[]>("obj1");
        my = (string)my.Deserialize(data);
    }              
}

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question