Skip to content
This repository has been archived by the owner on Mar 8, 2022. It is now read-only.

Commit

Permalink
Redis statistics, see #11
Browse files Browse the repository at this point in the history
TODO: update CompositeCache
  • Loading branch information
cd21h committed Jul 20, 2015
1 parent 4e6a42d commit bc586c2
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/Infrastructure/Caching/CacheNodeStatistics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public int HitRate
{
if (GetCount > 0)
{
return (int) ((HitCount * 10) / (GetCount / 10));
return (int) ((HitCount * 100) / GetCount);
}
return 0;
}
Expand Down
33 changes: 29 additions & 4 deletions src/Infrastructure/Caching/CacheStatistics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace Alphacloud.Common.Infrastructure.Caching
{

using System;
using System.Collections.Generic;

Expand All @@ -31,6 +32,9 @@ namespace Alphacloud.Common.Infrastructure.Caching
[Serializable]
public class CacheStatistics
{
readonly List<CacheNodeStatistics> _nodes;


/// <summary>
/// Initializes a new instance of the <see cref="CacheStatistics" /> class.
/// </summary>
Expand All @@ -42,35 +46,54 @@ public CacheStatistics(bool isSuccess) : this()
IsSuccess = isSuccess;
}


/// <summary>
/// Initializes a new instance of the <see cref="CacheStatistics" /> class.
/// </summary>
/// <param name="hitCount">The hit count.</param>
/// <param name="getCount">The get count.</param>
/// <param name="putCount">The put count.</param>
/// <param name="itemCount">The item count.</param>
public CacheStatistics(long hitCount, long getCount, long putCount, long itemCount) : this()
/// <param name="nodes">Staistics per node.</param>
/// <exception cref="ArgumentNullException"><paramref name="nodes" /> is <see langword="null" />.</exception>
public CacheStatistics(long hitCount, long getCount, long putCount, long itemCount,
[NotNull] IEnumerable<CacheNodeStatistics> nodes = null) : this()
{
IsSuccess = true;
HitCount = hitCount;
GetCount = getCount;
PutCount = putCount;
ItemCount = itemCount;
if (nodes != null)
_nodes.AddRange(nodes);
}


/// <summary>
/// Initializes a new instance of the <see cref="CacheStatistics" /> class.
/// </summary>
protected CacheStatistics()
{
Nodes = new List<CacheNodeStatistics>(8);
_nodes = new List<CacheNodeStatistics>(8);
}


/// <summary>
/// Statistics by node.
/// </summary>
[NotNull]
public List<CacheNodeStatistics> Nodes { get; private set; }
public IEnumerable<CacheNodeStatistics> Nodes
{
get { return _nodes; }
}

/// <summary>
/// Number of cache nodes.
/// </summary>
public int NodeCount
{
get { return _nodes.Count; }
}

/// <summary>
/// Gets a value indicating whether statistics request was successful.
Expand Down Expand Up @@ -113,9 +136,11 @@ public int HitRate
/// </summary>
public long ItemCount { get; private set; }


public override string ToString()
{
return "Nodes: {0}, Total items: {1}, Hit rate: {2}%".ApplyArgs(Nodes.Count, ItemCount, HitRate);
return "Nodes: {0}, Total items: {1}, Hit rate: {2}%".ApplyArgs(_nodes.Count, ItemCount, HitRate);
}
}

}
7 changes: 4 additions & 3 deletions src/Infrastructure/Caching/CompositeCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ public CacheStatistics GetStatistics()
var localStats = _localCache.GetStatistics();
if (localStats.IsSuccess)
{
backingStats.Nodes.Add(
new CacheNodeStatistics(LocalCacheInstanceName, localStats.HitCount, localStats.GetCount,
localStats.PutCount, localStats.ItemCount));
throw new NotImplementedException("Update local statistics");
//backingStats.Nodes.Add(
// new CacheNodeStatistics(LocalCacheInstanceName, localStats.HitCount, localStats.GetCount,
// localStats.PutCount, localStats.ItemCount));
}
return backingStats;
}
Expand Down
7 changes: 1 addition & 6 deletions src/Integration/Caching.Memcached/MemcachedStatsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,8 @@ void ParseStats()
var putCount = _stats.GetValue(ServerStats.All, StatItem.SetCount);
var getCount = _stats.GetValue(ServerStats.All, StatItem.GetCount);
var totalItems = _stats.GetValue(ServerStats.All, StatItem.ItemCount);
_result = new CacheStatistics(hitCount, getCount, putCount, totalItems);

var nodes = _stats.GetRaw("uptime").Select(kvp => kvp.Key);
foreach (var node in nodes)
{
_result.Nodes.Add(GetNodeStatistics(node));
}
_result = new CacheStatistics(hitCount, getCount, putCount, totalItems, nodes.Select(GetNodeStatistics));
}


Expand Down
4 changes: 2 additions & 2 deletions src/Integration/Caching.Redis/Caching.Redis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
<HintPath>..\..\packages\JetBrains.Annotations.9.1.1\lib\net20\JetBrains.Annotations.dll</HintPath>
</Reference>
<Reference Include="StackExchange.Redis.StrongName, Version=1.0.316.0, Culture=neutral, PublicKeyToken=c219ff1ca8c2ce46, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\StackExchange.Redis.StrongName.1.0.414\lib\net45\StackExchange.Redis.StrongName.dll</HintPath>
<HintPath>..\..\packages\StackExchange.Redis.StrongName.1.0.481\lib\net45\StackExchange.Redis.StrongName.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down
25 changes: 20 additions & 5 deletions src/Integration/Caching.Redis/RedisAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace Alphacloud.Common.Caching.Redis
{
using System;
using System.Collections.Generic;
using System.Linq;

using Core.Data;
using Core.Utils;
using Infrastructure.Caching;
Expand Down Expand Up @@ -57,16 +59,30 @@ public RedisAdapter(

protected override CacheStatistics DoGetStatistics()
{
var endPoints = _db.Multiplexer.GetEndPoints(configuredOnly: true);
var endPoints = _db.Multiplexer.GetEndPoints();
var nodeStatistics = new List<CacheNodeStatistics>();

foreach (var endPoint in endPoints)
{
var server = _db.Multiplexer.GetServer(endPoint);
if (server == null)
if (server == null || !server.IsConnected)
continue;

var info = server.Info();
info.ToString();
var stats = info.First(g => g.Key == "Stats");
var hits = Convert.ToInt64(stats.First(k => k.Key == "keyspace_hits").Value);
var misses = Convert.ToInt64(stats.First(k => k.Key == "keyspace_misses").Value);
var databaseKeys = server.DatabaseSize(_db.Database);
var node = new CacheNodeStatistics(server.EndPoint.ToString(),
hits, hits + misses, -1, databaseKeys
);
nodeStatistics.Add(node);
}
return new CacheStatistics(false);

var hitCount = nodeStatistics.Sum(n => n.HitCount);
var getCount = nodeStatistics.Sum(n => n.GetCount);
var totalItems = (long)nodeStatistics.Average(n => n.ItemCount);
return new CacheStatistics(hitCount, getCount, -1, totalItems, nodeStatistics);
}


Expand Down Expand Up @@ -130,7 +146,6 @@ object SafeGetValue(RedisValue data, string key, ISerializer serializer)
{
try
{
object item = null;
if (!data.IsNull)
{
return serializer.Deserialize(data);
Expand Down
2 changes: 1 addition & 1 deletion src/Integration/Caching.Redis/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
<package id="Common.Logging" version="3.0.0" targetFramework="net451" />
<package id="Common.Logging.Core" version="3.0.0" targetFramework="net451" />
<package id="JetBrains.Annotations" version="9.1.1" targetFramework="net451" />
<package id="StackExchange.Redis.StrongName" version="1.0.414" targetFramework="net451" />
<package id="StackExchange.Redis.StrongName" version="1.0.481" targetFramework="net451" />
</packages>
4 changes: 2 additions & 2 deletions src/Tests/Caching.Redis.Tests/Caching.Redis.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
<Private>True</Private>
</Reference>
<Reference Include="StackExchange.Redis.StrongName, Version=1.0.316.0, Culture=neutral, PublicKeyToken=c219ff1ca8c2ce46, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\StackExchange.Redis.StrongName.1.0.414\lib\net45\StackExchange.Redis.StrongName.dll</HintPath>
<HintPath>..\..\packages\StackExchange.Redis.StrongName.1.0.481\lib\net45\StackExchange.Redis.StrongName.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down
3 changes: 1 addition & 2 deletions src/Tests/Caching.Redis.Tests/RedisIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public void FixtureSetUp()
{
var configurationOptions = new ConfigurationOptions {
ClientName = "Alphacloud.Redis.Tests",
EndPoints = {"alphacloud-test"},
EndPoints = { "localhost:6379", "localhost:6380" },
AllowAdmin = true,
};

Expand All @@ -202,7 +202,6 @@ public void FixtureSetUp()
public void TestFixtureTearDown()
{
Disposer.TryDispose(_redis);
//Disposer.TryDispose(_streamManager);
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/Caching.Redis.Tests/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
<package id="Moq" version="4.2.1409.1722" targetFramework="net451" />
<package id="NUnit" version="2.6.4" targetFramework="net451" />
<package id="ReSharper.Annotations" version="7.1.3.130415" targetFramework="net451" />
<package id="StackExchange.Redis.StrongName" version="1.0.414" targetFramework="net451" />
<package id="StackExchange.Redis.StrongName" version="1.0.481" targetFramework="net451" />
</packages>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace Caching.Memcached.Tests
{
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Reflection;
using Alphacloud.Common.Caching.Memcached;
Expand Down Expand Up @@ -128,7 +129,7 @@ public void ShouldParseServerStatistics()
{
_cacheStatistics.Nodes.Should().HaveCount(2, "wrong server count");

var s1 = _cacheStatistics.Nodes[0];
var s1 = _cacheStatistics.Nodes.First();
s1.Server.Should().Be("127.0.0.1:11211");
s1.HitCount.Should().Be(100);
s1.GetCount.Should().Be(200);
Expand Down

0 comments on commit bc586c2

Please sign in to comment.