Skip to content

泛型方法 GetWithCas<T> 的实现是不是应该考虑把类型信息传递到反序列化器中? #123

@echofool

Description

@echofool

主要是这里提供了一个泛型的反序列化方法


代码做一点修改就能支持。
see
public CasResult<T> GetWithCas<T>(string key)

        public CasResult<object> GetWithCas(string key)
        {
            return this.GetWithCas<object>(key);
        }

        public CasResult<T> GetWithCas<T>(string key)
        {
            CasResult<object> tmp;

            return this.TryGetWithCas(key, out tmp)
                    ? new CasResult<T> { Cas = tmp.Cas, Result = (T)tmp.Result }
                    : new CasResult<T> { Cas = tmp.Cas, Result = default(T) };
        }

        public bool TryGetWithCas(string key, out CasResult<object> value)
        {
            object tmp;
            ulong cas;

            var retval = this.PerformTryGet(key, out cas, out tmp);

            value = new CasResult<object> { Cas = cas, Result = tmp };

            return retval.Success;
        }

        protected virtual IGetOperationResult PerformTryGet(string key, out ulong cas, out object value)
        {
            var hashedKey = this.keyTransformer.Transform(key);
            var node = this.pool.Locate(hashedKey);
            var result = GetOperationResultFactory.Create();

            cas = 0;
            value = null;

            if (node != null)
            {
                var command = this.pool.OperationFactory.Get(hashedKey);
                var commandResult = node.Execute(command);

                if (commandResult.Success)
                {
                    result.Value = value = this.transcoder.Deserialize(command.Result);
                    result.Cas = cas = command.CasValue;

                    result.Pass();
                    return result;
                }
                else
                {
                    commandResult.Combine(result);
                    return result;
                }
            }

            result.Value = value;
            result.Cas = cas;

            result.Fail("Unable to locate node");
            return result;
        }

以上代码中我觉得可以改成如下的代码

        public CasResult<object> GetWithCas(string key)
        {
            return this.GetWithCas<object>(key);
        }

        public CasResult<T> GetWithCas<T>(string key)
        {
            CasResult<T> tmp;

            return this.TryGetWithCas(key, out tmp)
                    ? new CasResult<T> { Cas = tmp.Cas, Result = tmp.Result }
                    : new CasResult<T> { Cas = tmp.Cas, Result = default(T) };
        }

        public bool TryGetWithCas(string key, out CasResult<object> value)
        {
            object tmp;
            ulong cas;

            var retval = this.PerformTryGet(key, out cas, out tmp);

            value = new CasResult<object> { Cas = cas, Result = tmp };

            return retval.Success;
        }

        public bool TryGetWithCas<T>(string key, out CasResult<T> value)
        {
            T tmp;
            ulong cas;

            var retval = this.PerformTryGet(key, out cas, out tmp);

            value = new CasResult<T> { Cas = cas, Result = tmp };

            return retval.Success;
        }

        protected virtual IGetOperationResult PerformTryGet(string key, out ulong cas, out object value)
        {
            var hashedKey = this.keyTransformer.Transform(key);
            var node = this.pool.Locate(hashedKey);
            var result = GetOperationResultFactory.Create();

            cas = 0;
            value = null;

            if (node != null)
            {
                var command = this.pool.OperationFactory.Get(hashedKey);
                var commandResult = node.Execute(command);

                if (commandResult.Success)
                {
                    result.Value = value = this.transcoder.Deserialize(command.Result);
                    result.Cas = cas = command.CasValue;

                    result.Pass();
                    return result;
                }
                else
                {
                    commandResult.Combine(result);
                    return result;
                }
            }

            result.Value = value;
            result.Cas = cas;

            result.Fail("Unable to locate node");
            return result;
        }

        protected virtual IGetOperationResult PerformTryGet<T>(string key, out ulong cas, out T value)
        {
            var hashedKey = this.keyTransformer.Transform(key);
            var node = this.pool.Locate(hashedKey);
            var result = GetOperationResultFactory.Create();

            cas = 0;
            value = default(T);

            if (node != null)
            {
                var command = this.pool.OperationFactory.Get(hashedKey);
                var commandResult = node.Execute(command);

                if (commandResult.Success)
                {
                    result.Value = value = this.transcoder.Deserialize<T>(command.Result);
                    result.Cas = cas = command.CasValue;

                    result.Pass();
                    return result;
                }
                else
                {
                    commandResult.Combine(result);
                    return result;
                }
            }

            result.Value = value;
            result.Cas = cas;

            result.Fail("Unable to locate node");
            return result;
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions