Skip to content

Commit 0b4fa19

Browse files
authored
Merge pull request #48 from catcherwong/dotnetcore
Add FlushAllAsync api
2 parents aaa3376 + afead5f commit 0b4fa19

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

Enyim.Caching/IMemcachedClient.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public interface IMemcachedClient : IDisposable
6666
Task<bool> RemoveAsync(string key);
6767

6868
void FlushAll();
69+
Task FlushAllAsync();
6970

7071
ServerStats Stats();
7172
ServerStats Stats(string type);

Enyim.Caching/MemcachedClient.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,20 @@ public void FlushAll()
884884
}
885885
}
886886

887+
public async Task FlushAllAsync()
888+
{
889+
var tasks = new List<Task>();
890+
891+
foreach (var node in this.pool.GetWorkingNodes())
892+
{
893+
var command = this.pool.OperationFactory.Flush();
894+
895+
tasks.Add(node.ExecuteAsync(command));
896+
}
897+
898+
await Task.WhenAll(tasks);
899+
}
900+
887901
/// <summary>
888902
/// Returns statistics about the servers.
889903
/// </summary>

Enyim.Caching/NullMemcachedClient.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,5 +252,10 @@ public Task<T> GetValueOrCreateAsync<T>(string key, int cacheSeconds, Task<T> ge
252252
{
253253
return generator;
254254
}
255+
256+
public Task FlushAllAsync()
257+
{
258+
return Task.CompletedTask;
259+
}
255260
}
256261
}

MemcachedTest/MemcachedClientTest.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,27 @@ public void IncrementLongTest()
358358
Assert.Equal(initialValue + 24, client.Increment("VALUE", 10UL, 24UL));
359359
}
360360
}
361+
362+
[Fact]
363+
public async Task FlushTest()
364+
{
365+
using (MemcachedClient client = GetClient())
366+
{
367+
for (int i = 0; i < 10; i++)
368+
{
369+
string cacheKey = $"Hello_Flush_{i}";
370+
Assert.True(await client.StoreAsync(StoreMode.Set, cacheKey, i, DateTime.Now.AddSeconds(30)));
371+
}
372+
373+
await client.FlushAllAsync();
374+
375+
for (int i = 0; i < 10; i++)
376+
{
377+
string cacheKey = $"Hello_Flush_{i}";
378+
Assert.Null(await client.GetValueAsync<string>(cacheKey));
379+
}
380+
}
381+
}
361382
}
362383
}
363384

0 commit comments

Comments
 (0)