Permalink
Browse files

support for custom memory cache via Raven/MemoryCacher setting

  • Loading branch information...
1 parent fb1055b commit 492d657bc47e178a9d69fe8c870046e4b978af05 @dubray dubray committed with ayende Jan 11, 2017
@@ -86,6 +86,7 @@ internal class ConfigOptionDocs : IEnumerable<string>
{"Raven/MemoryCacheLimitCheckInterval", "TimeSpan", "HH:MM:SS", "The internal for checking that the internal document cache inside RavenDB server will be cleaned."},
{"Raven/MemoryCacheExpiration", "int", null, "The expiration value for documents in the internal document cache. Value is in seconds. Default: 60 minutes"},
{"Raven/MemoryLimitForProcessing", "int", null, "Maximum number of megabytes that can be used by database to control the maximum size of the processing batches. Default: 1024 or 75% percent of available memory if 1GB is not available."},
+ {"Raven/MemoryCacher", "string", "assembly qualified type name", "The MemoryCacher type to use for caching database documents."},
// Esent
{"Raven/Esent/CacheSizeMax", "int", null, "The size in MB of the Esent page cache, which is the default storage engine. Default: 25% of RAM on 64 bits, 256 MB on 32 bits."},
@@ -11,6 +11,7 @@
using System.ComponentModel.Composition.Primitives;
using System.IO;
using System.Linq;
+using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
@@ -27,6 +28,7 @@
using Raven.Imports.Newtonsoft.Json;
using Enum = System.Enum;
using Raven.Abstractions;
+using Raven.Database.Impl;
namespace Raven.Database.Config
{
@@ -173,6 +175,24 @@ public InMemoryRavenConfiguration Initialize()
MemoryCacheLimitCheckInterval = ravenSettings.MemoryCacheLimitCheckInterval.Value;
+ if (!string.IsNullOrWhiteSpace(ravenSettings.MemoryCacher.Value))
+ {
+ CustomMemoryCacher = config =>
+ {
+ var customCacherType = Type.GetType(ravenSettings.MemoryCacher.Value);
+
+ var argTypes = new Type[] { typeof(InMemoryRavenConfiguration) };
+ var argValues = new object[] { config };
+
+ var ctor = customCacherType.GetConstructor(argTypes);
+ var obj = ctor.Invoke(argValues);
+
+ var cacher = obj as IDocumentCacher;
+
+ return cacher;
+ };
+ }
+
// Discovery
DisableClusterDiscovery = ravenSettings.DisableClusterDiscovery.Value;
@@ -1087,6 +1107,11 @@ public CompositionContainer Container
/// </summary>
public int MemoryLimitForProcessingInMb { get; set; }
+ /// <summary>
+ /// Custom MemoryCacher type to use for caching database documents.
+ /// </summary>
+ public Func<InMemoryRavenConfiguration, IDocumentCacher> CustomMemoryCacher { get; set; }
+
public long DynamicMemoryLimitForProcessing
{
get
@@ -19,13 +19,13 @@
namespace Raven.Database.Config
{
- internal interface ILowMemoryHandler
+ public interface ILowMemoryHandler
{
LowMemoryHandlerStatistics HandleLowMemory();
LowMemoryHandlerStatistics GetStats();
}
- internal static class MemoryStatistics
+ public static class MemoryStatistics
{
private static readonly ILog Log = LogManager.GetCurrentClassLogger();
@@ -123,6 +123,8 @@ public void Setup(int defaultMaxNumberOfItemsToIndexInSingleBatch, int defaultIn
new TimeSpanSetting(settings["Raven/MemoryCacheLimitCheckInterval"], MemoryCache.Default.PollingInterval,
TimeSpanArgumentType.FromParse);
+ MemoryCacher = new StringSetting(settings["Raven/MemoryCacher"], (string)null);
+
PrewarmFacetsSyncronousWaitTime =
new TimeSpanSetting(settings["Raven/PrewarmFacetsSyncronousWaitTime"], TimeSpan.FromSeconds(3),
TimeSpanArgumentType.FromParse);
@@ -423,6 +425,8 @@ private int GetDefaultMemoryCacheLimitMegabytes()
public TimeSpanSetting MemoryCacheLimitCheckInterval { get; private set; }
+ public StringSetting MemoryCacher { get; private set; }
+
public TimeSpanSetting MaxProcessingRunLatency { get; private set; }
public TimeSpanSetting PrewarmFacetsOnIndexingMaxAge { get; private set; }
@@ -88,10 +88,18 @@ public TransactionalStorage(InMemoryRavenConfiguration configuration, Action onC
configuration.Container.SatisfyImportsOnce(this);
if (configuration.CacheDocumentsInMemory == false)
+ {
documentCacher = new NullDocumentCacher();
+ }
+ else if (configuration.CustomMemoryCacher != null)
+ {
+ documentCacher = configuration.CustomMemoryCacher(configuration);
+ }
else
- documentCacher = new DocumentCacher(configuration);
-
+ {
+ documentCacher = new DocumentCacher(configuration);
+ }
+
database = configuration.DataDirectory;
this.configuration = configuration;
this.onCommit = onCommit;
@@ -852,9 +852,19 @@ private void Output(string message)
private IDocumentCacher CreateDocumentCacher(InMemoryRavenConfiguration configuration)
{
if (configuration.CacheDocumentsInMemory == false)
- return new NullDocumentCacher();
+ {
+ documentCacher = new NullDocumentCacher();
+ }
+ else if (configuration.CustomMemoryCacher != null)
+ {
+ documentCacher = configuration.CustomMemoryCacher(configuration);
+ }
+ else
+ {
+ documentCacher = new DocumentCacher(configuration);
+ }
- return new DocumentCacher(configuration);
+ return documentCacher;
}
}
}

0 comments on commit 492d657

Please sign in to comment.