Solved

Binary Serialization memcache C# .NET

Posted on 2014-03-22
2
1,374 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Six Sigma Control Plans

688 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