示例#1
0
        /// <summary>
        /// 删除key
        /// </summary>
        /// <returns></returns>
        public override bool TryDelete(string key)
        {
            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.DeleteRequest(key);
                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("delete 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("delete key", "key", key, "status:", status));
                        }

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