?
Solved

Binary Serialization memcache C# .NET

Posted on 2014-03-22
2
Medium Priority
?
1,479 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Progress
Introduction to Processes
Suggested Courses

649 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