Skip to content

Commit

Permalink
Allow to load cache configuration from web.config
Browse files Browse the repository at this point in the history
  • Loading branch information
tatarincev committed Apr 21, 2017
1 parent 5bc7c42 commit 0b13882
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
23 changes: 16 additions & 7 deletions VirtoCommerce.Platform.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Web.Optimization;
using System.Web.Routing;
using CacheManager.Core;
using CacheManager.Core.Configuration;
using Common.Logging;
using Hangfire;
using Microsoft.ApplicationInsights.Extensibility;
Expand Down Expand Up @@ -326,14 +327,22 @@ private static void InitializePlatform(IAppBuilder app, IUnityContainer containe
var moduleCatalog = container.Resolve<IModuleCatalog>();

#region Caching
var cacheManager = CacheFactory.Build("platformCache", settings =>
ICacheManager<object> cacheManager = null;
//Try to load cache configuration from web.config first
if (ConfigurationManager.GetSection(CacheManagerSection.DefaultSectionName) != null)
{
//Should be aware to using Web cache cache handle because it not worked in native threads. (Hangfire jobs)
settings
.WithUpdateMode(CacheUpdateMode.Up)
.WithSystemRuntimeCacheHandle("memCacheHandle")
.WithExpiration(ExpirationMode.Absolute, TimeSpan.FromDays(1));
});
cacheManager = CacheFactory.FromConfiguration<object>("platformCache");
}
else
{
cacheManager = CacheFactory.Build("platformCache", settings =>
{
//Should be aware to using Web cache cache handle because it not worked in native threads. (Hangfire jobs)
settings.WithUpdateMode(CacheUpdateMode.Up)
.WithSystemRuntimeCacheHandle("memCacheHandle")
.WithExpiration(ExpirationMode.Absolute, TimeSpan.FromDays(1));
});
}
container.RegisterInstance(cacheManager);
#endregion

Expand Down
26 changes: 19 additions & 7 deletions VirtoCommerce.Platform.Web/Web.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="cacheManager" type="CacheManager.Core.Configuration.CacheManagerSection, CacheManager.Core" />
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" />
<sectionGroup name="common">
Expand Down Expand Up @@ -58,6 +59,23 @@
<add address="*" maxconnection="96" />
</connectionManagement>
</system.net>
<cacheManager>
<managers>
<cache name="platformCache" updateMode="Up" enableStatistics="false" enablePerformanceCounters="false">
<handle ref="memCacheHandle" name="memCacheHandle" expirationMode="Absolute" timeout="30m" />
</cache>
</managers>
<cacheHandles>
<handleDef id="memCacheHandle" type="CacheManager.SystemRuntimeCaching.MemoryCacheHandle`1, CacheManager.SystemRuntimeCaching"/>
</cacheHandles>
</cacheManager>
<system.runtime.caching>
<memoryCache>
<namedCaches>
<add name="memCacheHandle" physicalMemoryLimitPercentage="80" pollingInterval="00:00:30" />
</namedCaches>
</memoryCache>
</system.runtime.caching>
<system.web>
<compilation targetFramework="4.6.1" debug="true" />
<!-- 50MB in kilobytes, default is 4096 or 4MB-->
Expand Down Expand Up @@ -109,13 +127,7 @@
</requestFiltering>
</security>
</system.webServer>
<system.runtime.caching>
<memoryCache>
<namedCaches>
<add name="memCacheHandle" physicalMemoryLimitPercentage="80" pollingInterval="00:00:30" />
</namedCaches>
</memoryCache>
</system.runtime.caching>

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="App_Data\Modules" />
Expand Down

0 comments on commit 0b13882

Please sign in to comment.