diff --git a/src/Umbraco.Core/Cache/CacheKeys.cs b/src/Umbraco.Core/Cache/CacheKeys.cs index f0b84ef138..57922d36af 100644 --- a/src/Umbraco.Core/Cache/CacheKeys.cs +++ b/src/Umbraco.Core/Cache/CacheKeys.cs @@ -16,7 +16,8 @@ namespace Umbraco.Core.Cache public const string MemberCacheKey = "UL_GetMember"; - public const string TemplateCacheKey = "template"; + public const string TemplateFrontEndCacheKey = "template"; + public const string TemplateBusinessLogicCacheKey = "UmbracoTemplateCache"; public const string UserCacheKey = "UmbracoUser"; diff --git a/src/Umbraco.Web/Cache/CacheRefresherEventHandler.cs b/src/Umbraco.Web/Cache/CacheRefresherEventHandler.cs index f5fb90eafc..17d00ae99d 100644 --- a/src/Umbraco.Web/Cache/CacheRefresherEventHandler.cs +++ b/src/Umbraco.Web/Cache/CacheRefresherEventHandler.cs @@ -60,9 +60,12 @@ namespace Umbraco.Web.Cache User.Deleting += UserDeleting; //Bind to template events + //NOTE: we need to bind to legacy and new API events currently: http://issues.umbraco.org/issue/U4-1979 Template.AfterSave += TemplateAfterSave; Template.AfterDelete += TemplateAfterDelete; + FileService.SavedTemplate += FileServiceSavedTemplate; + FileService.DeletedTemplate += FileServiceDeletedTemplate; //Bind to macro events @@ -241,6 +244,27 @@ namespace Umbraco.Web.Cache #endregion #region Template event handlers + + /// + /// Removes cache for template + /// + /// + /// + static void FileServiceDeletedTemplate(IFileService sender, Core.Events.DeleteEventArgs e) + { + e.DeletedEntities.ForEach(x => DistributedCache.Instance.RemoveTemplateCache(x.Id)); + } + + /// + /// Refresh cache for template + /// + /// + /// + static void FileServiceSavedTemplate(IFileService sender, Core.Events.SaveEventArgs e) + { + e.SavedEntities.ForEach(x => DistributedCache.Instance.RefreshTemplateCache(x.Id)); + } + /// /// Removes cache for template /// diff --git a/src/Umbraco.Web/Cache/DistributedCacheExtensions.cs b/src/Umbraco.Web/Cache/DistributedCacheExtensions.cs index 85d46bb0a1..36eb98a123 100644 --- a/src/Umbraco.Web/Cache/DistributedCacheExtensions.cs +++ b/src/Umbraco.Web/Cache/DistributedCacheExtensions.cs @@ -45,6 +45,7 @@ namespace Umbraco.Web.Cache { dc.Remove(new Guid(DistributedCache.TemplateRefresherId), templateId); } + #endregion #region Page cache diff --git a/src/Umbraco.Web/Cache/TemplateCacheRefresher.cs b/src/Umbraco.Web/Cache/TemplateCacheRefresher.cs index 5b4c1c3110..0aec83104a 100644 --- a/src/Umbraco.Web/Cache/TemplateCacheRefresher.cs +++ b/src/Umbraco.Web/Cache/TemplateCacheRefresher.cs @@ -50,8 +50,11 @@ namespace Umbraco.Web.Cache private void RemoveFromCache(int id) { - ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch( - string.Format("{0}{1}", CacheKeys.TemplateCacheKey, id)); + ApplicationContext.Current.ApplicationCache.ClearCacheItem( + string.Format("{0}{1}", CacheKeys.TemplateFrontEndCacheKey, id)); + + ApplicationContext.Current.ApplicationCache.ClearCacheItem( + string.Format("{0}{1}", CacheKeys.TemplateBusinessLogicCacheKey, id)); } } diff --git a/src/Umbraco.Web/umbraco.presentation/template.cs b/src/Umbraco.Web/umbraco.presentation/template.cs index 6063bca584..6e596ab5cd 100644 --- a/src/Umbraco.Web/umbraco.presentation/template.cs +++ b/src/Umbraco.Web/umbraco.presentation/template.cs @@ -10,6 +10,7 @@ using System.Web.UI; using System.Collections; using System.Collections.Generic; using Umbraco.Core; +using Umbraco.Core.Cache; using Umbraco.Web; using Umbraco.Web.Cache; using umbraco.DataLayer; @@ -29,16 +30,11 @@ namespace umbraco readonly StringBuilder _templateOutput = new StringBuilder(); - // Cache - //static System.Web.Caching.Cache templateCache = System.Web.HttpRuntime.Cache; - private string _templateDesign = ""; int _masterTemplate = -1; private string _templateName = ""; private string _templateAlias = ""; - private const string CacheKey = "template"; - #endregion #region public properties @@ -498,7 +494,7 @@ namespace umbraco var tId = templateID; var t = ApplicationContext.Current.ApplicationCache.GetCacheItem( - string.Format("{0}{1}", CacheKey, tId), () => + string.Format("{0}{1}", CacheKeys.TemplateFrontEndCacheKey, tId), () => { using (var templateData = SqlHelper.ExecuteReader("select nodeId, alias, master, text, design from cmsTemplate inner join umbracoNode node on node.id = cmsTemplate.nodeId where nodeId = @templateID", SqlHelper.CreateParameter("@templateID", templateID))) { diff --git a/src/umbraco.cms/businesslogic/template/Template.cs b/src/umbraco.cms/businesslogic/template/Template.cs index da119d857b..26e4ffdddd 100644 --- a/src/umbraco.cms/businesslogic/template/Template.cs +++ b/src/umbraco.cms/businesslogic/template/Template.cs @@ -3,6 +3,8 @@ using System.Linq; using System.Collections; using System.Xml; using Umbraco.Core; +using Umbraco.Core.Cache; +using Umbraco.Core.IO; using Umbraco.Core.Logging; using umbraco.DataLayer; using System.Text.RegularExpressions; @@ -10,7 +12,6 @@ using System.IO; using System.Collections.Generic; using umbraco.cms.businesslogic.cache; using umbraco.BusinessLogic; -using umbraco.IO; using umbraco.cms.businesslogic.web; namespace umbraco.cms.businesslogic.template @@ -39,10 +40,8 @@ namespace umbraco.cms.businesslogic.template public static readonly string UmbracoMasterTemplate = SystemDirectories.Umbraco + "/masterpages/default.master"; private static Hashtable _templateAliases = new Hashtable(); private static volatile bool _templateAliasesInitialized = false; - private static object templateCacheSyncLock = new object(); - private static readonly string UmbracoTemplateCacheKey = "UmbracoTemplateCache"; - private static object _templateLoaderLocker = new object(); - private static Guid _objectType = new Guid(Constants.ObjectTypes.Template); + private static readonly object TemplateLoaderLocker = new object(); + private static readonly Guid ObjectType = new Guid(Constants.ObjectTypes.Template); private static readonly char[] NewLineChars = Environment.NewLine.ToCharArray(); #endregion @@ -94,7 +93,6 @@ namespace umbraco.cms.businesslogic.template if (!e.Cancel) { - FlushCache(); base.Save(); FireAfterSave(e); } @@ -211,7 +209,7 @@ namespace umbraco.cms.businesslogic.template SqlHelper.ExecuteNonQuery("Update cmsTemplate set alias = @alias where NodeId = " + this.Id, SqlHelper.CreateParameter("@alias", _alias)); _templateAliasesInitialized = false; - initTemplateAliases(); + InitTemplateAliases(); } } @@ -402,7 +400,7 @@ namespace umbraco.cms.businesslogic.template { // CMSNode MakeNew(int parentId, Guid objectType, int userId, int level, string text, Guid uniqueID) - CMSNode n = CMSNode.MakeNew(-1, _objectType, u.Id, 1, name, Guid.NewGuid()); + CMSNode n = CMSNode.MakeNew(-1, ObjectType, u.Id, 1, name, Guid.NewGuid()); //ensure unique alias name = helpers.Casing.SafeAlias(name); @@ -489,7 +487,7 @@ namespace umbraco.cms.businesslogic.template public static List