Skip to content

Commit a5514e4

Browse files
committed
Dual license; separate CacheHost DLL for self hosting; performance counter cleanup
1 parent 06b4781 commit a5514e4

27 files changed

+534
-197
lines changed

Dache.CacheHost/CacheHostEngine.cs

Lines changed: 57 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,91 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using Dache.CacheHost.Polling;
6+
using Dache.CacheHost.Routing;
7+
using Dache.CacheHost.Storage;
8+
using Dache.Core.Communication;
9+
using Dache.Core.Logging;
10+
using Dache.Core.Performance;
211

312
namespace Dache.CacheHost
413
{
514
/// <summary>
6-
/// The engine which runs the cache host.
15+
/// The cache host engine. Instantiate this to host an instance of the cache host within your own process.
716
/// </summary>
8-
public class CacheHostEngine : IRunnable
17+
public class CacheHostEngine
918
{
10-
// The cache server
11-
private readonly IRunnable _cacheServer = null;
12-
// The cache host information poller
13-
private readonly IRunnable _cacheHostInformationPoller = null;
19+
// The cache host runner
20+
private readonly IRunnable _cacheHostRunner = null;
1421

1522
/// <summary>
1623
/// The constructor.
1724
/// </summary>
18-
/// <param name="cacheHostInformationPoller">The cache host information poller.</param>
19-
/// <param name="cacheHostServer">The cache host server.</param>
20-
public CacheHostEngine(IRunnable cacheHostInformationPoller, IRunnable cacheHostServer)
25+
/// <param name="memCache">The mem cache to use.</param>
26+
/// <param name="logger">The logger to use.</param>
27+
/// <param name="port">The port to open.</param>
28+
/// <param name="physicalMemoryLimitPercentage">The cache memory limit, as a percentage of physical memory.</param>
29+
/// <param name="maximumConnections">The maximum number of simultaneous connections permitted to the cache host.</param>
30+
/// <param name="messageBufferSize">The message buffer size.</param>
31+
public CacheHostEngine(IMemCache memCache, ILogger logger, int port, int physicalMemoryLimitPercentage = 85, int maximumConnections = 20, int messageBufferSize = 1024)
2132
{
2233
// Sanitize
23-
if (cacheHostInformationPoller == null)
34+
if (memCache == null)
2435
{
25-
throw new ArgumentNullException("cacheHostInformationPoller");
36+
throw new ArgumentNullException("memCache");
2637
}
27-
if (cacheHostServer == null)
38+
if (logger == null)
2839
{
29-
throw new ArgumentNullException("cacheHostServer");
40+
throw new ArgumentNullException("logger");
3041
}
42+
if (port <= 0)
43+
{
44+
throw new ArgumentException("cannot be <= 0", "port");
45+
}
46+
if (physicalMemoryLimitPercentage < 5 || physicalMemoryLimitPercentage > 90)
47+
{
48+
throw new ArgumentException("must be >= 5 and <= 90", "physicalMemoryLimitPercentage");
49+
}
50+
if (maximumConnections <= 0)
51+
{
52+
throw new ArgumentException("cannot be <= 0", "maximumConnections");
53+
}
54+
if (messageBufferSize < 32 || messageBufferSize > 32768)
55+
{
56+
throw new ArgumentException("must be >= 32 and <= 32768", "messageBufferSize");
57+
}
58+
59+
// Configure the custom performance counter manager
60+
var customPerformanceCounterManager = new CustomPerformanceCounterManager(port, false);
61+
62+
// Initialize the tag routing table
63+
var tagRoutingTable = new TagRoutingTable();
3164

32-
// Set the cache host information poller
33-
_cacheHostInformationPoller = cacheHostInformationPoller;
65+
// Initialize the cache host server
66+
var cacheHostServer = new CacheHostServer(memCache, tagRoutingTable, logger, port, maximumConnections, messageBufferSize);
3467

35-
// Initialize the serer
36-
_cacheServer = cacheHostServer;
68+
// Initialize the cache host information poller
69+
var cacheHostInformationPoller = new CacheHostInformationPoller(memCache, customPerformanceCounterManager, 1000);
70+
71+
// Instantiate the cache host runner
72+
_cacheHostRunner = new CacheHostRunner(cacheHostInformationPoller, cacheHostServer);
3773
}
3874

3975
/// <summary>
40-
/// Starts the cache host engine.
76+
/// Starts the cache host.
4177
/// </summary>
4278
public void Start()
4379
{
44-
// Begin listening for requests
45-
_cacheServer.Start();
46-
47-
// Start the cache host information poller
48-
_cacheHostInformationPoller.Start();
80+
_cacheHostRunner.Start();
4981
}
5082

5183
/// <summary>
52-
/// Stops the cache host engine.
84+
/// Stops the cache host.
5385
/// </summary>
5486
public void Stop()
5587
{
56-
// Stop listening for requests
57-
_cacheServer.Stop();
58-
59-
// Stop the cache host information poller
60-
_cacheHostInformationPoller.Stop();
88+
_cacheHostRunner.Stop();
6189
}
6290
}
6391
}

Dache.CacheHost/CacheHostRunner.cs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
3+
namespace Dache.CacheHost
4+
{
5+
/// <summary>
6+
/// Runs the cache host.
7+
/// </summary>
8+
internal class CacheHostRunner : IRunnable
9+
{
10+
// The cache server
11+
private readonly IRunnable _cacheServer = null;
12+
// The cache host information poller
13+
private readonly IRunnable _cacheHostInformationPoller = null;
14+
15+
/// <summary>
16+
/// The constructor.
17+
/// </summary>
18+
/// <param name="cacheHostInformationPoller">The cache host information poller.</param>
19+
/// <param name="cacheHostServer">The cache host server.</param>
20+
public CacheHostRunner(IRunnable cacheHostInformationPoller, IRunnable cacheHostServer)
21+
{
22+
// Sanitize
23+
if (cacheHostInformationPoller == null)
24+
{
25+
throw new ArgumentNullException("cacheHostInformationPoller");
26+
}
27+
if (cacheHostServer == null)
28+
{
29+
throw new ArgumentNullException("cacheHostServer");
30+
}
31+
32+
// Set the cache host information poller
33+
_cacheHostInformationPoller = cacheHostInformationPoller;
34+
35+
// Initialize the serer
36+
_cacheServer = cacheHostServer;
37+
}
38+
39+
/// <summary>
40+
/// Starts the cache host.
41+
/// </summary>
42+
public void Start()
43+
{
44+
// Begin listening for requests
45+
_cacheServer.Start();
46+
47+
// Start the cache host information poller
48+
_cacheHostInformationPoller.Start();
49+
}
50+
51+
/// <summary>
52+
/// Stops the cache host.
53+
/// </summary>
54+
public void Stop()
55+
{
56+
// Stop listening for requests
57+
_cacheServer.Stop();
58+
59+
// Stop the cache host information poller
60+
_cacheHostInformationPoller.Stop();
61+
}
62+
}
63+
}

Dache.CacheHost/Communication/CacheHostServer.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Net.Sockets;
88
using System.Runtime.Caching;
99
using Dache.CacheHost;
10-
using Dache.CacheHost.Configuration;
1110
using Dache.CacheHost.Routing;
1211
using Dache.CacheHost.Storage;
1312
using Dache.Core.Logging;
@@ -18,7 +17,7 @@ namespace Dache.Core.Communication
1817
/// <summary>
1918
/// The server for client to cache communication.
2019
/// </summary>
21-
public class CacheHostServer : ICacheHostContract, IRunnable
20+
internal class CacheHostServer : ICacheHostContract, IRunnable
2221
{
2322
// The mem cache
2423
private readonly IMemCache _memCache;
@@ -44,10 +43,11 @@ public class CacheHostServer : ICacheHostContract, IRunnable
4443
/// </summary>
4544
/// <param name="memCache">The mem cache.</param>
4645
/// <param name="tagRoutingTable">The tag routing table.</param>
46+
/// <param name="logger">The logger.</param>
4747
/// <param name="port">The port.</param>
4848
/// <param name="maximumConnections">The maximum number of simultaneous connections.</param>
4949
/// <param name="messageBufferSize">The buffer size to use for sending and receiving data.</param>
50-
public CacheHostServer(IMemCache memCache, ITagRoutingTable tagRoutingTable, int port, int maximumConnections, int messageBufferSize)
50+
public CacheHostServer(IMemCache memCache, ITagRoutingTable tagRoutingTable, ILogger logger, int port, int maximumConnections, int messageBufferSize)
5151
{
5252
// Sanitize
5353
if (memCache == null)
@@ -58,6 +58,10 @@ public CacheHostServer(IMemCache memCache, ITagRoutingTable tagRoutingTable, int
5858
{
5959
throw new ArgumentNullException("tagRoutingTable");
6060
}
61+
if (logger == null)
62+
{
63+
throw new ArgumentNullException("logger");
64+
}
6165
if (port <= 0)
6266
{
6367
throw new ArgumentException("cannot be <= 0", "port");
@@ -75,6 +79,8 @@ public CacheHostServer(IMemCache memCache, ITagRoutingTable tagRoutingTable, int
7579
_memCache = memCache;
7680
// Set the tag routing table
7781
_tagRoutingTable = tagRoutingTable;
82+
// Set the logger
83+
_logger = logger;
7884

7985
// Set maximum connections and message buffer size
8086
_maximumConnections = maximumConnections;
@@ -88,9 +94,6 @@ public CacheHostServer(IMemCache memCache, ITagRoutingTable tagRoutingTable, int
8894
// Define the server
8995
_server = SimplSocket.CreateServer(() => new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp),
9096
(sender, e) => { /* Ignore it, client's toast */ }, ReceiveMessage, messageBufferSize, maximumConnections, false);
91-
92-
// Load custom logging
93-
_logger = CustomLoggerLoader.LoadLogger();
9497
}
9598

9699
private void ReceiveMessage(object sender, MessageReceivedArgs e)

Dache.CacheHost/Dache.CacheHost.csproj

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
66
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
77
<ProjectGuid>{1B9FCBF1-DFCA-4911-8A2D-345D45C77FE6}</ProjectGuid>
8-
<OutputType>WinExe</OutputType>
8+
<OutputType>Library</OutputType>
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>Dache.CacheHost</RootNamespace>
1111
<AssemblyName>Dache.CacheHost</AssemblyName>
@@ -38,6 +38,9 @@
3838
<Prefer32Bit>false</Prefer32Bit>
3939
<DocumentationFile>bin\Release\Dache.CacheHost.xml</DocumentationFile>
4040
</PropertyGroup>
41+
<PropertyGroup>
42+
<StartupObject />
43+
</PropertyGroup>
4144
<ItemGroup>
4245
<Reference Include="SharpMemoryCache">
4346
<HintPath>..\packages\SharpMemoryCache.1.0.0\lib\SharpMemoryCache.dll</HintPath>
@@ -47,24 +50,15 @@
4750
<HintPath>..\packages\SimplSockets.1.1.2\lib\SimplSockets.dll</HintPath>
4851
</Reference>
4952
<Reference Include="System" />
50-
<Reference Include="System.configuration" />
51-
<Reference Include="System.Configuration.Install" />
5253
<Reference Include="System.Core" />
5354
<Reference Include="System.Net" />
5455
<Reference Include="System.Runtime.Caching" />
5556
<Reference Include="System.ServiceProcess" />
5657
</ItemGroup>
5758
<ItemGroup>
5859
<Compile Include="CacheHostEngine.cs" />
59-
<Compile Include="CacheHostService.cs">
60-
<SubType>Component</SubType>
61-
</Compile>
60+
<Compile Include="CacheHostRunner.cs" />
6261
<Compile Include="Communication\CacheHostServer.cs" />
63-
<Compile Include="Configuration\CacheHostConfigurationSection.cs" />
64-
<Compile Include="..\Dache.Client\Configuration\CustomTypeElement.cs">
65-
<Link>Configuration\CustomTypeElement.cs</Link>
66-
</Compile>
67-
<Compile Include="Configuration\CustomLoggerLoader.cs" />
6862
<Compile Include="IRunnable.cs" />
6963
<Compile Include="Persistence\DiskDataPersister.cs" />
7064
<Compile Include="Persistence\IDataPersister.cs" />
@@ -74,9 +68,6 @@
7468
<Compile Include="..\SharedAssemblyInfo.cs">
7569
<Link>Properties\SharedAssemblyInfo.cs</Link>
7670
</Compile>
77-
<Compile Include="CustomInstaller.cs">
78-
<SubType>Component</SubType>
79-
</Compile>
8071
<Compile Include="Routing\ITagRoutingTable.cs" />
8172
<Compile Include="Routing\TagRoutingTable.cs" />
8273
<Compile Include="Storage\GZipMemCache.cs" />
@@ -90,16 +81,7 @@
9081
</ProjectReference>
9182
</ItemGroup>
9283
<ItemGroup>
93-
<None Include="App.config">
94-
<SubType>Designer</SubType>
95-
</None>
96-
<None Include="install.bat">
97-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
98-
</None>
9984
<None Include="packages.config" />
100-
<None Include="uninstall.bat">
101-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
102-
</None>
10385
</ItemGroup>
10486
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
10587
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

Dache.CacheHost/IRunnable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/// <summary>
44
/// Represents a runnable object.
55
/// </summary>
6-
public interface IRunnable
6+
internal interface IRunnable
77
{
88
/// <summary>
99
/// Starts running.

Dache.CacheHost/Polling/CacheHostInformationPoller.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Dache.CacheHost.Polling
99
/// <summary>
1010
/// Polls cache hosts for information.
1111
/// </summary>
12-
public class CacheHostInformationPoller : IRunnable
12+
internal class CacheHostInformationPoller : IRunnable
1313
{
1414
// The mem cache
1515
private readonly IMemCache _memCache = null;

Dache.CacheHost/Storage/MemCache.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,11 @@ public class MemCache : IMemCache
3737
/// <summary>
3838
/// The constructor.
3939
/// </summary>
40-
/// <param name="cacheName">The name of the cache.</param>
4140
/// <param name="physicalMemoryLimitPercentage">The cache memory limit, as a percentage of the total system memory.</param>
4241
/// <param name="customPerformanceCounterManager">The custom performance counter manager.</param>
43-
public MemCache(string cacheName, int physicalMemoryLimitPercentage, ICustomPerformanceCounterManager customPerformanceCounterManager)
42+
public MemCache(int physicalMemoryLimitPercentage, ICustomPerformanceCounterManager customPerformanceCounterManager)
4443
{
4544
// Sanitize
46-
if (string.IsNullOrWhiteSpace(cacheName))
47-
{
48-
throw new ArgumentNullException("cacheName");
49-
}
5045
if (physicalMemoryLimitPercentage <= 0)
5146
{
5247
throw new ArgumentException("cannot be <= 0", "physicalMemoryLimitPercentage");
@@ -56,7 +51,7 @@ public MemCache(string cacheName, int physicalMemoryLimitPercentage, ICustomPerf
5651
throw new ArgumentNullException("customPerformanceCounterManager");
5752
}
5853

59-
_cacheName = cacheName;
54+
_cacheName = "Dache";
6055
_cacheConfig = new NameValueCollection();
6156
_cacheConfig.Add("pollingInterval", "00:00:05");
6257
_cacheConfig.Add("cacheMemoryLimitMegabytes", "0");
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<configuration>
33
<!-- Config Sections -->
44
<configSections>
5-
<section name="cacheHostSettings" type="Dache.CacheHost.Configuration.CacheHostConfigurationSection, Dache.CacheHost" allowExeDefinition="MachineToApplication" />
5+
<section name="cacheHostSettings" type="Dache.CacheHostService.Configuration.CacheHostConfigurationSection, Dache.CacheHostService" allowExeDefinition="MachineToApplication" />
66
</configSections>
77
<!-- Cache Host Settings -->
88
<cacheHostSettings port="33333" maximumConnections="20" cacheMemoryLimitPercentage="85" />

Dache.CacheHost/Configuration/CacheHostConfigurationSection.cs renamed to Dache.CacheHostService/Configuration/CacheHostConfigurationSection.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
using System.ComponentModel;
33
using System.Configuration;
44
using Dache.CacheHost.Storage;
5-
using Dache.Client.Configuration;
65

7-
namespace Dache.CacheHost.Configuration
6+
namespace Dache.CacheHostService.Configuration
87
{
98
/// <summary>
109
/// An application configuration section that allows a user to specify cache host settings.
@@ -72,7 +71,7 @@ public int MaximumConnections
7271
}
7372

7473
/// <summary>
75-
/// The cache memory limit, as a percentage of the total system memory. Valid range is 20 to 90.
74+
/// The cache memory limit, as a percentage of the total system memory. Valid range is 5 to 90.
7675
/// </summary>
7776
[IntegerValidator(MinValue = 5, MaxValue = 90)]
7877
[ConfigurationProperty("cacheMemoryLimitPercentage", IsRequired = true, DefaultValue = 80)]

Dache.CacheHost/Configuration/CustomLoggerLoader.cs renamed to Dache.CacheHostService/Configuration/CustomLoggerLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using Dache.Core.Logging;
33

4-
namespace Dache.CacheHost.Configuration
4+
namespace Dache.CacheHostService.Configuration
55
{
66
/// <summary>
77
/// Loads custom logger from configuration.

0 commit comments

Comments
 (0)