1
+ using Enyim . Caching . Configuration ;
1
2
using System ;
2
- using System . Linq ;
3
3
using System . Collections . Generic ;
4
+ using System . Linq ;
4
5
using System . Net ;
5
6
6
7
namespace Enyim . Caching . Memcached
@@ -17,6 +18,7 @@ public sealed class ServerStats
17
18
/// Defines a value which indicates that the statstics should be retrieved for all servers in the pool.
18
19
/// </summary>
19
20
public static readonly IPEndPoint All = new IPEndPoint ( IPAddress . Any , 0 ) ;
21
+
20
22
#region [ readonly int[] Optable ]
21
23
// defines which values can be summed and which not
22
24
private static readonly int [ ] Optable =
@@ -25,6 +27,7 @@ public sealed class ServerStats
25
27
1 , 1 , 1 , 1 , 1 , 1 , 1 , 1
26
28
} ;
27
29
#endregion
30
+
28
31
#region [ readonly string[] StatKeys ]
29
32
private static readonly string [ ] StatKeys =
30
33
{
@@ -47,11 +50,14 @@ public sealed class ServerStats
47
50
} ;
48
51
#endregion
49
52
50
- private Dictionary < EndPoint , Dictionary < string , string > > _results ;
53
+ private readonly Dictionary < EndPoint , Dictionary < string , string > > _results ;
51
54
52
- internal ServerStats ( Dictionary < EndPoint , Dictionary < string , string > > results )
55
+ private readonly bool _useIPv6 ;
56
+
57
+ internal ServerStats ( Dictionary < EndPoint , Dictionary < string , string > > results , bool useIPv6 )
53
58
{
54
59
_results = results ;
60
+ _useIPv6 = useIPv6 ;
55
61
}
56
62
57
63
/// <summary>
@@ -62,12 +68,14 @@ internal ServerStats(Dictionary<EndPoint, Dictionary<string, string>> results)
62
68
/// <returns>The value of the specified stat item</returns>
63
69
public long GetValue ( EndPoint server , StatItem item )
64
70
{
71
+ server = server . GetIPEndPoint ( _useIPv6 ) ;
72
+
65
73
// asked for a specific server
66
74
if ( server is not IPEndPoint || ( ( IPEndPoint ) server ) . Address != IPAddress . Any )
67
75
{
68
76
// error check
69
77
string tmp = GetRaw ( server , item ) ;
70
- if ( String . IsNullOrEmpty ( tmp ) )
78
+ if ( string . IsNullOrEmpty ( tmp ) )
71
79
throw new ArgumentException ( "Item was not found: " + item ) ;
72
80
73
81
long value ;
@@ -100,8 +108,9 @@ public long GetValue(EndPoint server, StatItem item)
100
108
/// <returns>The version of memcached</returns>
101
109
public Version GetVersion ( EndPoint server )
102
110
{
111
+ server = server . GetIPEndPoint ( _useIPv6 ) ;
103
112
string version = GetRaw ( server , StatItem . Version ) ;
104
- if ( String . IsNullOrEmpty ( version ) )
113
+ if ( string . IsNullOrEmpty ( version ) )
105
114
throw new ArgumentException ( "No version found for the server " + server ) ;
106
115
107
116
return new Version ( version ) ;
@@ -114,12 +123,13 @@ public Version GetVersion(EndPoint server)
114
123
/// <returns>A value indicating how long the server is running</returns>
115
124
public TimeSpan GetUptime ( EndPoint server )
116
125
{
126
+ server = server . GetIPEndPoint ( _useIPv6 ) ;
117
127
string uptime = GetRaw ( server , StatItem . Uptime ) ;
118
- if ( String . IsNullOrEmpty ( uptime ) )
128
+ if ( string . IsNullOrEmpty ( uptime ) )
119
129
throw new ArgumentException ( "No uptime found for the server " + server ) ;
120
130
121
131
long value ;
122
- if ( ! Int64 . TryParse ( uptime , out value ) )
132
+ if ( ! long . TryParse ( uptime , out value ) )
123
133
throw new ArgumentException ( "Invalid uptime string was returned: " + uptime ) ;
124
134
125
135
return TimeSpan . FromSeconds ( value ) ;
@@ -133,12 +143,11 @@ public TimeSpan GetUptime(EndPoint server)
133
143
/// <returns>The value of the stat item</returns>
134
144
public string GetRaw ( EndPoint server , string key )
135
145
{
136
- Dictionary < string , string > serverValues ;
137
- string retval ;
146
+ server = server . GetIPEndPoint ( _useIPv6 ) ;
138
147
139
- if ( _results . TryGetValue ( server , out serverValues ) )
148
+ if ( _results . TryGetValue ( server , out Dictionary < string , string > serverValues ) )
140
149
{
141
- if ( serverValues . TryGetValue ( key , out retval ) )
150
+ if ( serverValues . TryGetValue ( key , out string retval ) )
142
151
return retval ;
143
152
144
153
if ( _log . IsDebugEnabled )
@@ -161,17 +170,17 @@ public string GetRaw(EndPoint server, string key)
161
170
/// <returns>The value of the stat item</returns>
162
171
public string GetRaw ( EndPoint server , StatItem item )
163
172
{
173
+ server = server . GetIPEndPoint ( _useIPv6 ) ;
174
+
164
175
if ( ( int ) item < StatKeys . Length && ( int ) item >= 0 )
165
176
return GetRaw ( server , StatKeys [ ( int ) item ] ) ;
166
177
167
- throw new ArgumentOutOfRangeException ( " item" ) ;
178
+ throw new ArgumentOutOfRangeException ( nameof ( item ) ) ;
168
179
}
169
180
170
181
public IEnumerable < KeyValuePair < EndPoint , string > > GetRaw ( string key )
171
182
{
172
- string tmp ;
173
-
174
- return _results . Select ( kvp => new KeyValuePair < EndPoint , string > ( kvp . Key , kvp . Value . TryGetValue ( key , out tmp ) ? tmp : null ) ) . ToList ( ) ;
183
+ return _results . Select ( kvp => new KeyValuePair < EndPoint , string > ( kvp . Key , kvp . Value . TryGetValue ( key , out string tmp ) ? tmp : null ) ) . ToList ( ) ;
175
184
}
176
185
}
177
186
}
0 commit comments