Exemplo n.º 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);
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 对值+-1
        /// </summary>
        /// <returns></returns>
        protected override bool TryInterlocked(Command command, string key, long inter, TimeSpan expireTime)
        {
            if (key.IsNullOrWhiteSpace())
            {
                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]))
                {
                    return(true);
                }
            }

            return(false);

            //开始something
            bool dodo(ConnectionPool pool)
            {
                var request = BinaryConvert.InterlockedRequest(command, key, inter, this.GetTotalSecond(expireTime));
                var item    = pool.Alloc();

                try
                {
                    item.Connection.Write(request);
                    item.Connection.Flush();
                    var @byte  = item.Connection.Read(24);
                    var status = BinaryConvert.ParseStatus(@byte);
                    switch ((BinaryProtocols.ResponseStatus)status)
                    {
                    case BinaryProtocols.ResponseStatus.KeyExists:
                    {
                        if (this.LoggerBuilder == null)
                        {
                            return(false);
                        }

                        this.LoggerBuilder().Build(typeof(TextCached)).Info(string.Concat("decr/incr key", key, "not found"));
                        return(false);
                    }

                    case BinaryProtocols.ResponseStatus.NoError:
                    {
                        return(true);
                    }

                    default:
                    {
                        if (this.LoggerBuilder != null)
                        {
                            this.LoggerBuilder().Build(typeof(TextCached)).Info(string.Concat("decr/incr key", "key", key, "status:", status));
                        }

                        return(false);
                    }
                    }
                }
                catch
                {
                    return(false);
                }
                finally
                {
                    pool.Recycle(item);
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 开始set
        /// </summary>
        private bool TrySetValue(Command command, string key, byte[] value, byte flag, TimeSpan expireTime)
        {
            if (expireTime < TimeSpan.Zero)
            {
                return(true);
            }

            if (key.IsNullOrWhiteSpace())
            {
                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]))
                {
                    return(true);
                }
            }

            return(false);

            //开始set
            bool dodo(ConnectionPool pool)
            {
                var request = BinaryConvert.AddRequest(command, key, value, flag, this.GetTotalSecond(expireTime));
                var item    = pool.Alloc();

                try
                {
                    item.Connection.Write(request);
                    item.Connection.Flush();
                    var @byte  = item.Connection.Read(24);
                    var status = BinaryConvert.ParseStatus(@byte);
                    switch ((BinaryProtocols.ResponseStatus)status)
                    {
                    case BinaryProtocols.ResponseStatus.NoError:
                    {
                        return(true);
                    }

                    case BinaryProtocols.ResponseStatus.KeyExists:
                    {
                        //主要是这里引出不同
                        return(command == Command.add ? false : true);
                    }

                    case BinaryProtocols.ResponseStatus.KeyNotFound:
                    {
                        if (this.LoggerBuilder == null)
                        {
                            return(false);
                        }

                        this.LoggerBuilder().Build(typeof(TextCached)).Info(string.Concat(command, "key", key, "not stored"));
                        return(false);
                    }

                    default:
                    {
                        if (this.LoggerBuilder != null)
                        {
                            this.LoggerBuilder().Build(typeof(TextCached)).Info(string.Concat(command, "key", key, "status:", status));
                        }

                        return(false);
                    }
                    }
                }
                catch (Exception)
                {
                    return(false);
                }
                finally
                {
                    pool.Recycle(item);
                }
            }
        }