Solved

Binary Serialization memcache C# .NET

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
print bytes of an integer 6 26
Problem to copy file 14 52
global Variable - 2 functions in powershell 1 21
.Net remove carriage returns, line feeds and tabbs 5 14
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.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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…

733 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