Skip to content

feat: add async stats api #262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2025
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
21 changes: 21 additions & 0 deletions src/Enyim.Caching/MemcachedClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,27 @@ public ServerStats Stats(string type)
return new ServerStats(results, _userIPv6);
}

public Task<ServerStats> StatsAsync()
{
return StatsAsync(null);
}

public async Task<ServerStats> StatsAsync(string type)
{
var results = new Dictionary<EndPoint, Dictionary<string, string>>();

foreach (var node in _pool.GetWorkingNodes())
{
var cmd = _pool.OperationFactory.Stats(type);
await node.ExecuteAsync(cmd);
var endpoint = node.EndPoint;
lock (results)
results[endpoint] = cmd.Result;
}

return new ServerStats(results, _userIPv6);
}

/// <summary>
/// Removes the specified item from the cache.
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions test/Enyim.Caching.Tests/MemcachedClienStatsTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Enyim.Caching.Configuration;
using System;
using System.Net;
using System.Threading.Tasks;
using Xunit;

namespace Enyim.Caching.Tests
Expand All @@ -17,5 +18,17 @@ public void When_Getting_Uptime_Is_Successful()
uptime = _client.Stats().GetUptime(ipEndPoint);
Assert.True(uptime > TimeSpan.Zero);
}

[Fact]
public async Task When_Getting_Uptime_Using_Async_Stats_Is_Successful()
{
var uptime = (await _client.StatsAsync()).GetUptime(new DnsEndPoint(_memcachedHost, _memcachedPort));
Assert.True(uptime > TimeSpan.Zero);

var ipEndPoint = new DnsEndPoint(_memcachedHost, _memcachedPort).GetIPEndPoint(false);
uptime = (await _client.StatsAsync()).GetUptime(ipEndPoint);
Console.WriteLine("uptime: " + uptime);
Assert.True(uptime > TimeSpan.Zero);
}
}
}
3 changes: 0 additions & 3 deletions test/Enyim.Caching.Tests/MemcachedClientTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
using Enyim.Caching.Memcached;
using Enyim.Caching.Memcached.Results;
using Enyim.Caching.Memcached.Transcoders;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

Expand Down