forked from enyim/EnyimMemcached
-
Notifications
You must be signed in to change notification settings - Fork 51
Closed
Description
主要是这里提供了一个泛型的反序列化方法
T Deserialize<T>(CacheItem item); |
代码做一点修改就能支持。
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;
}
TeemoHQ
Metadata
Metadata
Assignees
Labels
No labels