From 75ceb539b43e57dc735a68e083fcd4b03a9d8b7e Mon Sep 17 00:00:00 2001 From: Shannon Date: Fri, 9 Aug 2013 11:37:57 +1000 Subject: [PATCH] Streamlines cache helper ctor's --- src/Umbraco.Core/ApplicationContext.cs | 12 --- src/Umbraco.Core/CacheHelper.cs | 83 +++++++++++++++---- src/Umbraco.Core/CoreBootManager.cs | 9 +- .../Standalone/ServiceContextManager.cs | 9 +- .../Standalone/ServiceContextManager.cs | 14 +++- src/Umbraco.Web/WebBootManager.cs | 4 +- 6 files changed, 97 insertions(+), 34 deletions(-) diff --git a/src/Umbraco.Core/ApplicationContext.cs b/src/Umbraco.Core/ApplicationContext.cs index feafd8ed96..84cbb70448 100644 --- a/src/Umbraco.Core/ApplicationContext.cs +++ b/src/Umbraco.Core/ApplicationContext.cs @@ -38,18 +38,6 @@ namespace Umbraco.Core ApplicationCache = cache; } - /// - /// Constructor used to specify if we will enable application cache or not, used for unit tests - /// - /// - internal ApplicationContext(bool enableCache) - { - //create a new application cache from the HttpRuntime.Cache - ApplicationCache = HttpRuntime.Cache == null - ? new CacheHelper(new System.Web.Caching.Cache(), enableCache) - : new CacheHelper(HttpRuntime.Cache, enableCache); - } - /// /// Singleton accessor /// diff --git a/src/Umbraco.Core/CacheHelper.cs b/src/Umbraco.Core/CacheHelper.cs index 5cf0adb3da..8bb80670d8 100644 --- a/src/Umbraco.Core/CacheHelper.cs +++ b/src/Umbraco.Core/CacheHelper.cs @@ -25,31 +25,82 @@ namespace Umbraco.Core private readonly IRuntimeCacheProvider _httpCache; private readonly IRuntimeCacheProvider _nullHttpCache = new NullCacheProvider(); - public CacheHelper(System.Web.Caching.Cache cache) - : this(cache, true) - { - } - - internal CacheHelper(System.Web.Caching.Cache cache, bool enableCache) - : this(new HttpRuntimeCacheProvider(cache), enableCache) - { - } - - internal CacheHelper(IRuntimeCacheProvider httpCacheProvider, bool enableCache) - : this(httpCacheProvider, new StaticCacheProvider(), new HttpRequestCacheProvider(HttpContext.Current), enableCache) + /// + /// Creates a cache helper with disabled caches + /// + /// + /// + /// Good for unit testing + /// + internal static CacheHelper CreateDisabledCacheHelper() { + return new CacheHelper(null, null, null, false); } - internal CacheHelper( + /// + /// Initializes a new instance for use in the web + /// + public CacheHelper() + : this( + new HttpRuntimeCacheProvider(HttpRuntime.Cache), + new StaticCacheProvider(), + new HttpRequestCacheProvider(() => new HttpContextWrapper(HttpContext.Current))) + { + } + + /// + /// Initializes a new instance for use in the web + /// + /// + public CacheHelper(System.Web.Caching.Cache cache) + : this( + new HttpRuntimeCacheProvider(cache), + new StaticCacheProvider(), + new HttpRequestCacheProvider(() => new HttpContextWrapper(HttpContext.Current))) + { + } + + /// + /// Initializes a new instance based on the provided providers + /// + /// + /// + /// + public CacheHelper( IRuntimeCacheProvider httpCacheProvider, ICacheProvider staticCacheProvider, + ICacheProvider requestCacheProvider) + : this(httpCacheProvider, staticCacheProvider, requestCacheProvider, true) + { + } + + /// + /// Private ctor used for creating a disabled cache helper + /// + /// + /// + /// + /// + private CacheHelper( + IRuntimeCacheProvider httpCacheProvider, + ICacheProvider staticCacheProvider, ICacheProvider requestCacheProvider, bool enableCache) { - _httpCache = httpCacheProvider; - _staticCache = staticCacheProvider; + if (enableCache) + { + _httpCache = httpCacheProvider; + _staticCache = staticCacheProvider; + _requestCache = requestCacheProvider; + } + else + { + _httpCache = null; + _staticCache = null; + _requestCache = null; + } + _enableCache = enableCache; - _requestCache = requestCacheProvider; } /// diff --git a/src/Umbraco.Core/CoreBootManager.cs b/src/Umbraco.Core/CoreBootManager.cs index 92195c9271..6f195c6404 100644 --- a/src/Umbraco.Core/CoreBootManager.cs +++ b/src/Umbraco.Core/CoreBootManager.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Web; using AutoMapper; +using Umbraco.Core.Cache; using Umbraco.Core.Configuration; using Umbraco.Core.Logging; using Umbraco.Core.Models.Mapping; @@ -110,7 +111,13 @@ namespace Umbraco.Core /// protected virtual void CreateApplicationCache() { - ApplicationCache = new CacheHelper(new System.Web.Caching.Cache(), true); + var cacheHelper = new CacheHelper( + new ObjectCacheRuntimeCacheProvider(), + new StaticCacheProvider(), + //we have no request based cache when not running in web-based context + new NullCacheProvider()); + + ApplicationCache = cacheHelper; } /// diff --git a/src/Umbraco.Core/Standalone/ServiceContextManager.cs b/src/Umbraco.Core/Standalone/ServiceContextManager.cs index fb4c36d5c2..955c7a25df 100644 --- a/src/Umbraco.Core/Standalone/ServiceContextManager.cs +++ b/src/Umbraco.Core/Standalone/ServiceContextManager.cs @@ -1,5 +1,6 @@ using System; using System.Diagnostics; +using Umbraco.Core.Cache; using Umbraco.Core.Persistence; using Umbraco.Core.Persistence.Mappers; using Umbraco.Core.Persistence.UnitOfWork; @@ -45,6 +46,12 @@ namespace Umbraco.Core.Standalone { if (_serviceContext == null) { + var cacheHelper = new CacheHelper( + new ObjectCacheRuntimeCacheProvider(), + new StaticCacheProvider(), + //we have no request based cache when running standalone + new NullCacheProvider()); + var dbFactory = new DefaultDatabaseFactory(_connectionString, _providerName); var dbContext = new DatabaseContext(dbFactory); Database.Mapper = new PetaPocoMapper(); @@ -52,7 +59,7 @@ namespace Umbraco.Core.Standalone new PetaPocoUnitOfWorkProvider(dbFactory), new FileUnitOfWorkProvider(), new PublishingStrategy(), - new CacheHelper(new System.Web.Caching.Cache(), true)); + cacheHelper); //initialize the DatabaseContext dbContext.Initialize(_providerName); diff --git a/src/Umbraco.Web/Standalone/ServiceContextManager.cs b/src/Umbraco.Web/Standalone/ServiceContextManager.cs index 7773440d7a..561fadfc2c 100644 --- a/src/Umbraco.Web/Standalone/ServiceContextManager.cs +++ b/src/Umbraco.Web/Standalone/ServiceContextManager.cs @@ -1,6 +1,7 @@ using System; using System.Diagnostics; using Umbraco.Core; +using Umbraco.Core.Cache; using Umbraco.Core.Persistence; using Umbraco.Core.Persistence.Mappers; using Umbraco.Core.Persistence.UnitOfWork; @@ -34,6 +35,15 @@ namespace Umbraco.Web.Standalone { if (_serviceContext == null) { + var cacheHelper = new CacheHelper( + //SD: Not sure if this is correct? Should we be using HttpRuntime.Cache here since this is for 'Web' ? + // just not quite sure what this standalone stuff is. + new ObjectCacheRuntimeCacheProvider(), + new StaticCacheProvider(), + //SD: Not sure if this is correct? Should we be using request cache here since this is for 'Web' ? + // just not quite sure what this standalone stuff is. + new NullCacheProvider()); + var dbFactory = new DefaultDatabaseFactory(_connectionString, _providerName); var dbContext = new DatabaseContext(dbFactory); Database.Mapper = new PetaPocoMapper(); @@ -41,9 +51,7 @@ namespace Umbraco.Web.Standalone new PetaPocoUnitOfWorkProvider(dbFactory), new FileUnitOfWorkProvider(), new PublishingStrategy(), - //SD: Not sure if this is correct? Should we be using HttpRuntime.Cache here since this is for 'Web' ? - // just not quite sure what this standalone stuff is :) - new CacheHelper(new System.Web.Caching.Cache(), true)); + cacheHelper); //initialize the DatabaseContext dbContext.Initialize(_providerName); diff --git a/src/Umbraco.Web/WebBootManager.cs b/src/Umbraco.Web/WebBootManager.cs index 280f0969e2..6374b2cf1a 100644 --- a/src/Umbraco.Web/WebBootManager.cs +++ b/src/Umbraco.Web/WebBootManager.cs @@ -7,6 +7,7 @@ using System.Web.Mvc; using System.Web.Routing; using StackExchange.Profiling.MVCHelpers; using Umbraco.Core; +using Umbraco.Core.Cache; using Umbraco.Core.Configuration; using Umbraco.Core.Dictionary; using Umbraco.Core.Dynamics; @@ -150,7 +151,8 @@ namespace Umbraco.Web /// protected override void CreateApplicationCache() { - ApplicationCache = new CacheHelper(HttpRuntime.Cache, true); + //create a web-based cache helper + ApplicationCache = new CacheHelper(); } ///