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

Commit

Permalink
Add statistics to Redis cache wrapper, see #11
Browse files Browse the repository at this point in the history
Add XML documentation and unit tests
  • Loading branch information
cd21h committed Jun 29, 2015
1 parent 4a2780a commit 4e6a42d
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/Alphacloud.Common.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@
<s:String x:Key="/Default/Environment/Editor/MatchingBraceHighlighting/Style/@EntryValue">OUTLINE</s:String>
<s:Boolean x:Key="/Default/Environment/Editor/UseCamelHumps/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpFileLayoutPatternsUpgrade/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EAddAccessorOwnerDeclarationBracesMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean>
<s:Int64 x:Key="/Default/Environment/UnitTesting/ParallelProcessesCount/@EntryValue">2</s:Int64>
Expand Down
4 changes: 2 additions & 2 deletions src/CommonAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#endif

[assembly: AssemblyCompany("Alphacloud.Net")]
[assembly: AssemblyProduct("Alphacloud commmon libs")]
[assembly: AssemblyCopyright("Copyright © Alphacloud 2013")]
[assembly: AssemblyProduct("Alphacloud commmon libraries")]
[assembly: AssemblyCopyright("Copyright © Alphacloud 2013-2015")]
[assembly: AssemblyTrademark("Alphacloud")]
[assembly: AssemblyCulture("")]
113 changes: 97 additions & 16 deletions src/Core/Data/CompactBinarySerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,47 @@ namespace Alphacloud.Common.Core.Data
using System.Text;
using JetBrains.Annotations;

/// <summary>
/// Serializer interface.
/// </summary>
public interface ISerializer
{
/// <summary>
/// Gets amount of memory allicated by underlying data structures.
/// </summary>
int MemoryAllocated { get; }


/// <summary>
/// Serialize object.
/// </summary>
/// <param name="obj">The object to serialize.</param>
/// <returns>Serialized object</returns>
[NotNull]
byte[] Serialize([NotNull] object obj);


/// <summary>
/// Deserializes object.
/// </summary>
/// <param name="buffer">The buffer.</param>
/// <returns></returns>
[CanBeNull]
object Deserialize([NotNull] byte[] buffer);
}

/// <summary>
/// Serialize object's text representation (.ToString()) as UTF8 encoded string.
/// </summary>
/// <remarks>
/// Used for debugging.
/// Deserialized object is always string.
/// </remarks>
public class StringSerializer : ISerializer
{
#region ISerializer Members

//
public int MemoryAllocated
{
get { return 1; }
Expand All @@ -61,40 +91,62 @@ public object Deserialize(byte[] buffer)
#endregion
}

public class CompactBinarySerializer : IDisposable, ISerializer
public abstract class CompactBinarySerializerBase
{
readonly BinaryFormatter _formatter;
readonly MemoryStream _stream;


public CompactBinarySerializer()
protected CompactBinarySerializerBase([NotNull] BinaryFormatter formatter)
{
_formatter = new BinaryFormatter {
AssemblyFormat = FormatterAssemblyStyle.Simple,
TypeFormat = FormatterTypeStyle.TypesWhenNeeded
};
_stream = new MemoryStream();
if (formatter == null) throw new ArgumentNullException("formatter");
_formatter = formatter;
}

#region ISerializer Members

public int MemoryAllocated
protected CompactBinarySerializerBase() : this(new BinaryFormatter {
AssemblyFormat = FormatterAssemblyStyle.Simple,
TypeFormat = FormatterTypeStyle.TypesWhenNeeded
})
{
get { return _stream.Capacity; }
}


public abstract int MemoryAllocated { get; }


public byte[] Serialize([NotNull] object obj)
{
if (obj == null) throw new ArgumentNullException("obj");
var stream = AcquireStream();

_stream.Position = 0;
_formatter.Serialize(_stream, obj);
_stream.SetLength(_stream.Position);
return _stream.ToArray();
try
{
stream.Position = 0;
_formatter.Serialize(stream, obj);
stream.SetLength(stream.Position);
return stream.ToArray();
}
finally
{
ReleaseStream(stream);
}
}


/// <summary>
/// Acquires the stream.
/// </summary>
/// <returns></returns>
protected abstract MemoryStream AcquireStream();


/// <summary>
/// Releases the stream.
/// </summary>
/// <param name="stream">The stream.</param>
protected abstract void ReleaseStream(MemoryStream stream);


public object Deserialize([NotNull] byte[] buffer)
{
if (buffer == null) throw new ArgumentNullException("buffer");
Expand All @@ -106,8 +158,14 @@ public object Deserialize([NotNull] byte[] buffer)
return _formatter.Deserialize(ms);
}
}
}

#endregion
/// <summary>
/// Serialize object using <see cref="BinaryFormatter" />.
/// </summary>
public class CompactBinarySerializer : CompactBinarySerializerBase, ISerializer, IDisposable
{
MemoryStream _stream;

#region IDisposable Members

Expand All @@ -116,9 +174,32 @@ public object Deserialize([NotNull] byte[] buffer)
/// </summary>
public void Dispose()
{
if (_stream == null) return;

_stream.Dispose();
_stream = null;
}

#endregion

#region ISerializer Members

public override int MemoryAllocated
{
get { return AcquireStream().Capacity; }
}

#endregion

protected override MemoryStream AcquireStream()
{
return _stream = (_stream ?? new MemoryStream());
}


protected override void ReleaseStream(MemoryStream stream)
{
// do nothing
}
}
}
3 changes: 2 additions & 1 deletion src/Core/Utils/ObjectPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ public sealed class SerializerPool : ObjectPool<ISerializer>


public SerializerPool(int maxPoolSize, [NotNull] Func<ISerializer> objectConstructor,
int maxPooledMemoryBlockSize) : base(maxPoolSize, objectConstructor)
int maxPooledMemoryBlockSize)
: base(maxPoolSize, objectConstructor)
{
if (maxPooledMemoryBlockSize <= 0)
throw new ArgumentOutOfRangeException("maxPooledMemoryBlockSize", maxPooledMemoryBlockSize,
Expand Down
11 changes: 7 additions & 4 deletions src/Integration/Caching.Redis/Caching.Redis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@
<Reference Include="Common.Logging.Core">
<HintPath>..\..\packages\Common.Logging.Core.3.0.0\lib\net40\Common.Logging.Core.dll</HintPath>
</Reference>
<Reference Include="JetBrains.Annotations">
<HintPath>..\..\packages\JetBrains.Annotations.8.0.5.0\lib\net20\JetBrains.Annotations.dll</HintPath>
<Reference Include="JetBrains.Annotations, Version=9.1.1.0, Culture=neutral, PublicKeyToken=1010a0d8d6380325, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\JetBrains.Annotations.9.1.1\lib\net20\JetBrains.Annotations.dll</HintPath>
</Reference>
<Reference Include="StackExchange.Redis.StrongName">
<HintPath>..\..\packages\StackExchange.Redis.StrongName.1.0.394\lib\net45\StackExchange.Redis.StrongName.dll</HintPath>
<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>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -71,6 +73,7 @@
<None Include="..\..\Alphacloud.Common.snk">
<Link>Alphacloud.Common.snk</Link>
</None>
<None Include="Caching.Redis.nuspec" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
Expand Down
27 changes: 27 additions & 0 deletions src/Integration/Caching.Redis/Caching.Redis.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0"?>
<package >
<metadata>
<id>$id$</id>
<version>$version$</version>
<title>$title$</title>
<authors>$author$</authors>
<owners>$author$</owners>
<licenseUrl>http://www.apache.org/licenses/LICENSE-2.0</licenseUrl>
<projectUrl>https://github.com/alphacloud/alphacloud.common</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>$description$</description>
<releaseNotes>
Initial version.
</releaseNotes>
<copyright>Copyright 2015</copyright>
<tags>alphacloud caching redis</tags>
<dependencies>
<dependency id="ReSharper.Annotations" version="[9.1.1,10.0.0)" />
<dependency id="Common.Logging" version="[3.0.0,4.0.0)" />
<dependency id="StackExchange.Redis.StrongName" version="[1.0.414,2.0.0)" />
<dependency id="Alphacloud.Common.Core" version="[0.3.0.0,2.0.0.0)" />
<dependency id="Alphacloud.Common.Infrastructure" version="[0.4.0.0,2.0.0.0)" />
<dependency id="Microsoft.IO.RecyclableMemoryStream" version="[1.0.0,2.0.0)" />
</dependencies>
</metadata>
</package>
9 changes: 9 additions & 0 deletions src/Integration/Caching.Redis/RedisAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ public RedisAdapter(

protected override CacheStatistics DoGetStatistics()
{
var endPoints = _db.Multiplexer.GetEndPoints(configuredOnly: true);
foreach (var endPoint in endPoints)
{
var server = _db.Multiplexer.GetServer(endPoint);
if (server == null)
continue;
var info = server.Info();
info.ToString();
}
return new CacheStatistics(false);
}

Expand Down
5 changes: 3 additions & 2 deletions src/Integration/Caching.Redis/RedisFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ public class RedisFactory : CacheFactoryBase
{
IDictionary<string, RedisConfiguration> _configurationByInstance;
// todo: customize pool to reject object based on memory size
IObjectPool<ISerializer> _serializers = new SerializerPool(16,
() => new CompactBinarySerializer(), 64 * 1024);
IObjectPool<ISerializer> _serializers;


public RedisFactory([NotNull] IEnumerable<RedisConfiguration> configurationOptions)
Expand All @@ -82,6 +81,8 @@ void InitInstanceConfiguration([NotNull] IEnumerable<RedisConfiguration> configu
if (configurationOptions == null) throw new ArgumentNullException("configurationOptions");

_configurationByInstance = configurationOptions.ToDictionary(k => k.InstanceName ?? DefaultInstanceName);
_serializers = new SerializerPool(16, () => new CompactBinarySerializer(), 256 * 1024);

}


Expand Down
4 changes: 2 additions & 2 deletions src/Integration/Caching.Redis/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
<packages>
<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="8.0.5.0" targetFramework="net451" />
<package id="StackExchange.Redis.StrongName" version="1.0.394" targetFramework="net451" />
<package id="JetBrains.Annotations" version="9.1.1" targetFramework="net451" />
<package id="StackExchange.Redis.StrongName" version="1.0.414" targetFramework="net451" />
</packages>
5 changes: 4 additions & 1 deletion src/Tests/Caching.Redis.Tests/Caching.Redis.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
</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.394\lib\net45\StackExchange.Redis.StrongName.dll</HintPath>
<HintPath>..\..\packages\StackExchange.Redis.StrongName.1.0.414\lib\net45\StackExchange.Redis.StrongName.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand Down Expand Up @@ -100,6 +100,9 @@
<Name>Testing.Nunit</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
Expand Down
12 changes: 11 additions & 1 deletion src/Tests/Caching.Redis.Tests/RedisIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ public void MultiGet_Should_RetrieveMultipleItems()
res[k2].Should().Be("val2");
}


[Test]
public void CanGetStatistics()
{
var stat = _cache.GetStatistics();
}

#region Nested type: CachedItem

[Serializable]
Expand Down Expand Up @@ -177,9 +184,11 @@ public void FixtureSetUp()
{
var configurationOptions = new ConfigurationOptions {
ClientName = "Alphacloud.Redis.Tests",
EndPoints = {"alphacloud-test"}
EndPoints = {"alphacloud-test"},
AllowAdmin = true,
};

//_streamManager = new RecyclableMemoryStreamManager(32 * 1024, 256 * 1024, 4 * 1024 * 1024);
_redis = ConnectionMultiplexer.Connect(configurationOptions, Console.Out);
_db = _redis.GetDatabase();
_factory = new RedisFactory(new[] {
Expand All @@ -193,6 +202,7 @@ 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.394" targetFramework="net451" />
<package id="StackExchange.Redis.StrongName" version="1.0.414" targetFramework="net451" />
</packages>

0 comments on commit 4e6a42d

Please sign in to comment.