예제 #1
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);
                }
            }
        }