diff --git a/src/Umbraco.Core/Cache/CacheKeys.cs b/src/Umbraco.Core/Cache/CacheKeys.cs index 16acefd60a..f0b84ef138 100644 --- a/src/Umbraco.Core/Cache/CacheKeys.cs +++ b/src/Umbraco.Core/Cache/CacheKeys.cs @@ -30,5 +30,8 @@ namespace Umbraco.Core.Cache public const string DomainCacheKey = "UmbracoDomainList"; + public const string StylesheetCacheKey = "UmbracoStylesheet"; + public const string StylesheetPropertyCacheKey = "UmbracoStylesheetProperty"; + } } \ No newline at end of file diff --git a/src/Umbraco.Web/Cache/CacheRefresherEventHandler.cs b/src/Umbraco.Web/Cache/CacheRefresherEventHandler.cs index 9822a8768c..f5fb90eafc 100644 --- a/src/Umbraco.Web/Cache/CacheRefresherEventHandler.cs +++ b/src/Umbraco.Web/Cache/CacheRefresherEventHandler.cs @@ -21,15 +21,16 @@ namespace Umbraco.Web.Cache public class CacheRefresherEventHandler : ApplicationEventHandler { protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) - { - //Bind to content events - currently used for: - // - macro clearing - // - clearing the xslt cache (MS.Internal.Xml.XPath.XPathSelectionIterator) - //NOTE: These are 'special' event handlers that will only clear cache for items on the current server - // that is because this event will fire based on a distributed cache call, meaning this event fires on - // all servers based on the distributed cache call for updating content. - content.AfterUpdateDocumentCache += ContentAfterUpdateDocumentCache; - content.AfterClearDocumentCache += ContentAfterClearDocumentCache; + { + //Bind to stylesheet events + //NOTE: we need to bind to legacy and new API events currently: http://issues.umbraco.org/issue/U4-1979 + + global::umbraco.cms.businesslogic.web.StylesheetProperty.AfterSave += StylesheetPropertyAfterSave; + global::umbraco.cms.businesslogic.web.StylesheetProperty.AfterDelete += StylesheetPropertyAfterDelete; + global::umbraco.cms.businesslogic.web.StyleSheet.AfterDelete += StyleSheetAfterDelete; + global::umbraco.cms.businesslogic.web.StyleSheet.AfterSave += StyleSheetAfterSave; + FileService.SavedStylesheet += FileServiceSavedStylesheet; + FileService.DeletedStylesheet += FileServiceDeletedStylesheet; //Bind to domain events @@ -84,6 +85,39 @@ namespace Umbraco.Web.Cache MediaService.Trashing += MediaServiceTrashing; } + #region Stylesheet and stylesheet property event handlers + static void StylesheetPropertyAfterSave(global::umbraco.cms.businesslogic.web.StylesheetProperty sender, SaveEventArgs e) + { + DistributedCache.Instance.RefreshStylesheetPropertyCache(sender); + } + + static void StylesheetPropertyAfterDelete(global::umbraco.cms.businesslogic.web.StylesheetProperty sender, DeleteEventArgs e) + { + DistributedCache.Instance.RemoveStylesheetPropertyCache(sender); + } + + static void FileServiceDeletedStylesheet(IFileService sender, Core.Events.DeleteEventArgs e) + { + e.DeletedEntities.ForEach(DistributedCache.Instance.RemoveStylesheetCache); + } + + static void FileServiceSavedStylesheet(IFileService sender, Core.Events.SaveEventArgs e) + { + e.SavedEntities.ForEach(DistributedCache.Instance.RefreshStylesheetCache); + } + + static void StyleSheetAfterSave(StyleSheet sender, SaveEventArgs e) + { + DistributedCache.Instance.RefreshStylesheetCache(sender); + } + + static void StyleSheetAfterDelete(StyleSheet sender, DeleteEventArgs e) + { + DistributedCache.Instance.RemoveStylesheetCache(sender); + } + #endregion + + #region Domain event handlers static void DomainNew(Domain sender, NewEventArgs e) { DistributedCache.Instance.RefreshDomainCache(sender); @@ -97,8 +131,10 @@ namespace Umbraco.Web.Cache static void DomainAfterSave(Domain sender, SaveEventArgs e) { DistributedCache.Instance.RefreshDomainCache(sender); - } + } + #endregion + #region Language event handlers /// /// Fires when a langauge is deleted /// @@ -147,8 +183,10 @@ namespace Umbraco.Web.Cache static void LanguageAfterDelete(global::umbraco.cms.businesslogic.language.Language sender, DeleteEventArgs e) { DistributedCache.Instance.RemoveLanguageCache(sender); - } + } + #endregion + #region Content/media Type event handlers /// /// Fires when a media type is deleted /// @@ -185,32 +223,12 @@ namespace Umbraco.Web.Cache /// /// static void ContentTypeServiceSavedContentType(IContentTypeService sender, Core.Events.SaveEventArgs e) - { + { e.SavedEntities.ForEach(contentType => DistributedCache.Instance.RefreshContentTypeCache(contentType)); - } - - /// - /// Fires after the document cache has been cleared for a particular document - /// - /// - /// - static void ContentAfterClearDocumentCache(global::umbraco.cms.businesslogic.web.Document sender, DocumentCacheEventArgs e) - { - DistributedCache.Instance.ClearAllMacroCacheOnCurrentServer(); - DistributedCache.Instance.ClearXsltCacheOnCurrentServer(); - } - - /// - /// Fires after the document cache has been updated for a particular document - /// - /// - /// - static void ContentAfterUpdateDocumentCache(global::umbraco.cms.businesslogic.web.Document sender, DocumentCacheEventArgs e) - { - DistributedCache.Instance.ClearAllMacroCacheOnCurrentServer(); - DistributedCache.Instance.ClearXsltCacheOnCurrentServer(); - } - + } + #endregion + + #region User event handlers static void UserDeleting(User sender, System.EventArgs e) { DistributedCache.Instance.RemoveUserCache(sender.Id); @@ -219,8 +237,10 @@ namespace Umbraco.Web.Cache static void UserSaving(User sender, System.EventArgs e) { DistributedCache.Instance.RefreshUserCache(sender.Id); - } + } + #endregion + #region Template event handlers /// /// Removes cache for template /// @@ -239,8 +259,10 @@ namespace Umbraco.Web.Cache static void TemplateAfterSave(Template sender, SaveEventArgs e) { DistributedCache.Instance.RefreshTemplateCache(sender.Id); - } + } + #endregion + #region Macro event handlers /// /// Flush macro from cache /// @@ -257,13 +279,15 @@ namespace Umbraco.Web.Cache /// /// static void MacroAfterSave(Macro sender, SaveEventArgs e) - { + { DistributedCache.Instance.RefreshMacroCache(sender); - } + } + #endregion + #region Media event handlers static void MediaServiceTrashing(IMediaService sender, Core.Events.MoveEventArgs e) { - DistributedCache.Instance.RemoveMediaCache(e.Entity); + DistributedCache.Instance.RemoveMediaCache(e.Entity); } static void MediaServiceMoving(IMediaService sender, Core.Events.MoveEventArgs e) @@ -279,16 +303,19 @@ namespace Umbraco.Web.Cache static void MediaServiceSaved(IMediaService sender, Core.Events.SaveEventArgs e) { DistributedCache.Instance.RefreshMediaCache(e.SavedEntities.ToArray()); - } + } + #endregion + #region Member event handlers static void MemberBeforeDelete(Member sender, DeleteEventArgs e) { - DistributedCache.Instance.RemoveMemberCache(sender.Id); + DistributedCache.Instance.RemoveMemberCache(sender.Id); } static void MemberAfterSave(Member sender, SaveEventArgs e) { DistributedCache.Instance.RefreshMemberCache(sender.Id); - } + } + #endregion } } \ No newline at end of file diff --git a/src/Umbraco.Web/Cache/DistributedCache.cs b/src/Umbraco.Web/Cache/DistributedCache.cs index d81ce0b939..00bc7c3969 100644 --- a/src/Umbraco.Web/Cache/DistributedCache.cs +++ b/src/Umbraco.Web/Cache/DistributedCache.cs @@ -44,6 +44,8 @@ namespace Umbraco.Web.Cache public const string ContentTypeCacheRefresherId = "6902E22C-9C10-483C-91F3-66B7CAE9E2F5"; public const string LanguageCacheRefresherId = "3E0F95D8-0BE5-44B8-8394-2B8750B62654"; public const string DomainCacheRefresherId = "11290A79-4B57-4C99-AD72-7748A3CF38AF"; + public const string StylesheetCacheRefresherId = "E0633648-0DEB-44AE-9A48-75C3A55CB670"; + public const string StylesheetPropertyCacheRefresherId = "2BC7A3A4-6EB1-4FBC-BAA3-C9E7B6D36D38"; #endregion diff --git a/src/Umbraco.Web/Cache/DistributedCacheExtensions.cs b/src/Umbraco.Web/Cache/DistributedCacheExtensions.cs index 8be2f88a69..85d46bb0a1 100644 --- a/src/Umbraco.Web/Cache/DistributedCacheExtensions.cs +++ b/src/Umbraco.Web/Cache/DistributedCacheExtensions.cs @@ -279,6 +279,58 @@ namespace Umbraco.Web.Cache } #endregion + #region Stylesheet Cache + + public static void RefreshStylesheetPropertyCache(this DistributedCache dc, global::umbraco.cms.businesslogic.web.StylesheetProperty styleSheetProperty) + { + if (styleSheetProperty != null) + { + dc.Refresh(new Guid(DistributedCache.StylesheetPropertyCacheRefresherId), styleSheetProperty.Id); + } + } + + public static void RemoveStylesheetPropertyCache(this DistributedCache dc, global::umbraco.cms.businesslogic.web.StylesheetProperty styleSheetProperty) + { + if (styleSheetProperty != null) + { + dc.Remove(new Guid(DistributedCache.StylesheetPropertyCacheRefresherId), styleSheetProperty.Id); + } + } + + public static void RefreshStylesheetCache(this DistributedCache dc, StyleSheet styleSheet) + { + if (styleSheet != null) + { + dc.Refresh(new Guid(DistributedCache.StylesheetCacheRefresherId), styleSheet.Id); + } + } + + public static void RemoveStylesheetCache(this DistributedCache dc, StyleSheet styleSheet) + { + if (styleSheet != null) + { + dc.Remove(new Guid(DistributedCache.StylesheetCacheRefresherId), styleSheet.Id); + } + } + + public static void RefreshStylesheetCache(this DistributedCache dc, Umbraco.Core.Models.Stylesheet styleSheet) + { + if (styleSheet != null) + { + dc.Refresh(new Guid(DistributedCache.StylesheetCacheRefresherId), styleSheet.Id); + } + } + + public static void RemoveStylesheetCache(this DistributedCache dc, Umbraco.Core.Models.Stylesheet styleSheet) + { + if (styleSheet != null) + { + dc.Remove(new Guid(DistributedCache.StylesheetCacheRefresherId), styleSheet.Id); + } + } + + #endregion + #region Domain Cache public static void RefreshDomainCache(this DistributedCache dc, Domain domain) diff --git a/src/Umbraco.Web/Cache/PageCacheRefresher.cs b/src/Umbraco.Web/Cache/PageCacheRefresher.cs index 0d1530b8ce..366607afd2 100644 --- a/src/Umbraco.Web/Cache/PageCacheRefresher.cs +++ b/src/Umbraco.Web/Cache/PageCacheRefresher.cs @@ -61,6 +61,8 @@ namespace Umbraco.Web.Cache public override void Refresh(int id) { content.Instance.UpdateDocumentCache(id); + DistributedCache.Instance.ClearAllMacroCacheOnCurrentServer(); + DistributedCache.Instance.ClearXsltCacheOnCurrentServer(); base.Refresh(id); } @@ -71,18 +73,24 @@ namespace Umbraco.Web.Cache public override void Remove(int id) { content.Instance.ClearDocumentCache(id); + DistributedCache.Instance.ClearAllMacroCacheOnCurrentServer(); + DistributedCache.Instance.ClearXsltCacheOnCurrentServer(); base.Remove(id); } public override void Refresh(IContent instance) { content.Instance.UpdateDocumentCache(new Document(instance)); + DistributedCache.Instance.ClearAllMacroCacheOnCurrentServer(); + DistributedCache.Instance.ClearXsltCacheOnCurrentServer(); base.Refresh(instance); } public override void Remove(IContent instance) { content.Instance.ClearDocumentCache(new Document(instance)); + DistributedCache.Instance.ClearAllMacroCacheOnCurrentServer(); + DistributedCache.Instance.ClearXsltCacheOnCurrentServer(); base.Remove(instance); } } diff --git a/src/Umbraco.Web/Cache/StylesheetCacheRefresher.cs b/src/Umbraco.Web/Cache/StylesheetCacheRefresher.cs new file mode 100644 index 0000000000..5d98671a76 --- /dev/null +++ b/src/Umbraco.Web/Cache/StylesheetCacheRefresher.cs @@ -0,0 +1,50 @@ +using System; +using Umbraco.Core; +using Umbraco.Core.Cache; + +namespace Umbraco.Web.Cache +{ + /// + /// A cache refresher to ensure stylesheet cache is refreshed when stylesheets change + /// + public sealed class StylesheetCacheRefresher : CacheRefresherBase + { + protected override StylesheetCacheRefresher Instance + { + get { return this; } + } + + public override Guid UniqueIdentifier + { + get { return new Guid(DistributedCache.StylesheetCacheRefresherId); } + } + + public override string Name + { + get { return "Stylesheet cache refresher"; } + } + + public override void RefreshAll() + { + ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch(CacheKeys.StylesheetCacheKey); + base.RefreshAll(); + } + + public override void Refresh(int id) + { + ApplicationContext.Current.ApplicationCache.ClearCacheItem(GetStylesheetCacheKey(id)); + base.Refresh(id); + } + + public override void Remove(int id) + { + ApplicationContext.Current.ApplicationCache.ClearCacheItem(GetStylesheetCacheKey(id)); + base.Remove(id); + } + + private static string GetStylesheetCacheKey(int id) + { + return CacheKeys.StylesheetCacheKey + id; + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Web/Cache/StylesheetPropertyCacheRefresher.cs b/src/Umbraco.Web/Cache/StylesheetPropertyCacheRefresher.cs new file mode 100644 index 0000000000..4f301a56e8 --- /dev/null +++ b/src/Umbraco.Web/Cache/StylesheetPropertyCacheRefresher.cs @@ -0,0 +1,50 @@ +using System; +using Umbraco.Core; +using Umbraco.Core.Cache; + +namespace Umbraco.Web.Cache +{ + /// + /// A cache refresher to ensure stylesheet property cache is refreshed when stylesheet properties change + /// + public sealed class StylesheetPropertyCacheRefresher : CacheRefresherBase + { + protected override StylesheetPropertyCacheRefresher Instance + { + get { return this; } + } + + public override Guid UniqueIdentifier + { + get { return new Guid(DistributedCache.StylesheetCacheRefresherId); } + } + + public override string Name + { + get { return "Stylesheet property cache refresher"; } + } + + public override void RefreshAll() + { + ApplicationContext.Current.ApplicationCache.ClearCacheByKeySearch(CacheKeys.StylesheetPropertyCacheKey); + base.RefreshAll(); + } + + public override void Refresh(int id) + { + ApplicationContext.Current.ApplicationCache.ClearCacheItem(GetStylesheetPropertyCacheKey(id)); + base.Refresh(id); + } + + public override void Remove(int id) + { + ApplicationContext.Current.ApplicationCache.ClearCacheItem(GetStylesheetPropertyCacheKey(id)); + base.Remove(id); + } + + private static string GetStylesheetPropertyCacheKey(int id) + { + return CacheKeys.StylesheetPropertyCacheKey + id; + } + } +} \ No newline at end of file diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index 71f078f6c3..e16c39efe7 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -273,6 +273,8 @@ + + diff --git a/src/umbraco.cms/businesslogic/cache/Cache.cs b/src/umbraco.cms/businesslogic/cache/Cache.cs index 926a3d8b07..dd424bad88 100644 --- a/src/umbraco.cms/businesslogic/cache/Cache.cs +++ b/src/umbraco.cms/businesslogic/cache/Cache.cs @@ -13,10 +13,9 @@ namespace umbraco.cms.businesslogic.cache /// class so that is why the class declaration is not marked obsolete. /// We haven't migrated it because I don't know why it is needed. /// + [Obsolete("Use the ApplicationContext.Current.ApplicationCache instead")] public class Cache { - private static readonly object m_Locker = new object(); - /// /// Clears everything in umbraco's runtime cache, which means that not only /// umbraco content is removed, but also other cache items from pages running in diff --git a/src/umbraco.cms/businesslogic/web/StyleSheet.cs b/src/umbraco.cms/businesslogic/web/StyleSheet.cs index 49c593546e..c73b3a337f 100644 --- a/src/umbraco.cms/businesslogic/web/StyleSheet.cs +++ b/src/umbraco.cms/businesslogic/web/StyleSheet.cs @@ -4,9 +4,10 @@ using System.IO; using System.Linq; using System.Text.RegularExpressions; using System.Xml; +using Umbraco.Core.Cache; +using Umbraco.Core.IO; using umbraco.cms.businesslogic.cache; using umbraco.DataLayer; -using umbraco.IO; using Umbraco.Core; namespace umbraco.cms.businesslogic.web @@ -19,12 +20,9 @@ namespace umbraco.cms.businesslogic.web private string _filename = ""; private string _content = ""; - private StylesheetProperty[] m_properties; + private StylesheetProperty[] _properties; public static Guid ModuleObjectType = new Guid(Constants.ObjectTypes.Stylesheet); - private static object stylesheetCacheSyncLock = new object(); - private static readonly string UmbracoStylesheetCacheKey = "UmbracoStylesheet"; - public string Filename { get { return _filename; } @@ -53,18 +51,18 @@ namespace umbraco.cms.businesslogic.web { get { - if (m_properties == null) + if (_properties == null) { - BusinessLogic.console.IconI[] tmp = this.ChildrenOfAllObjectTypes; - - StylesheetProperty[] retVal = new StylesheetProperty[tmp.Length]; - for (int i = 0; i < tmp.Length; i++) + var tmp = this.ChildrenOfAllObjectTypes; + var retVal = new StylesheetProperty[tmp.Length]; + for (var i = 0; i < tmp.Length; i++) { + //So this will go get cached properties but yet the above call to ChildrenOfAllObjectTypes is not cached :/ retVal[i] = StylesheetProperty.GetStyleSheetProperty(tmp[i].Id); } - m_properties = retVal; + _properties = retVal; } - return m_properties; + return _properties; } } @@ -73,19 +71,19 @@ namespace umbraco.cms.businesslogic.web public StyleSheet(Guid id) : base(id) { - setupStyleSheet(true, true); + SetupStyleSheet(true, true); } public StyleSheet(int id) : base(id) { - setupStyleSheet(true, true); + SetupStyleSheet(true, true); } public StyleSheet(int id, bool setupStyleProperties, bool loadContentFromFile) : base(id) { - setupStyleSheet(loadContentFromFile, setupStyleProperties); + SetupStyleSheet(loadContentFromFile, setupStyleProperties); } /// @@ -93,7 +91,7 @@ namespace umbraco.cms.businesslogic.web /// public override void Save() { - SaveEventArgs e = new SaveEventArgs(); + var e = new SaveEventArgs(); FireBeforeSave(e); if (!e.Cancel) { @@ -102,10 +100,10 @@ namespace umbraco.cms.businesslogic.web } } - private void setupStyleSheet(bool loadFileData, bool updateStyleProperties) + private void SetupStyleSheet(bool loadFileData, bool updateStyleProperties) { // Get stylesheet data - using (IRecordsReader dr = SqlHelper.ExecuteReader("select filename, content from cmsStylesheet where nodeid = " + Id)) + using (var dr = SqlHelper.ExecuteReader("select filename, content from cmsStylesheet where nodeid = " + Id)) { if (dr.Read()) { @@ -119,14 +117,14 @@ namespace umbraco.cms.businesslogic.web } else if (File.Exists(IOHelper.MapPath(String.Format("{0}/{1}.css", SystemDirectories.Css, this.Text)))) { - string propertiesContent = String.Empty; + var propertiesContent = String.Empty; - using (StreamReader re = File.OpenText(IOHelper.MapPath(String.Format("{0}/{1}.css", SystemDirectories.Css, this.Text)))) + using (var re = File.OpenText(IOHelper.MapPath(String.Format("{0}/{1}.css", SystemDirectories.Css, this.Text)))) { string input = null; _content = string.Empty; // NH: Updates the reader to support properties - bool readingProperties = false; + var readingProperties = false; while ((input = re.ReadLine()) != null && true) { @@ -151,7 +149,7 @@ namespace umbraco.cms.businesslogic.web { if (propertiesContent != String.Empty) { - parseProperties(propertiesContent); + ParseProperties(propertiesContent); } } } @@ -159,15 +157,15 @@ namespace umbraco.cms.businesslogic.web } } - private void parseProperties(string propertiesContent) + private void ParseProperties(string propertiesContent) { - MatchCollection m = Regex.Matches(propertiesContent, "([^{]*){([^}]*)}", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); + var m = Regex.Matches(propertiesContent, "([^{]*){([^}]*)}", RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace); foreach (Match match in m) { - GroupCollection groups = match.Groups; - string cssClass = groups[1].Value.Replace("\n", "").Replace("\r", "").Trim().Trim(Environment.NewLine.ToCharArray()); - string cssCode = groups[2].Value.Trim(Environment.NewLine.ToCharArray()); + var groups = match.Groups; + var cssClass = groups[1].Value.Replace("\n", "").Replace("\r", "").Trim().Trim(Environment.NewLine.ToCharArray()); + var cssCode = groups[2].Value.Trim(Environment.NewLine.ToCharArray()); foreach (StylesheetProperty sp in this.Properties) if (sp.Alias == cssClass && sp.value != cssCode) // check before setting to avoid invalidating cache unecessarily sp.value = cssCode; @@ -178,14 +176,14 @@ namespace umbraco.cms.businesslogic.web { // Create the umbraco node - CMSNode newNode = CMSNode.MakeNew(-1, ModuleObjectType, user.Id, 1, Text, Guid.NewGuid()); + var newNode = CMSNode.MakeNew(-1, ModuleObjectType, user.Id, 1, Text, Guid.NewGuid()); // Create the stylesheet data SqlHelper.ExecuteNonQuery(string.Format("insert into cmsStylesheet (nodeId, filename, content) values ('{0}','{1}',@content)", newNode.Id, FileName), SqlHelper.CreateParameter("@content", Content)); // save to file to avoid file coherency issues - StyleSheet newCss = new StyleSheet(newNode.Id, false, false); - NewEventArgs e = new NewEventArgs(); + var newCss = new StyleSheet(newNode.Id, false, false); + var e = new NewEventArgs(); newCss.OnNew(e); return newCss; @@ -194,9 +192,9 @@ namespace umbraco.cms.businesslogic.web public static StyleSheet[] GetAll() { - ArrayList dbStylesheets = new ArrayList(); + var dbStylesheets = new ArrayList(); - Guid[] topNodeIds = CMSNode.TopMostNodeIds(ModuleObjectType); + var topNodeIds = CMSNode.TopMostNodeIds(ModuleObjectType); //StyleSheet[] retval = new StyleSheet[topNodeIds.Length]; for (int i = 0; i < topNodeIds.Length; i++) { @@ -204,10 +202,10 @@ namespace umbraco.cms.businesslogic.web dbStylesheets.Add(new StyleSheet(topNodeIds[i]).Text.ToLower()); } - ArrayList fileStylesheets = new ArrayList(); - DirectoryInfo fileListing = new DirectoryInfo(IOHelper.MapPath(SystemDirectories.Css + "/")); + var fileStylesheets = new ArrayList(); + var fileListing = new DirectoryInfo(IOHelper.MapPath(SystemDirectories.Css + "/")); - foreach (FileInfo file in fileListing.GetFiles("*.css")) + foreach (var file in fileListing.GetFiles("*.css")) { if (!dbStylesheets.Contains(file.Name.Replace(file.Extension, "").ToLower())) { @@ -215,7 +213,7 @@ namespace umbraco.cms.businesslogic.web } } - StyleSheet[] retval = new StyleSheet[dbStylesheets.Count + fileStylesheets.Count]; + var retval = new StyleSheet[dbStylesheets.Count + fileStylesheets.Count]; for (int i = 0; i < topNodeIds.Length; i++) { retval[i] = new StyleSheet(topNodeIds[i]); @@ -250,7 +248,7 @@ namespace umbraco.cms.businesslogic.web public override void delete() { - DeleteEventArgs e = new DeleteEventArgs(); + var e = new DeleteEventArgs(); FireBeforeDelete(e); if (!e.Cancel) @@ -306,27 +304,30 @@ namespace umbraco.cms.businesslogic.web public static StyleSheet GetStyleSheet(int id, bool setupStyleProperties, bool loadContentFromFile) { - return Cache.GetCacheItem(GetCacheKey(id), stylesheetCacheSyncLock, TimeSpan.FromMinutes(30), () => - { - try - { - return new StyleSheet(id, setupStyleProperties, loadContentFromFile); - } - catch - { - return null; - } - }); + return ApplicationContext.Current.ApplicationCache.GetCacheItem( + GetCacheKey(id), + TimeSpan.FromMinutes(30), () => + { + try + { + return new StyleSheet(id, setupStyleProperties, loadContentFromFile); + } + catch + { + return null; + } + }); } + [Obsolete("Stylesheet cache is automatically invalidated by Umbraco when a stylesheet is saved or deleted")] public void InvalidateCache() { - Cache.ClearCacheItem(GetCacheKey(this.Id)); + ApplicationContext.Current.ApplicationCache.ClearCacheItem(GetCacheKey(Id)); } private static string GetCacheKey(int id) { - return UmbracoStylesheetCacheKey + id; + return CacheKeys.StylesheetCacheKey + id; } //EVENTS diff --git a/src/umbraco.cms/businesslogic/web/StylesheetProperty.cs b/src/umbraco.cms/businesslogic/web/StylesheetProperty.cs index 347cab0304..230b03fa1d 100644 --- a/src/umbraco.cms/businesslogic/web/StylesheetProperty.cs +++ b/src/umbraco.cms/businesslogic/web/StylesheetProperty.cs @@ -1,4 +1,6 @@ using System; +using Umbraco.Core; +using Umbraco.Core.Cache; using umbraco.cms.businesslogic.cache; using umbraco.DataLayer; @@ -9,28 +11,21 @@ namespace umbraco.cms.businesslogic.web private string _alias; private string _value; - private static object stylesheetPropertyCacheSyncLock = new object(); - private static readonly string UmbracoStylesheetPropertyCacheKey = "UmbracoStylesheetProperty"; - - private static Guid moduleObjectType = new Guid("5555da4f-a123-42b2-4488-dcdfb25e4111"); - // internal static moduleId = - - // Used to check whether the parent stylesheet cache has already been cleared - private bool isParentStyleSheetCacheCleared; - + private static readonly Guid ModuleObjectType = new Guid("5555da4f-a123-42b2-4488-dcdfb25e4111"); + public StylesheetProperty(int id) : base(id) { - initProperty(); + InitProperty(); } public StylesheetProperty(Guid id) : base(id) { - initProperty(); + InitProperty(); } - private void initProperty() { - - IRecordsReader dr = SqlHelper.ExecuteReader("Select stylesheetPropertyAlias,stylesheetPropertyValue from cmsStylesheetProperty where nodeId = " + this.Id); + private void InitProperty() + { + var dr = SqlHelper.ExecuteReader("Select stylesheetPropertyAlias,stylesheetPropertyValue from cmsStylesheetProperty where nodeId = " + this.Id); if (dr.Read()) { _alias = dr.GetString("stylesheetPropertyAlias"); @@ -41,67 +36,72 @@ namespace umbraco.cms.businesslogic.web dr.Close(); } - public StyleSheet StyleSheet() { + public StyleSheet StyleSheet() + { return new StyleSheet(this.Parent.Id, true, false); } - public void RefreshFromFile() { + public void RefreshFromFile() + { // ping the stylesheet - web.StyleSheet ss = new StyleSheet(this.Parent.Id); - initProperty(); + var ss = new StyleSheet(this.Parent.Id); + InitProperty(); } - public string Alias { - get{return _alias;} - set { + public string Alias + { + get { return _alias; } + set + { SqlHelper.ExecuteNonQuery(String.Format("update cmsStylesheetProperty set stylesheetPropertyAlias = '{0}' where nodeId = {1}", value.Replace("'", "''"), this.Id)); _alias=value; InvalidateCache(); - InvalidateParentStyleSheetCache(); } } - public string value { - get {return _value;} - set { + public string value + { + get { return _value; } + set + { SqlHelper.ExecuteNonQuery(String.Format("update cmsStylesheetProperty set stylesheetPropertyValue = '{0}' where nodeId = {1}", value.Replace("'", "''"), this.Id)); - _value=value; + _value = value; InvalidateCache(); - InvalidateParentStyleSheetCache(); } } - public static StylesheetProperty MakeNew(string Text, StyleSheet sheet, BusinessLogic.User user) { - CMSNode newNode = CMSNode.MakeNew(sheet.Id, moduleObjectType, user.Id, 2, Text, Guid.NewGuid()); + public static StylesheetProperty MakeNew(string Text, StyleSheet sheet, BusinessLogic.User user) + { + var newNode = CMSNode.MakeNew(sheet.Id, ModuleObjectType, user.Id, 2, Text, Guid.NewGuid()); SqlHelper.ExecuteNonQuery(String.Format("Insert into cmsStylesheetProperty (nodeId,stylesheetPropertyAlias,stylesheetPropertyValue) values ('{0}','{1}','')", newNode.Id, Text)); - StylesheetProperty ssp = new StylesheetProperty(newNode.Id); - NewEventArgs e = new NewEventArgs(); + var ssp = new StylesheetProperty(newNode.Id); + var e = new NewEventArgs(); ssp.OnNew(e); return ssp; } public override void delete() { - DeleteEventArgs e = new DeleteEventArgs(); + var e = new DeleteEventArgs(); FireBeforeDelete(e); - if (!e.Cancel) { - InvalidateCache(); - InvalidateParentStyleSheetCache(); + if (!e.Cancel) + { SqlHelper.ExecuteNonQuery("delete from cmsStylesheetProperty where nodeId = @nodeId", SqlHelper.CreateParameter("@nodeId", this.Id)); base.delete(); - FireAfterDelete(e); } } - public override void Save() { - SaveEventArgs e = new SaveEventArgs(); + public override void Save() + { + var e = new SaveEventArgs(); FireBeforeSave(e); - if (!e.Cancel) { + if (!e.Cancel) + { base.Save(); FireAfterSave(e); @@ -116,40 +116,30 @@ namespace umbraco.cms.businesslogic.web public static StylesheetProperty GetStyleSheetProperty(int id) { - return Cache.GetCacheItem(GetCacheKey(id), stylesheetPropertyCacheSyncLock, TimeSpan.FromMinutes(30), () => - { - try - { - return new StylesheetProperty(id); - } - catch - { - return null; - } - }); + return ApplicationContext.Current.ApplicationCache.GetCacheItem( + GetCacheKey(id), + TimeSpan.FromMinutes(30), () => + { + try + { + return new StylesheetProperty(id); + } + catch + { + return null; + } + }); } + [Obsolete("Umbraco automatically refreshes the cache when stylesheets and stylesheet properties are saved or deleted")] private void InvalidateCache() { - Cache.ClearCacheItem(GetCacheKey(this.Id)); - } - - /// - /// Invalidates the parent style sheet cache. - /// - private void InvalidateParentStyleSheetCache() - { - // not sure whether should be doing this check, but if not then the stylesheet is invalidated, then reloaded from db in order to invalidate again. - if (!isParentStyleSheetCacheCleared) - { - umbraco.cms.businesslogic.web.StyleSheet.GetStyleSheet(this.Parent.Id, false, false).InvalidateCache(); - isParentStyleSheetCacheCleared = true; - } + ApplicationContext.Current.ApplicationCache.ClearCacheItem(GetCacheKey(Id)); } private static string GetCacheKey(int id) { - return UmbracoStylesheetPropertyCacheKey + id; + return CacheKeys.StylesheetPropertyCacheKey + id; } // EVENTS