Skip to content

Commit caaf934

Browse files
authored
Merge pull request #66 from Gonyoda/master
Use key transformer during Remove/RemoveAsync
2 parents eb79619 + 98f2195 commit caaf934

File tree

4 files changed

+128
-9
lines changed

4 files changed

+128
-9
lines changed

Enyim.Caching.Tests/MemcachedClientTestsBase.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,26 @@
99
using Microsoft.Extensions.DependencyInjection;
1010
using Microsoft.Extensions.Configuration;
1111
using Microsoft.Extensions.Logging;
12+
using System.Threading.Tasks;
1213

1314
namespace Enyim.Caching.Tests
1415
{
1516
public abstract class MemcachedClientTestsBase
1617
{
1718
protected MemcachedClient _client;
1819

19-
public MemcachedClientTestsBase()
20+
public MemcachedClientTestsBase(Action<MemcachedClientOptions> onAddEnyimMemcached = null)
2021
{
2122
IServiceCollection services = new ServiceCollection();
22-
services.AddEnyimMemcached(options => options.AddServer("memcached", 11211));
23+
services.AddEnyimMemcached(options =>
24+
{
25+
options.AddServer("memcached", 11211);
26+
if (onAddEnyimMemcached != null)
27+
{
28+
onAddEnyimMemcached(options);
29+
}
30+
});
31+
2332
services.AddLogging(builder => builder.SetMinimumLevel(LogLevel.Debug).AddConsole());
2433
IServiceProvider serviceProvider = services.BuildServiceProvider();
2534
_client = serviceProvider.GetService<IMemcachedClient>() as MemcachedClient;
@@ -63,6 +72,20 @@ protected IStoreOperationResult Store(StoreMode mode = StoreMode.Set, string key
6372
return _client.ExecuteStore(mode, key, value);
6473
}
6574

75+
protected Task<bool> StoreAsync(StoreMode mode = StoreMode.Set, string key = null, object value = null)
76+
{
77+
if (string.IsNullOrEmpty(key))
78+
{
79+
key = GetUniqueKey("store");
80+
}
81+
82+
if (value == null)
83+
{
84+
value = GetRandomString();
85+
}
86+
return _client.StoreAsync(mode, key, value, TimeSpan.MaxValue);
87+
}
88+
6689
protected void StoreAssertPass(IStoreOperationResult result)
6790
{
6891
Assert.True(result.Success, "Success was false");
@@ -95,6 +118,15 @@ protected void GetAssertFail(IGetOperationResult result)
95118
Assert.Null(result.Value);
96119
}
97120

121+
protected void GetAssertFail(IGetOperationResult<object> result)
122+
{
123+
Assert.False(result.Success, "Success was true");
124+
Assert.Equal((ulong)0, result.Cas);
125+
Assert.True(result.StatusCode > 0, "StatusCode not greater than 0");
126+
Assert.False(result.HasValue, "HasValue was true");
127+
Assert.Null(result.Value);
128+
}
129+
98130
protected void MutateAssertPass(IMutateOperationResult result, ulong expectedValue)
99131
{
100132
Assert.True(result.Success, "Success was false");
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Enyim.Caching.Configuration;
7+
using Enyim.Caching.Memcached;
8+
using Xunit;
9+
10+
namespace Enyim.Caching.Tests
11+
{
12+
public class MemcachedClientWithKeyTransformerTests : MemcachedClientTestsBase
13+
{
14+
public MemcachedClientWithKeyTransformerTests(Action<MemcachedClientOptions> onAddEnyimMemcached = null)
15+
: base(options =>
16+
{
17+
options.KeyTransformer = "Enyim.Caching.Memcached.TigerHashKeyTransformer";
18+
if (onAddEnyimMemcached != null)
19+
{
20+
onAddEnyimMemcached(options);
21+
}
22+
})
23+
{
24+
}
25+
26+
[Fact]
27+
public void When_Removing_A_Valid_Transformed_Key_Is_Successful()
28+
{
29+
var key = GetUniqueKey("remove");
30+
var storeResult = Store(key: key);
31+
StoreAssertPass(storeResult);
32+
33+
var removeResult = _client.ExecuteRemove(key);
34+
Assert.True(removeResult.Success, "Success was false");
35+
Assert.True((removeResult.StatusCode ?? 0) == 0, "StatusCode was neither null nor 0");
36+
37+
var getResult = _client.ExecuteGet(key);
38+
GetAssertFail(getResult);
39+
}
40+
41+
42+
[Fact]
43+
public async Task When_Removing_A_Valid_Transformed_Key_Is_Successful_Async()
44+
{
45+
var key = GetUniqueKey("remove");
46+
var storeResult = await StoreAsync(key: key);
47+
Assert.True(storeResult, "Success was false");
48+
49+
var removeResult = await _client.RemoveAsync(key);
50+
51+
Assert.True(removeResult, "Success was false");
52+
53+
var getResult = await _client.GetAsync<object>(key);
54+
GetAssertFail(getResult);
55+
}
56+
57+
[Fact]
58+
public void When_Getting_Existing_Item_Value_With_Transformed_Key_Is_Not_Null_And_Result_Is_Successful()
59+
{
60+
var key = GetUniqueKey("get");
61+
var value = GetRandomString();
62+
var storeResult = Store(key: key, value: value);
63+
StoreAssertPass(storeResult);
64+
65+
var getResult = _client.ExecuteGet(key);
66+
GetAssertPass(getResult, value);
67+
}
68+
69+
[Fact]
70+
public void When_Storing_Item_With_With_Transformed_Key_And_Valid_Cas_Result_Is_Successful()
71+
{
72+
var key = GetUniqueKey("cas");
73+
var value = GetRandomString();
74+
var storeResult = Store(StoreMode.Add, key, value);
75+
StoreAssertPass(storeResult);
76+
77+
var casResult = _client.ExecuteCas(StoreMode.Set, key, value, storeResult.Cas);
78+
StoreAssertPass(casResult);
79+
}
80+
}
81+
}

Enyim.Caching/MemcachedClient.Results.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -444,13 +444,13 @@ public IConcatOperationResult ExecutePrepend(string key, ulong cas, ArraySegment
444444
/// <returns>true if the item was successfully removed from the cache; false otherwise.</returns>
445445
public IRemoveOperationResult ExecuteRemove(string key)
446446
{
447-
//var hashedKey = this.keyTransformer.Transform(key);
448-
var node = this.pool.Locate(key);
447+
var hashedKey = this.keyTransformer.Transform(key);
448+
var node = this.pool.Locate(hashedKey);
449449
var result = RemoveOperationResultFactory.Create();
450450

451451
if (node != null)
452452
{
453-
var command = this.pool.OperationFactory.Delete(key, 0);
453+
var command = this.pool.OperationFactory.Delete(hashedKey, 0);
454454
var commandResult = node.Execute(command);
455455

456456
if (commandResult.Success)
@@ -472,12 +472,13 @@ public IRemoveOperationResult ExecuteRemove(string key)
472472

473473
public async Task<IRemoveOperationResult> ExecuteRemoveAsync(string key)
474474
{
475-
var node = this.pool.Locate(key);
475+
var hashedKey = this.keyTransformer.Transform(key);
476+
var node = this.pool.Locate(hashedKey);
476477
var result = RemoveOperationResultFactory.Create();
477478

478479
if (node != null)
479480
{
480-
var command = this.pool.OperationFactory.Delete(key, 0);
481+
var command = this.pool.OperationFactory.Delete(hashedKey, 0);
481482
var commandResult = await node.ExecuteAsync(command);
482483

483484
if (commandResult.Success)

Enyim.Caching/MemcachedClient.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ public async Task<IGetOperationResult<T>> GetAsync<T>(string key)
196196
result.Value = transcoder.Deserialize<T>(command.Result);
197197
return result;
198198
}
199+
else
200+
{
201+
commandResult.Combine(result);
202+
203+
return result;
204+
}
199205
}
200206
catch (Exception ex)
201207
{
@@ -208,8 +214,7 @@ public async Task<IGetOperationResult<T>> GetAsync<T>(string key)
208214
_logger.LogError($"Unable to locate memcached node");
209215
}
210216

211-
result.Success = false;
212-
result.Value = default(T);
217+
result.Fail("Unable to locate node");
213218
return result;
214219
}
215220

0 commit comments

Comments
 (0)