Solved

Binary Serialization memcache C# .NET

Posted on 2014-03-22
2
1,292 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

3 Use Cases for Connected Systems

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

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

911 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now