コード例 #1
0
        /// <summary>
        /// 获取对象,不想装箱与拆箱
        /// </summary>
        public override bool TryGetResult <T>(string key, out T value)
        {
            value = default(T);
            if (string.IsNullOrEmpty(key))
            {
                return(false);
            }

            var hv   = this.SumASCII(key);
            var sort = this.SortServer(hv, this.connectionPools.Length);

            foreach (var s in sort)
            {
                if (dodo(this.connectionPools[s], out value))
                {
                    return(true);
                }
            }

            return(false);

            bool dodo(ConnectionPool pool, out T val)
            {
                val = default(T);
                var request = BinaryConvert.GetRequest(key);
                var item    = pool.Alloc();
                var detory  = false;

                try
                {
                    item.Connection.Write(request);
                    item.Connection.Flush();
                    var @byte       = item.Connection.Read(24);
                    var status      = BinaryConvert.ParseStatus(@byte);
                    var nflag       = byte.MinValue;
                    var totallength = BinaryConvert.ParseTotalLength(@byte);
                    var extralength = BinaryConvert.ParseExtraLength(@byte);
                    var keylength   = BinaryConvert.ParseKeyLength(@byte);
                    if (status != 0x0000)
                    {
                        if (totallength > 0)
                        {
                            item.Connection.Read(totallength);
                        }
                        return(false);
                    }

                    var alldata = item.Connection.Read(totallength);
                    var offset  = 0;
                    if (extralength > 0)
                    {
                        offset += 4;
                        nflag   = alldata[3];
                    }

                    var nkey = string.Empty;
                    if (keylength > 0)
                    {
                        var data = new byte[keylength];
                        Buffer.BlockCopy(alldata, 0, data, offset, data.Length);
                        offset += data.Length;
                        nkey    = Encoding.ASCII.GetString(data);
                    }

                    if (totallength > 0)
                    {
                        var data = new byte[totallength - offset];
                        Buffer.BlockCopy(alldata, offset, data, 0, data.Length);
                        offset += data.Length;
                        if (this.compress.TryDecompress(data, nflag, out val))
                        {
                            return(true);
                        }
                    }

                    return(false);
                }
                catch (Exception)
                {
                    detory = true;
                    return(false);
                }
                finally
                {
                    if (detory)
                    {
                        pool.Detory(item);
                    }
                    else
                    {
                        pool.Recycle(item);
                    }
                }
            }
        }