using System; using System.Collections.Generic; using System.Linq; using Umbraco.Core; using Umbraco.Core.Models; using umbraco; namespace Umbraco.Web.Cache { /// /// Extension methods for DistrubutedCache /// public static class DistributedCacheExtensions { public static void RemoveUserCache(this DistributedCache dc, int userId) { dc.Remove(new Guid(DistributedCache.UserCacheRefresherId), userId); } public static void RefreshUserCache(this DistributedCache dc, int userId) { dc.Refresh(new Guid(DistributedCache.UserCacheRefresherId), userId); } /// /// Refreshes the cache amongst servers for a template /// /// /// public static void RefreshTemplateCache(this DistributedCache dc, int templateId) { dc.Refresh(new Guid(DistributedCache.TemplateRefresherId), templateId); } /// /// Removes the cache amongst servers for a template /// /// /// public static void RemoveTemplateCache(this DistributedCache dc, int templateId) { dc.Remove(new Guid(DistributedCache.TemplateRefresherId), templateId); } /// /// Refreshes the cache amongst servers for all pages /// /// public static void RefreshAllPageCache(this DistributedCache dc) { dc.RefreshAll(new Guid(DistributedCache.PageCacheRefresherId)); } /// /// Refreshes the cache amongst servers for a page /// /// /// public static void RefreshPageCache(this DistributedCache dc, int documentId) { dc.Refresh(new Guid(DistributedCache.PageCacheRefresherId), documentId); } /// /// Refreshes page cache for all instances passed in /// /// /// public static void RefreshPageCache(this DistributedCache dc, params IContent[] content) { dc.Refresh(new Guid(DistributedCache.PageCacheRefresherId), x => x.Id, content); } /// /// Removes the cache amongst servers for a page /// /// /// public static void RemovePageCache(this DistributedCache dc, params IContent[] content) { dc.Remove(new Guid(DistributedCache.PageCacheRefresherId), x => x.Id, content); } /// /// Removes the cache amongst servers for a page /// /// /// public static void RemovePageCache(this DistributedCache dc, int documentId) { dc.Remove(new Guid(DistributedCache.PageCacheRefresherId), documentId); } /// /// Refreshes the cache amongst servers for a member /// /// /// public static void RefreshMemberCache(this DistributedCache dc, int memberId) { dc.Refresh(new Guid(DistributedCache.MemberCacheRefresherId), memberId); } /// /// Removes the cache amongst servers for a member /// /// /// public static void RemoveMemberCache(this DistributedCache dc, int memberId) { dc.Remove(new Guid(DistributedCache.MemberCacheRefresherId), memberId); } /// /// Refreshes the cache amongst servers for a media item /// /// /// public static void RefreshMediaCache(this DistributedCache dc, int mediaId) { dc.Refresh(new Guid(DistributedCache.MediaCacheRefresherId), mediaId); } /// /// Refreshes the cache amongst servers for a media item /// /// /// public static void RefreshMediaCache(this DistributedCache dc, params IMedia[] media) { dc.Refresh(new Guid(DistributedCache.MediaCacheRefresherId), x => x.Id, media); } /// /// Removes the cache amongst servers for a media item /// /// /// public static void RemoveMediaCache(this DistributedCache dc, int mediaId) { dc.Remove(new Guid(DistributedCache.MediaCacheRefresherId), mediaId); } /// /// Removes the cache amongst servers for media items /// /// /// public static void RemoveMediaCache(this DistributedCache dc, params IMedia[] media) { dc.Remove(new Guid(DistributedCache.MediaCacheRefresherId), x => x.Id, media); } /// /// Refreshes the cache amongst servers for a macro item /// /// /// public static void RefreshMacroCache(this DistributedCache dc, int macroId) { dc.Refresh(new Guid(DistributedCache.MacroCacheRefresherId), macroId); } /// /// Refreshes the cache amongst servers for a macro item /// /// /// public static void RefreshMacroCache(this DistributedCache dc, global::umbraco.cms.businesslogic.macro.Macro macro) { if (macro != null) { dc.Refresh(new Guid(DistributedCache.MacroCacheRefresherId), macro1 => macro1.Id, macro); } } /// /// Removes the cache amongst servers for a macro item /// /// /// public static void RemoveMacroCache(this DistributedCache dc, int macroId) { dc.Remove(new Guid(DistributedCache.MacroCacheRefresherId), macroId); } /// /// Removes the cache amongst servers for a macro item /// /// /// public static void RemoveMacroCache(this DistributedCache dc, global::umbraco.cms.businesslogic.macro.Macro macro) { if (macro != null) { dc.Remove(new Guid(DistributedCache.MacroCacheRefresherId), macro1 => macro1.Id, macro); } } /// /// Removes the cache amongst servers for a macro item /// /// /// public static void RemoveMacroCache(this DistributedCache dc, macro macro) { if (macro != null && macro.Model != null) { dc.Remove(new Guid(DistributedCache.MacroCacheRefresherId), macro1 => macro1.Model.Id, macro); } } /// /// Clears the cache for all macros on the current server /// /// public static void ClearAllMacroCacheOnCurrentServer(this DistributedCache dc) { //NOTE: The 'false' ensure that it will only refresh on the current server, not post to all servers dc.RefreshAll(new Guid(DistributedCache.MacroCacheRefresherId), false); } public static void ClearXsltCacheOnCurrentServer(this DistributedCache dc) { if (UmbracoSettings.UmbracoLibraryCacheDuration > 0) { ApplicationContext.Current.ApplicationCache.ClearCacheObjectTypes("MS.Internal.Xml.XPath.XPathSelectionIterator"); } } } }