From 2a7e493c744e025ee1708fa2c0efcc826e4644df Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Wed, 3 Apr 2013 23:39:51 +0600 Subject: [PATCH] Changes all instances of the legacy Cache class to be using ApplicationContext.Current.ApplicationCache. Fixes: #U4-2039 - ensures macro xslt cache is invalidated in LB scenarios. --- src/Umbraco.Core/Cache/CacheKeys.cs | 1 + src/Umbraco.Core/Cache/CacheProviderBase.cs | 12 ++-- .../Cache/HttpRuntimeCacheProvider.cs | 12 ++-- src/Umbraco.Core/Cache/NullCacheProvider.cs | 12 ++-- src/Umbraco.Core/CacheHelper.cs | 24 ++++++++ src/Umbraco.Web/Cache/MacroCacheRefresher.cs | 3 +- src/Umbraco.Web/umbraco.presentation/macro.cs | 58 +++++++++---------- .../umbraco/cacheBrowser.aspx.cs | 6 +- .../umbraco/webservices/nodeSorter.asmx.cs | 2 +- 9 files changed, 76 insertions(+), 54 deletions(-) diff --git a/src/Umbraco.Core/Cache/CacheKeys.cs b/src/Umbraco.Core/Cache/CacheKeys.cs index 3c89146198..b022fbbe2e 100644 --- a/src/Umbraco.Core/Cache/CacheKeys.cs +++ b/src/Umbraco.Core/Cache/CacheKeys.cs @@ -10,6 +10,7 @@ namespace Umbraco.Core.Cache public const string MediaCacheKey = "UL_GetMedia"; + public const string MacroXsltCacheKey = "macroXslt_"; public const string MacroCacheKey = "UmbracoMacroCache"; public const string MacroHtmlCacheKey = "macroHtml_"; public const string MacroControlCacheKey = "macroControl_"; diff --git a/src/Umbraco.Core/Cache/CacheProviderBase.cs b/src/Umbraco.Core/Cache/CacheProviderBase.cs index ca06211c00..fafa0939f8 100644 --- a/src/Umbraco.Core/Cache/CacheProviderBase.cs +++ b/src/Umbraco.Core/Cache/CacheProviderBase.cs @@ -22,13 +22,13 @@ namespace Umbraco.Core.Cache public abstract IEnumerable GetCacheItemsByKeySearch(string keyStartsWith); public abstract T GetCacheItem(string cacheKey); public abstract T GetCacheItem(string cacheKey, Func getCacheItem); - public abstract T GetCacheItem(string cacheKey, TimeSpan timeout, Func getCacheItem); - public abstract T GetCacheItem(string cacheKey, CacheItemRemovedCallback refreshAction, TimeSpan timeout, Func getCacheItem); - public abstract T GetCacheItem(string cacheKey, CacheItemPriority priority, CacheItemRemovedCallback refreshAction, TimeSpan timeout, Func getCacheItem); - public abstract T GetCacheItem(string cacheKey, CacheItemPriority priority, CacheItemRemovedCallback refreshAction, CacheDependency cacheDependency, TimeSpan timeout, Func getCacheItem); + public abstract T GetCacheItem(string cacheKey, TimeSpan? timeout, Func getCacheItem); + public abstract T GetCacheItem(string cacheKey, CacheItemRemovedCallback refreshAction, TimeSpan? timeout, Func getCacheItem); + public abstract T GetCacheItem(string cacheKey, CacheItemPriority priority, CacheItemRemovedCallback refreshAction, TimeSpan? timeout, Func getCacheItem); + public abstract T GetCacheItem(string cacheKey, CacheItemPriority priority, CacheItemRemovedCallback refreshAction, CacheDependency cacheDependency, TimeSpan? timeout, Func getCacheItem); public abstract void InsertCacheItem(string cacheKey, CacheItemPriority priority, Func getCacheItem); - public abstract void InsertCacheItem(string cacheKey, CacheItemPriority priority, TimeSpan timeout, Func getCacheItem); - public abstract void InsertCacheItem(string cacheKey, CacheItemPriority priority, CacheDependency cacheDependency, TimeSpan timeout, Func getCacheItem); + public abstract void InsertCacheItem(string cacheKey, CacheItemPriority priority, TimeSpan? timeout, Func getCacheItem); + public abstract void InsertCacheItem(string cacheKey, CacheItemPriority priority, CacheDependency cacheDependency, TimeSpan? timeout, Func getCacheItem); public abstract void InsertCacheItem(string cacheKey, CacheItemPriority priority, CacheItemRemovedCallback refreshAction, CacheDependency cacheDependency, TimeSpan? timeout, Func getCacheItem); } } diff --git a/src/Umbraco.Core/Cache/HttpRuntimeCacheProvider.cs b/src/Umbraco.Core/Cache/HttpRuntimeCacheProvider.cs index b241273358..681dc8fab4 100644 --- a/src/Umbraco.Core/Cache/HttpRuntimeCacheProvider.cs +++ b/src/Umbraco.Core/Cache/HttpRuntimeCacheProvider.cs @@ -180,7 +180,7 @@ namespace Umbraco.Core.Cache /// /// public override TT GetCacheItem(string cacheKey, - TimeSpan timeout, Func getCacheItem) + TimeSpan? timeout, Func getCacheItem) { return GetCacheItem(cacheKey, null, timeout, getCacheItem); } @@ -195,7 +195,7 @@ namespace Umbraco.Core.Cache /// /// public override TT GetCacheItem(string cacheKey, - CacheItemRemovedCallback refreshAction, TimeSpan timeout, + CacheItemRemovedCallback refreshAction, TimeSpan? timeout, Func getCacheItem) { return GetCacheItem(cacheKey, CacheItemPriority.Normal, refreshAction, timeout, getCacheItem); @@ -212,7 +212,7 @@ namespace Umbraco.Core.Cache /// /// public override TT GetCacheItem(string cacheKey, - CacheItemPriority priority, CacheItemRemovedCallback refreshAction, TimeSpan timeout, + CacheItemPriority priority, CacheItemRemovedCallback refreshAction, TimeSpan? timeout, Func getCacheItem) { return GetCacheItem(cacheKey, priority, refreshAction, null, timeout, getCacheItem); @@ -233,7 +233,7 @@ namespace Umbraco.Core.Cache CacheItemPriority priority, CacheItemRemovedCallback refreshAction, CacheDependency cacheDependency, - TimeSpan timeout, + TimeSpan? timeout, Func getCacheItem) { return GetCacheItem(cacheKey, priority, refreshAction, cacheDependency, timeout, getCacheItem, Locker); @@ -301,7 +301,7 @@ namespace Umbraco.Core.Cache /// public override void InsertCacheItem(string cacheKey, CacheItemPriority priority, - TimeSpan timeout, + TimeSpan? timeout, Func getCacheItem) { InsertCacheItem(cacheKey, priority, null, null, timeout, getCacheItem); @@ -319,7 +319,7 @@ namespace Umbraco.Core.Cache public override void InsertCacheItem(string cacheKey, CacheItemPriority priority, CacheDependency cacheDependency, - TimeSpan timeout, + TimeSpan? timeout, Func getCacheItem) { InsertCacheItem(cacheKey, priority, null, cacheDependency, timeout, getCacheItem); diff --git a/src/Umbraco.Core/Cache/NullCacheProvider.cs b/src/Umbraco.Core/Cache/NullCacheProvider.cs index d6cc3eb344..3a41ae2a77 100644 --- a/src/Umbraco.Core/Cache/NullCacheProvider.cs +++ b/src/Umbraco.Core/Cache/NullCacheProvider.cs @@ -46,22 +46,22 @@ namespace Umbraco.Core.Cache return getCacheItem(); } - public override T GetCacheItem(string cacheKey, TimeSpan timeout, Func getCacheItem) + public override T GetCacheItem(string cacheKey, TimeSpan? timeout, Func getCacheItem) { return getCacheItem(); } - public override T GetCacheItem(string cacheKey, CacheItemRemovedCallback refreshAction, TimeSpan timeout, Func getCacheItem) + public override T GetCacheItem(string cacheKey, CacheItemRemovedCallback refreshAction, TimeSpan? timeout, Func getCacheItem) { return getCacheItem(); } - public override T GetCacheItem(string cacheKey, CacheItemPriority priority, CacheItemRemovedCallback refreshAction, TimeSpan timeout, Func getCacheItem) + public override T GetCacheItem(string cacheKey, CacheItemPriority priority, CacheItemRemovedCallback refreshAction, TimeSpan? timeout, Func getCacheItem) { return getCacheItem(); } - public override T GetCacheItem(string cacheKey, CacheItemPriority priority, CacheItemRemovedCallback refreshAction, CacheDependency cacheDependency, TimeSpan timeout, Func getCacheItem) + public override T GetCacheItem(string cacheKey, CacheItemPriority priority, CacheItemRemovedCallback refreshAction, CacheDependency cacheDependency, TimeSpan? timeout, Func getCacheItem) { return getCacheItem(); } @@ -70,11 +70,11 @@ namespace Umbraco.Core.Cache { } - public override void InsertCacheItem(string cacheKey, CacheItemPriority priority, TimeSpan timeout, Func getCacheItem) + public override void InsertCacheItem(string cacheKey, CacheItemPriority priority, TimeSpan? timeout, Func getCacheItem) { } - public override void InsertCacheItem(string cacheKey, CacheItemPriority priority, CacheDependency cacheDependency, TimeSpan timeout, Func getCacheItem) + public override void InsertCacheItem(string cacheKey, CacheItemPriority priority, CacheDependency cacheDependency, TimeSpan? timeout, Func getCacheItem) { } diff --git a/src/Umbraco.Core/CacheHelper.cs b/src/Umbraco.Core/CacheHelper.cs index 64031f4fc3..7c80a1e6d3 100644 --- a/src/Umbraco.Core/CacheHelper.cs +++ b/src/Umbraco.Core/CacheHelper.cs @@ -273,6 +273,30 @@ namespace Umbraco.Core } } + /// + /// Gets (and adds if necessary) an item from the cache + /// + /// + /// + /// + /// + /// + /// + public TT GetCacheItem(string cacheKey, + CacheItemPriority priority, + CacheDependency cacheDependency, + Func getCacheItem) + { + if (!_enableCache) + { + return _nullCache.GetCacheItem(cacheKey, priority, null, cacheDependency, null, getCacheItem); + } + else + { + return _httpCache.GetCacheItem(cacheKey, priority, null, cacheDependency, null, getCacheItem); + } + } + /// /// Inserts an item into the cache, if it already exists in the cache it will be replaced /// diff --git a/src/Umbraco.Web/Cache/MacroCacheRefresher.cs b/src/Umbraco.Web/Cache/MacroCacheRefresher.cs index e747149245..48005bd8c2 100644 --- a/src/Umbraco.Web/Cache/MacroCacheRefresher.cs +++ b/src/Umbraco.Web/Cache/MacroCacheRefresher.cs @@ -27,7 +27,8 @@ namespace Umbraco.Web.Cache CacheKeys.MacroControlCacheKey, CacheKeys.MacroHtmlCacheKey, CacheKeys.MacroHtmlDateAddedCacheKey, - CacheKeys.MacroControlDateAddedCacheKey + CacheKeys.MacroControlDateAddedCacheKey, + CacheKeys.MacroXsltCacheKey, }; } diff --git a/src/Umbraco.Web/umbraco.presentation/macro.cs b/src/Umbraco.Web/umbraco.presentation/macro.cs index b16b0d967f..56f651685b 100644 --- a/src/Umbraco.Web/umbraco.presentation/macro.cs +++ b/src/Umbraco.Web/umbraco.presentation/macro.cs @@ -162,15 +162,13 @@ namespace umbraco #endregion - private const string _xsltExtensionsCacheKey = "UmbracoXsltExtensions"; + private const string XsltExtensionsCacheKey = "UmbracoXsltExtensions"; - private static readonly string _xsltExtensionsConfig = + private static readonly string XsltExtensionsConfig = IOHelper.MapPath(SystemDirectories.Config + "/xsltExtensions.config"); - private static readonly object _xsltExtensionsSyncLock = new object(); - private static readonly Func _xsltExtensionsDependency = - () => new CacheDependency(_xsltExtensionsConfig); + () => new CacheDependency(XsltExtensionsConfig); /// /// Creates an empty macro object. @@ -693,22 +691,18 @@ namespace umbraco public static XslCompiledTransform getXslt(string XsltFile) { - if (HttpRuntime.Cache["macroXslt_" + XsltFile] != null) - { - return (XslCompiledTransform)HttpRuntime.Cache["macroXslt_" + XsltFile]; - } - else - { - var xslReader = - new XmlTextReader(IOHelper.MapPath(SystemDirectories.Xslt + "/" + XsltFile)); - - XslCompiledTransform macroXSLT = CreateXsltTransform(xslReader, GlobalSettings.DebugMode); - HttpRuntime.Cache.Insert( - "macroXslt_" + XsltFile, - macroXSLT, - new CacheDependency(IOHelper.MapPath(SystemDirectories.Xslt + "/" + XsltFile))); - return macroXSLT; - } + //TODO: SD: Do we really need to cache this?? + return ApplicationContext.Current.ApplicationCache.GetCacheItem( + CacheKeys.MacroXsltCacheKey + XsltFile, + CacheItemPriority.Default, + new CacheDependency(IOHelper.MapPath(SystemDirectories.Xslt + "/" + XsltFile)), + () => + { + using (var xslReader = new XmlTextReader(IOHelper.MapPath(SystemDirectories.Xslt.EnsureEndsWith('/') + XsltFile))) + { + return CreateXsltTransform(xslReader, GlobalSettings.DebugMode); + } + }); } public void UpdateMacroModel(Hashtable attributes) @@ -737,9 +731,11 @@ namespace umbraco public static XslCompiledTransform CreateXsltTransform(XmlTextReader xslReader, bool debugMode) { - var macroXSLT = new XslCompiledTransform(debugMode); - var xslResolver = new XmlUrlResolver(); - xslResolver.Credentials = CredentialCache.DefaultCredentials; + var macroXslt = new XslCompiledTransform(debugMode); + var xslResolver = new XmlUrlResolver + { + Credentials = CredentialCache.DefaultCredentials + }; xslReader.EntityHandling = EntityHandling.ExpandEntities; @@ -747,11 +743,11 @@ namespace umbraco { if (GlobalSettings.ApplicationTrustLevel > AspNetHostingPermissionLevel.Medium) { - macroXSLT.Load(xslReader, XsltSettings.TrustedXslt, xslResolver); + macroXslt.Load(xslReader, XsltSettings.TrustedXslt, xslResolver); } else { - macroXSLT.Load(xslReader, XsltSettings.Default, xslResolver); + macroXslt.Load(xslReader, XsltSettings.Default, xslResolver); } } finally @@ -759,13 +755,13 @@ namespace umbraco xslReader.Close(); } - return macroXSLT; + return macroXslt; } + [Obsolete("This is no longer used in the codebase and will be removed in future versions")] public static void unloadXslt(string XsltFile) { - if (HttpRuntime.Cache["macroXslt_" + XsltFile] != null) - HttpRuntime.Cache.Remove("macroXslt_" + XsltFile); + ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch(CacheKeys.MacroXsltCacheKey + XsltFile); } #region LoadMacroXslt @@ -1016,7 +1012,7 @@ namespace umbraco // Having this stuff in cache just adds to the gigantic amount of cache data and will cause more cache turnover to happen. return ApplicationContext.Current.ApplicationCache.GetCacheItem( - _xsltExtensionsCacheKey, + XsltExtensionsCacheKey, CacheItemPriority.NotRemovable, // NH 4.7.1, Changing to NotRemovable null, // no refresh action _xsltExtensionsDependency(), // depends on the .config file @@ -1031,7 +1027,7 @@ namespace umbraco // Load the XSLT extensions configuration var xsltExt = new XmlDocument(); - xsltExt.Load(_xsltExtensionsConfig); + xsltExt.Load(XsltExtensionsConfig); // add all descendants of the XsltExtensions element foreach (XmlNode xsltEx in xsltExt.SelectSingleNode("/XsltExtensions")) diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/cacheBrowser.aspx.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/cacheBrowser.aspx.cs index 8941939aa3..0e923ffc03 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/cacheBrowser.aspx.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/cacheBrowser.aspx.cs @@ -17,9 +17,9 @@ namespace umbraco.cms.presentation { // Cache removal checks if (Request.QueryString["clearByType"] != null) - cms.businesslogic.cache.Cache.ClearCacheObjectTypes(Request.QueryString["clearByType"]); + ApplicationContext.ApplicationCache.ClearCacheObjectTypes(Request.QueryString["clearByType"]); else if (Request.QueryString["clearByKey"] != null) - cms.businesslogic.cache.Cache.ClearCacheItem(Request.QueryString["clearByKey"]); + ApplicationContext.ApplicationCache.ClearCacheItem(Request.QueryString["clearByKey"]); // Put user code to initialize the page here Hashtable ht = cms.businesslogic.cache.Cache.ReturnCacheItemsOrdred(); @@ -33,7 +33,7 @@ namespace umbraco.cms.presentation } protected void Button1_Click(object sender, System.EventArgs e) { - cms.businesslogic.cache.Cache.ClearAllCache(); + ApplicationContext.ApplicationCache.ClearAllCache(); } protected System.Web.UI.HtmlControls.HtmlForm Form1; diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/webservices/nodeSorter.asmx.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/webservices/nodeSorter.asmx.cs index 146553b637..f308b999c9 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/webservices/nodeSorter.asmx.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/webservices/nodeSorter.asmx.cs @@ -129,7 +129,7 @@ namespace umbraco.presentation.webservices // Load balancing - then refresh entire cache - // NOTE: This seems a bit excessive to do simply for sorting! I'm going to leave this here for now but + // NOTE: SD: This seems a bit excessive to do simply for sorting! I'm going to leave this here for now but // the sort order should be updated in distributed calls when an item is Published (and it most likely is) // but I guess this was put here for a reason at some point. if (UmbracoSettings.UseDistributedCalls)