Skip to content

Commit a71ca44

Browse files
authored
Merge pull request #262 from cnblogs/add-async-stats-api
feat: add async stats api
2 parents e670108 + a71e98a commit a71ca44

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

src/Enyim.Caching/MemcachedClient.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,6 +1204,27 @@ public ServerStats Stats(string type)
12041204
return new ServerStats(results, _userIPv6);
12051205
}
12061206

1207+
public Task<ServerStats> StatsAsync()
1208+
{
1209+
return StatsAsync(null);
1210+
}
1211+
1212+
public async Task<ServerStats> StatsAsync(string type)
1213+
{
1214+
var results = new Dictionary<EndPoint, Dictionary<string, string>>();
1215+
1216+
foreach (var node in _pool.GetWorkingNodes())
1217+
{
1218+
var cmd = _pool.OperationFactory.Stats(type);
1219+
await node.ExecuteAsync(cmd);
1220+
var endpoint = node.EndPoint;
1221+
lock (results)
1222+
results[endpoint] = cmd.Result;
1223+
}
1224+
1225+
return new ServerStats(results, _userIPv6);
1226+
}
1227+
12071228
/// <summary>
12081229
/// Removes the specified item from the cache.
12091230
/// </summary>

test/Enyim.Caching.Tests/MemcachedClienStatsTests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Enyim.Caching.Configuration;
22
using System;
33
using System.Net;
4+
using System.Threading.Tasks;
45
using Xunit;
56

67
namespace Enyim.Caching.Tests
@@ -17,5 +18,17 @@ public void When_Getting_Uptime_Is_Successful()
1718
uptime = _client.Stats().GetUptime(ipEndPoint);
1819
Assert.True(uptime > TimeSpan.Zero);
1920
}
21+
22+
[Fact]
23+
public async Task When_Getting_Uptime_Using_Async_Stats_Is_Successful()
24+
{
25+
var uptime = (await _client.StatsAsync()).GetUptime(new DnsEndPoint(_memcachedHost, _memcachedPort));
26+
Assert.True(uptime > TimeSpan.Zero);
27+
28+
var ipEndPoint = new DnsEndPoint(_memcachedHost, _memcachedPort).GetIPEndPoint(false);
29+
uptime = (await _client.StatsAsync()).GetUptime(ipEndPoint);
30+
Console.WriteLine("uptime: " + uptime);
31+
Assert.True(uptime > TimeSpan.Zero);
32+
}
2033
}
2134
}

test/Enyim.Caching.Tests/MemcachedClientTestsBase.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22
using Enyim.Caching.Memcached;
33
using Enyim.Caching.Memcached.Results;
44
using Enyim.Caching.Memcached.Transcoders;
5-
using Microsoft.Extensions.Configuration;
65
using Microsoft.Extensions.DependencyInjection;
76
using Microsoft.Extensions.Logging;
87
using System;
98
using System.Collections.Generic;
10-
using System.Linq;
11-
using System.Text;
129
using System.Threading.Tasks;
1310
using Xunit;
1411

0 commit comments

Comments
 (0)