PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Raven.Database/Impl/DocumentCacher.cs

https://github.com/barryhagan/ravendb
C# | 118 lines | 98 code | 11 blank | 9 comment | 3 complexity | 651ccac50807cbc1a015b3fb3e8ad9dd MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Specialized;
  3. using System.Runtime.Caching;
  4. using Raven.Abstractions.Data;
  5. using Raven.Abstractions.Extensions;
  6. using Raven.Abstractions.Logging;
  7. using Raven.Database.Config;
  8. using Raven.Json.Linq;
  9. namespace Raven.Database.Impl
  10. {
  11. public class DocumentCacher : IDocumentCacher
  12. {
  13. private readonly InMemoryRavenConfiguration configuration;
  14. private readonly MemoryCache cachedSerializedDocuments;
  15. private static readonly ILog log = LogManager.GetCurrentClassLogger();
  16. [ThreadStatic]
  17. private static bool skipSettingDocumentInCache;
  18. public DocumentCacher(InMemoryRavenConfiguration configuration)
  19. {
  20. this.configuration = configuration;
  21. cachedSerializedDocuments = new MemoryCache(typeof(DocumentCacher).FullName + ".Cache", new NameValueCollection
  22. {
  23. {"physicalMemoryLimitPercentage", configuration.MemoryCacheLimitPercentage.ToString()},
  24. {"pollingInterval", configuration.MemoryCacheLimitCheckInterval.ToString(@"hh\:mm\:ss")},
  25. {"cacheMemoryLimitMegabytes", configuration.MemoryCacheLimitMegabytes.ToString()}
  26. });
  27. log.Info(@"MemoryCache Settings:
  28. PhysicalMemoryLimit = {0}
  29. CacheMemoryLimit = {1}
  30. PollingInterval = {2}", cachedSerializedDocuments.PhysicalMemoryLimit, cachedSerializedDocuments.CacheMemoryLimit,
  31. cachedSerializedDocuments.PollingInterval);
  32. }
  33. public static IDisposable SkipSettingDocumentsInDocumentCache()
  34. {
  35. var old = skipSettingDocumentInCache;
  36. skipSettingDocumentInCache = true;
  37. return new DisposableAction(() => skipSettingDocumentInCache = old);
  38. }
  39. public CachedDocument GetCachedDocument(string key, Etag etag)
  40. {
  41. CachedDocument cachedDocument;
  42. try
  43. {
  44. cachedDocument = (CachedDocument)cachedSerializedDocuments.Get("Doc/" + key + "/" + etag);
  45. }
  46. catch (OverflowException)
  47. {
  48. // this is a bug in the framework
  49. // http://connect.microsoft.com/VisualStudio/feedback/details/735033/memorycache-set-fails-with-overflowexception-exception-when-key-is-u7337-u7f01-u2117-exception-message-negating-the-minimum-value-of-a-twos-complement-number-is-invalid
  50. // in this case, we just threat it as uncachable
  51. return null;
  52. }
  53. if (cachedDocument == null)
  54. return null;
  55. return new CachedDocument
  56. {
  57. Document = (RavenJObject)cachedDocument.Document.CreateSnapshot(),
  58. Metadata = (RavenJObject)cachedDocument.Metadata.CreateSnapshot(),
  59. Size = cachedDocument.Size
  60. };
  61. }
  62. public void SetCachedDocument(string key, Etag etag, RavenJObject doc, RavenJObject metadata, int size)
  63. {
  64. if (skipSettingDocumentInCache)
  65. return;
  66. var documentClone = ((RavenJObject)doc.CloneToken());
  67. documentClone.EnsureCannotBeChangeAndEnableSnapshotting();
  68. var metadataClone = ((RavenJObject)metadata.CloneToken());
  69. metadataClone.EnsureCannotBeChangeAndEnableSnapshotting();
  70. try
  71. {
  72. cachedSerializedDocuments.Set("Doc/" + key + "/" + etag, new CachedDocument
  73. {
  74. Document = documentClone,
  75. Metadata = metadataClone,
  76. Size = size
  77. }, new CacheItemPolicy
  78. {
  79. SlidingExpiration = configuration.MemoryCacheExpiration,
  80. });
  81. }
  82. catch (OverflowException)
  83. {
  84. // this is a bug in the framework
  85. // http://connect.microsoft.com/VisualStudio/feedback/details/735033/memorycache-set-fails-with-overflowexception-exception-when-key-is-u7337-u7f01-u2117-exception-message-negating-the-minimum-value-of-a-twos-complement-number-is-invalid
  86. // in this case, we just threat it as uncachable
  87. }
  88. }
  89. public void RemoveCachedDocument(string key, Etag etag)
  90. {
  91. try
  92. {
  93. cachedSerializedDocuments.Remove("Doc/" + key + "/" + etag);
  94. }
  95. catch (OverflowException)
  96. {
  97. // this is a bug in the framework
  98. // http://connect.microsoft.com/VisualStudio/feedback/details/735033/memorycache-set-fails-with-overflowexception-exception-when-key-is-u7337-u7f01-u2117-exception-message-negating-the-minimum-value-of-a-twos-complement-number-is-invalid
  99. // in this case, we just threat it as uncachable
  100. }
  101. }
  102. public void Dispose()
  103. {
  104. cachedSerializedDocuments.Dispose();
  105. }
  106. }
  107. }