Jdesk / memcached

Automatically exported from code.google.com/p/memcached
0 stars 0 forks source link

Multi Set for Client in C# #114

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
/// <summary>
        /// This Method is resposible for serializing of a data .
        /// It does compression if required.
        /// </summary>
        /// <param name="obj">object to serialize. </param>
        /// <param name="key">key to save</param>
        /// <param name="val">serialized data in byte array</param>
        /// <param name="length">length in cache</param>
        /// <param name="flags">corresposnding falg after 
serialization.</param>
        /// <param name="asString"></param>
        /// <returns></returns>
        private bool SerializeData(object obj, string key, out byte[] 
val,out int length, out int flags, bool asString)
        {

            length = 0;
            flags = 0;
            val = new byte[0];
            if (NativeHandler.IsHandled(obj))
            {
                if (asString)
                {
                    if (obj != null)
                    {
                        if (log.IsInfoEnabled)
                        {
                            log.Info(GetLocalizedString("set store data as 
string").Replace("$$Key$$", key).Replace("$$Class$$", obj.GetType().Name));
                        }
                        try
                        {
                            val = UTF8Encoding.UTF8.GetBytes(obj.ToString
());
                            length = val.Length;
                        }
                        catch (ArgumentException ex)
                        {
                            if (log.IsErrorEnabled)
                            {
                                log.Error(GetLocalizedString("set invalid 
encoding").Replace("$$Encoding$$", _defaultEncoding), ex);
                            }
                            return false;
                        }
                    }
                    else
                    {
                        val = new byte[0];
                        length = 0;
                    }
                }
                else
                {
                    if (log.IsInfoEnabled)
                    {
                        log.Info(GetLocalizedString("set store with native 
handler"));
                    }

                    try
                    {
                        val = NativeHandler.Encode(obj);
                        length = val.Length;
                    }
                    catch (ArgumentException e)
                    {
                        if (log.IsErrorEnabled)
                        {
                            log.Error(GetLocalizedString("set failed to 
native handle object"), e);
                        }

                        return false;
                    }
                }
            }
            else
            {
                if (obj != null)
                {
                    if (log.IsInfoEnabled)
                    {
                        log.Info(GetLocalizedString("set 
serializing".Replace("$$Key$$", key).Replace("$$Class$$", obj.GetType
().Name)));
                    }

                    // always serialize for non-primitive types
                    try
                    {
                        MemoryStream memStream = new MemoryStream();
                        new BinaryFormatter().Serialize(memStream, obj);
                        val = memStream.GetBuffer();
                        length = (int)memStream.Length;
                        flags |= F_SERIALIZED;
                    }
                    catch (IOException e)
                    {
                        // if we fail to serialize, then
                        // we bail
                        if (log.IsErrorEnabled)
                        {
                            log.Error(GetLocalizedString("set failed to 
serialize").Replace("$$Object$$", obj.ToString()), e);
                        }

                        // return socket to pool and bail
                        return false;
                    }
                }
                else
                {
                    val = new byte[0];
                    length = 0;
                }
            }

            // now try to compress if we want to
            // and if the length is over the threshold 
            if (_compressEnable && length > _compressThreshold)
            {
                if (log.IsInfoEnabled)
                {
                    log.Info(GetLocalizedString("set trying to compress 
data"));
                    log.Info(GetLocalizedString("set size prior").Replace
("$$Size$$", length.ToString(new NumberFormatInfo())));
                }

                try
                {
                    MemoryStream memoryStream = new MemoryStream();
                    GZipOutputStream gos = new GZipOutputStream
(memoryStream);
                    gos.Write(val, 0, length);
                    gos.Finish();

                    // store it and set compression flag
                    val = memoryStream.GetBuffer();
                    length = (int)memoryStream.Length;
                    flags |= F_COMPRESSED;

                    if (log.IsInfoEnabled)
                    {
                        log.Info(GetLocalizedString("set compression 
success").Replace("$$Size$$", length.ToString(new NumberFormatInfo())));
                    }
                }
                catch (IOException e)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error(GetLocalizedString("set compression 
failure"), e);
                    }
                }
            }
            return true;
        }

        /// <summary>
        /// Stores data to cache.
        /// 
        /// If data does not already exist for this key on the server, or 
if the key is being
        /// deleted, the specified value will not be stored.
        /// The server will automatically delete the value when the 
expiration time has been reached.
        /// 
        /// If compression is enabled, and the data is longer than the 
compression threshold
        /// the data will be stored in compressed form.
        /// 
        /// As of the current release, all objects stored will use .NET 
serialization.
        /// </summary>
        /// <param name="cmdname">action to take (set, add, replace)
</param>
        /// <param name="keys">keys to store cache under</param>
        /// <param name="values">objects to cache</param>
        /// <param name="expiry">expiration</param>
        /// <param name="hashCode">if not null, then the int hashcode to 
use</param>
        /// <param name="asString">store this object as a string?</param>
        /// <returns>true/false indicating success</returns>
        private bool Set(string cmdname, string[] keys, object[] objs, 
DateTime expiry, object hashCode, bool asString)
        {
            if (expiry < DateTime.Now)
                return true;

            if (cmdname == null || cmdname.Trim().Length == 0 || keys == 
null || keys.Length == 0)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error(GetLocalizedString("set key null"));
                }
                return false;
            }

            // get SockIO obj
            SockIO sock = SockIOPool.GetInstance(_poolName).GetSock(keys
[0], hashCode);

            if (sock == null)
                return false;

            if (expiry == DateTime.MaxValue)
                expiry = new DateTime(0);

            // store flags
            int flags = 0;
            int keyCounter = 0;
            // byte array to hold data
            byte[] val;
            int length = 0;
            foreach (object obj in objs)
            {
                string key = keys[keyCounter++];
                if (!SerializeData(obj, key, out val,out length, out 
flags, asString))
                {
                    sock.Close();
                    sock = null;
                    return false;
                }

                string cmd = cmdname + " " + key + " " + flags + " "
                    + GetExpirationTime(expiry) + " " + length + "\r\n";

                sock.Write(UTF8Encoding.UTF8.GetBytes(cmd));
                sock.Write(val,0,length);
                sock.Write(UTF8Encoding.UTF8.GetBytes("\r\n"));
            }
            try
            {
                sock.Flush();
                // get result code
                string line = sock.ReadLine();
                if (log.IsInfoEnabled)
                {
                    log.Info(GetLocalizedString("set memcached command 
result").Replace("$$Cmd$$", "set").Replace("$$Line$$", line));
                }

                if (STORED == line)
                {
                    if (log.IsInfoEnabled)
                    {
                        log.Info(GetLocalizedString("set success").Replace
("$$Key$$", "Multi"));
                    }
                    sock.Close();
                    sock = null;
                    return true;
                }
                else if (NOTSTORED == line)
                {
                    if (log.IsInfoEnabled)
                    {
                        log.Info(GetLocalizedString("set not 
stored").Replace("$$Key$$", "Multi"));
                    }
                }
                else
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error(GetLocalizedString("set error").Replace
("$$Key$$", "Multi").Replace("$$Size$$", length.ToString(new 
NumberFormatInfo())).Replace("$$Line$$", line));
                    }
                }
            }
            catch (IOException e)
            {
                // exception thrown
                if (log.IsErrorEnabled)
                {
                    log.Error(GetLocalizedString("set IOException"), e);
                }

                try
                {
                    sock.TrueClose();
                }
                catch (IOException ioe)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error(GetLocalizedString("failed to close some 
socket").Replace("$$Socket$$", sock.ToString()), ioe);
                    }
                }

                sock = null;
            }
            if (sock != null)
                sock.Close();

            return false;
        }

Original issue reported on code.google.com by ratneshs...@gmail.com on 11 Dec 2009 at 11:36

GoogleCodeExporter commented 9 years ago
I'm not sure what all of this is, but it's not part of the memcached server.  
Perhaps
you meant to file a bug against whatever client it is that you're using.

Original comment by dsalli...@gmail.com on 11 Dec 2009 at 6:12