|
1 | 1 | 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; |
2 | 11 |
|
3 | 12 | namespace Dache.CacheHost |
4 | 13 | { |
5 | 14 | /// <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. |
7 | 16 | /// </summary> |
8 | | - public class CacheHostEngine : IRunnable |
| 17 | + public class CacheHostEngine |
9 | 18 | { |
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; |
14 | 21 |
|
15 | 22 | /// <summary> |
16 | 23 | /// The constructor. |
17 | 24 | /// </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) |
21 | 32 | { |
22 | 33 | // Sanitize |
23 | | - if (cacheHostInformationPoller == null) |
| 34 | + if (memCache == null) |
24 | 35 | { |
25 | | - throw new ArgumentNullException("cacheHostInformationPoller"); |
| 36 | + throw new ArgumentNullException("memCache"); |
26 | 37 | } |
27 | | - if (cacheHostServer == null) |
| 38 | + if (logger == null) |
28 | 39 | { |
29 | | - throw new ArgumentNullException("cacheHostServer"); |
| 40 | + throw new ArgumentNullException("logger"); |
30 | 41 | } |
| 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(); |
31 | 64 |
|
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); |
34 | 67 |
|
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); |
37 | 73 | } |
38 | 74 |
|
39 | 75 | /// <summary> |
40 | | - /// Starts the cache host engine. |
| 76 | + /// Starts the cache host. |
41 | 77 | /// </summary> |
42 | 78 | public void Start() |
43 | 79 | { |
44 | | - // Begin listening for requests |
45 | | - _cacheServer.Start(); |
46 | | - |
47 | | - // Start the cache host information poller |
48 | | - _cacheHostInformationPoller.Start(); |
| 80 | + _cacheHostRunner.Start(); |
49 | 81 | } |
50 | 82 |
|
51 | 83 | /// <summary> |
52 | | - /// Stops the cache host engine. |
| 84 | + /// Stops the cache host. |
53 | 85 | /// </summary> |
54 | 86 | public void Stop() |
55 | 87 | { |
56 | | - // Stop listening for requests |
57 | | - _cacheServer.Stop(); |
58 | | - |
59 | | - // Stop the cache host information poller |
60 | | - _cacheHostInformationPoller.Stop(); |
| 88 | + _cacheHostRunner.Stop(); |
61 | 89 | } |
62 | 90 | } |
63 | 91 | } |
0 commit comments