From e14d3fd3793aa4bb02e4d2f3ebae1b95407639bd Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Wed, 3 Apr 2013 22:34:40 +0600 Subject: [PATCH] changes all instances accessing cache by key "contentItem" to use CacheKeys.ContentItemCache and replaces all of it's cache usages with the ApplicationContext.Current.ApplicationCache. Obsoletes a couple of methods that are not used. --- src/Umbraco.Core/Cache/CacheKeys.cs | 2 + src/Umbraco.Core/Cache/CacheProviderBase.cs | 1 + .../Cache/HttpRuntimeCacheProvider.cs | 14 ++++++ src/Umbraco.Core/Cache/NullCacheProvider.cs | 4 ++ src/Umbraco.Core/CacheHelper.cs | 21 +++++++++ src/Umbraco.Web/Media/ImageUrl.cs | 10 ++--- .../umbraco.presentation/content.cs | 20 ++------- .../umbraco/templateControls/ItemRenderer.cs | 45 ++++++++++++------- 8 files changed, 77 insertions(+), 40 deletions(-) diff --git a/src/Umbraco.Core/Cache/CacheKeys.cs b/src/Umbraco.Core/Cache/CacheKeys.cs index 7bb30ff8f1..3c89146198 100644 --- a/src/Umbraco.Core/Cache/CacheKeys.cs +++ b/src/Umbraco.Core/Cache/CacheKeys.cs @@ -6,6 +6,8 @@ namespace Umbraco.Core.Cache /// public static class CacheKeys { + public const string ContentItemCacheKey = "contentItem"; + public const string MediaCacheKey = "UL_GetMedia"; public const string MacroCacheKey = "UmbracoMacroCache"; diff --git a/src/Umbraco.Core/Cache/CacheProviderBase.cs b/src/Umbraco.Core/Cache/CacheProviderBase.cs index 3bcb7373c1..ca06211c00 100644 --- a/src/Umbraco.Core/Cache/CacheProviderBase.cs +++ b/src/Umbraco.Core/Cache/CacheProviderBase.cs @@ -26,6 +26,7 @@ namespace Umbraco.Core.Cache 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, 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 af67d2959c..b241273358 100644 --- a/src/Umbraco.Core/Cache/HttpRuntimeCacheProvider.cs +++ b/src/Umbraco.Core/Cache/HttpRuntimeCacheProvider.cs @@ -277,6 +277,20 @@ namespace Umbraco.Core.Cache return result.TryConvertTo().Result; } + /// + /// Inserts an item into the cache, if it already exists in the cache it will be replaced + /// + /// + /// + /// + /// + public override void InsertCacheItem(string cacheKey, + CacheItemPriority priority, + Func getCacheItem) + { + InsertCacheItem(cacheKey, priority, null, null, null, getCacheItem); + } + /// /// Inserts an item into the cache, if it already exists in the cache it will be replaced /// diff --git a/src/Umbraco.Core/Cache/NullCacheProvider.cs b/src/Umbraco.Core/Cache/NullCacheProvider.cs index 8178b95278..d6cc3eb344 100644 --- a/src/Umbraco.Core/Cache/NullCacheProvider.cs +++ b/src/Umbraco.Core/Cache/NullCacheProvider.cs @@ -66,6 +66,10 @@ namespace Umbraco.Core.Cache return getCacheItem(); } + public override void InsertCacheItem(string cacheKey, CacheItemPriority priority, Func getCacheItem) + { + } + public override void InsertCacheItem(string cacheKey, CacheItemPriority priority, TimeSpan timeout, Func getCacheItem) { } diff --git a/src/Umbraco.Core/CacheHelper.cs b/src/Umbraco.Core/CacheHelper.cs index bcde711df3..64031f4fc3 100644 --- a/src/Umbraco.Core/CacheHelper.cs +++ b/src/Umbraco.Core/CacheHelper.cs @@ -272,6 +272,27 @@ namespace Umbraco.Core return _httpCache.GetCacheItem(cacheKey, priority, refreshAction, cacheDependency, timeout, getCacheItem); } } + + /// + /// Inserts an item into the cache, if it already exists in the cache it will be replaced + /// + /// + /// + /// + /// + public void InsertCacheItem(string cacheKey, + CacheItemPriority priority, + Func getCacheItem) + { + if (!_enableCache) + { + _nullCache.InsertCacheItem(cacheKey, priority, getCacheItem); + } + else + { + _httpCache.InsertCacheItem(cacheKey, priority, getCacheItem); + } + } /// /// Inserts an item into the cache, if it already exists in the cache it will be replaced diff --git a/src/Umbraco.Web/Media/ImageUrl.cs b/src/Umbraco.Web/Media/ImageUrl.cs index 6fcbb4fda8..348de10d26 100644 --- a/src/Umbraco.Web/Media/ImageUrl.cs +++ b/src/Umbraco.Web/Media/ImageUrl.cs @@ -4,6 +4,8 @@ using System.Collections.Specialized; using System.Globalization; using System.Linq; using System.Web; +using Umbraco.Core; +using Umbraco.Core.Cache; using Umbraco.Core.Media; using umbraco; @@ -72,12 +74,8 @@ namespace Umbraco.Web.Media private static object GetContentFromCache(int nodeIdInt, string field) { - var context = HttpContext.Current; - - if (context == null) - return string.Empty; - - var content = context.Cache[String.Format("contentItem{0}_{1}", nodeIdInt.ToString(CultureInfo.InvariantCulture), field)]; + var content = ApplicationContext.Current.ApplicationCache.GetCacheItem( + string.Format("{0}{1}_{2}", CacheKeys.ContentItemCacheKey, nodeIdInt.ToString(CultureInfo.InvariantCulture), field)); return content; } } diff --git a/src/Umbraco.Web/umbraco.presentation/content.cs b/src/Umbraco.Web/umbraco.presentation/content.cs index 32200560a5..3117fee330 100644 --- a/src/Umbraco.Web/umbraco.presentation/content.cs +++ b/src/Umbraco.Web/umbraco.presentation/content.cs @@ -497,23 +497,9 @@ namespace umbraco ClearContextCache(); } - // clear cached field values - if (HttpContext.Current != null) - { - System.Web.Caching.Cache httpCache = HttpContext.Current.Cache; - string cachedFieldKeyStart = String.Format("contentItem{0}_", d.Id); - var foundKeys = new List(); - foreach (DictionaryEntry cacheItem in httpCache) - { - string key = cacheItem.Key.ToString(); - if (key.StartsWith(cachedFieldKeyStart)) - foundKeys.Add(key); - } - foreach (string foundKey in foundKeys) - { - httpCache.Remove(foundKey); - } - } + var cachedFieldKeyStart = string.Format("{0}{1}_", CacheKeys.ContentItemCacheKey, d.Id); + ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch(cachedFieldKeyStart); + Action.RunActionHandlers(d, ActionPublish.Instance); FireAfterUpdateDocumentCache(d, e); diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/templateControls/ItemRenderer.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/templateControls/ItemRenderer.cs index 0c328e17a2..01a321d899 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/templateControls/ItemRenderer.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/templateControls/ItemRenderer.cs @@ -5,8 +5,11 @@ using System.IO; using System.Linq; using System.Text; using System.Web; +using System.Web.Caching; using System.Web.UI; using System.Xml; +using Umbraco.Core; +using Umbraco.Core.Cache; using Umbraco.Core.Macros; using Umbraco.Web; using Umbraco.Web.PublishedCache; @@ -229,29 +232,31 @@ namespace umbraco.presentation.templateControls return item.TextIfEmpty; } - /// - /// Gets the field content from database instead of the published XML via the APIs. - /// - /// The node id. - /// The field that should be fetched. - /// The contents of the from the content object + /// + /// Gets the field content from database instead of the published XML via the APIs. + /// + /// + /// The node id. + /// The field that should be fetched. + /// The contents of the from the content object + [Obsolete("This is no longer used in the codebase and will be removed in future versions")] protected virtual string GetContentFromDatabase(AttributeCollectionAdapter itemAttributes, int nodeIdInt, string currentField) { - Content c = new Content(nodeIdInt); + var c = new Content(nodeIdInt); - Property property = c.getProperty(currentField); + var property = c.getProperty(currentField); if (property == null) throw new ArgumentException(String.Format("Could not find property {0} of node {1}.", currentField, nodeIdInt)); - item umbItem = new item(property.Value.ToString(), itemAttributes); - string tempElementContent = umbItem.FieldContent; + var umbItem = new item(property.Value.ToString(), itemAttributes); + var tempElementContent = umbItem.FieldContent; // If the current content object is a document object, we'll only output it if it's published - if (c.nodeObjectType == cms.businesslogic.web.Document._objectType) + if (c.nodeObjectType == Document._objectType) { try { - Document d = (Document)c; + var d = (Document)c; if (!d.Published) tempElementContent = ""; } @@ -259,9 +264,13 @@ namespace umbraco.presentation.templateControls } // Add the content to the cache - if (!String.IsNullOrEmpty(tempElementContent)) - HttpContext.Current.Cache.Insert(String.Format("contentItem{0}_{1}", nodeIdInt.ToString(), currentField), tempElementContent); - return tempElementContent; + if (!string.IsNullOrEmpty(tempElementContent)) + { + ApplicationContext.Current.ApplicationCache.InsertCacheItem( + string.Format("{0}{1}_{2}", CacheKeys.ContentItemCacheKey, nodeIdInt, currentField), + CacheItemPriority.Default, () => tempElementContent); + } + return tempElementContent; } /// @@ -269,10 +278,12 @@ namespace umbraco.presentation.templateControls /// /// The node id. /// The field. - /// The cached contents of the from the content object + /// The cached contents of the from the content object + [Obsolete("This is no longer used in the codebase and will be removed in future versions")] protected virtual object GetContentFromCache(int nodeIdInt, string field) { - object content = HttpContext.Current.Cache[String.Format("contentItem{0}_{1}", nodeIdInt.ToString(), field.ToString())]; + var content = ApplicationContext.Current.ApplicationCache.GetCacheItem( + string.Format("{0}{1}_{2}", CacheKeys.ContentItemCacheKey, nodeIdInt, field)); return content; } }