Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Enyim.Caching.Tests/MemcachedClientGetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ public void When_TryGetting_Existing_Item_Value_Is_Not_Null_And_Result_Is_Succes
GetAssertPass(getResult, temp);
}

[Fact]
public void When_Generic_TryGetting_Existing_Item_Value_Is_Not_Null_And_Result_Is_Successful()
{
var key = GetUniqueKey("get");
var value = GetRandomString();
var storeResult = Store(key: key, value: value);
StoreAssertPass(storeResult);

string temp;
var getResult = _client.ExecuteTryGet(key, out temp);
GetAssertPass(getResult, temp);
}

[Fact]
public void When_Generic_Getting_Existing_Item_Value_Is_Not_Null_And_Result_Is_Successful()
{
Expand Down
2 changes: 2 additions & 0 deletions Enyim.Caching/IMemcachedClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public interface IMemcachedClient : IDisposable
Task<IDictionary<string, T>> GetAsync<T>(IEnumerable<string> keys);

bool TryGet(string key, out object value);
bool TryGet<T>(string key, out T value);
bool TryGetWithCas(string key, out CasResult<object> value);
bool TryGetWithCas<T>(string key, out CasResult<T> value);

CasResult<object> GetWithCas(string key);
CasResult<T> GetWithCas<T>(string key);
Expand Down
1 change: 1 addition & 0 deletions Enyim.Caching/IMemcachedResultsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public interface IMemcachedResultsClient
IDictionary<string, IGetOperationResult> ExecuteGet(IEnumerable<string> keys);

IGetOperationResult ExecuteTryGet(string key, out object value);
IGetOperationResult ExecuteTryGet<T>(string key, out T value);

IStoreOperationResult ExecuteStore(StoreMode mode, string key, object value);
IStoreOperationResult ExecuteStore(StoreMode mode, string key, object value, DateTime expiresAt);
Expand Down
29 changes: 21 additions & 8 deletions Enyim.Caching/MemcachedClient.Results.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,29 @@ public IGetOperationResult ExecuteTryGet(string key, out object value)
ulong cas = 0;

return this.PerformTryGet(key, out cas, out value);
}
}

/// <summary>
/// Retrieves the specified item from the cache.
/// </summary>
/// <param name="key">The identifier for the item to retrieve.</param>
/// <returns>The retrieved item, or <value>default(T)</value> if the key was not found.</returns>
public IGetOperationResult<T> ExecuteGet<T>(string key)
/// <summary>
/// Tries to get an item from the cache.
/// </summary>
/// <param name="key">The identifier for the item to retrieve.</param>
/// <param name="value">The retrieved item or null if not found.</param>
/// <returns>The <value>true</value> if the item was successfully retrieved.</returns>
public IGetOperationResult ExecuteTryGet<T>(string key, out T value)
{
ulong cas = 0;

return this.PerformTryGet(key, out cas, out value);
}

/// <summary>
/// Retrieves the specified item from the cache.
/// </summary>
/// <param name="key">The identifier for the item to retrieve.</param>
/// <returns>The retrieved item, or <value>default(T)</value> if the key was not found.</returns>
public IGetOperationResult<T> ExecuteGet<T>(string key)
{
object tmp;
T tmp;
var result = new DefaultGetOperationResultFactory<T>().Create();

var tryGetResult = ExecuteTryGet(key, out tmp);
Expand Down
62 changes: 59 additions & 3 deletions Enyim.Caching/MemcachedClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,18 +261,31 @@ public bool TryGet(string key, out object value)
return this.PerformTryGet(key, out cas, out value).Success;
}

/// <summary>
/// Tries to get an item from the cache.
/// </summary>
/// <param name="key">The identifier for the item to retrieve.</param>
/// <param name="value">The retrieved item or null if not found.</param>
/// <returns>The <value>true</value> if the item was successfully retrieved.</returns>
public bool TryGet<T>(string key, out T value)
{
ulong cas = 0;

return this.PerformTryGet(key, out cas, out value).Success;
}

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

public CasResult<T> GetWithCas<T>(string key)
{
CasResult<object> tmp;
CasResult<T> 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) };
? new CasResult<T> { Cas = tmp.Cas, Result = tmp.Result }
: new CasResult<T> { Cas = tmp.Cas, Result = default };
}

public bool TryGetWithCas(string key, out CasResult<object> value)
Expand All @@ -287,6 +300,15 @@ public bool TryGetWithCas(string key, out CasResult<object> value)
return retval.Success;
}

public bool TryGetWithCas<T>(string key, out CasResult<T> value)
{
var retVal = PerformTryGet(key, out var cas, out T 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);
Expand Down Expand Up @@ -323,6 +345,40 @@ protected virtual IGetOperationResult PerformTryGet(string key, out ulong cas, o
return result;
}

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

cas = 0;
value = default;

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

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

result.Pass();
return result;
}

commandResult.Combine(result);
return result;
}

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

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


#region [ Store ]

Expand Down
10 changes: 10 additions & 0 deletions Enyim.Caching/MemcachedClientT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,19 @@ public bool TryGet(string key, out object value)
return _memcachedClient.TryGet(key, out value);
}

public bool TryGet<T1>(string key, out T1 value)
{
return _memcachedClient.TryGet(key, out value);
}

public bool TryGetWithCas(string key, out CasResult<object> value)
{
return _memcachedClient.TryGetWithCas(key, out value);
}

public bool TryGetWithCas<T1>(string key, out CasResult<T1> value)
{
return _memcachedClient.TryGetWithCas(key, out value);
}
}
}
12 changes: 12 additions & 0 deletions Enyim.Caching/NullMemcachedClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,24 @@ public bool TryGet(string key, out object value)
return false;
}

public bool TryGet<T>(string key, out T value)
{
value = default;
return false;
}

public bool TryGetWithCas(string key, out CasResult<object> value)
{
value = new CasResult<object>();
return false;
}

public bool TryGetWithCas<T>(string key, out CasResult<T> value)
{
value = new CasResult<T>();
return false;
}

public bool Add(string key, object value, int cacheSeconds)
{
return true;
Expand Down