using System;
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core;
using Umbraco.Core.Models;
using umbraco;
using umbraco.cms.businesslogic.web;
namespace Umbraco.Web.Cache
{
///
/// Extension methods for DistrubutedCache
///
public static class DistributedCacheExtensions
{
#region User cache
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);
}
#endregion
#region Template cache
///
/// 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);
}
#endregion
#region Page cache
///
/// 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);
}
#endregion
#region Member cache
///
/// 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);
}
#endregion
#region Media Cache
///
/// Refreshes the cache amongst servers for a media item
///
///
///
public static void RefreshMediaCache(this DistributedCache dc, params IMedia[] media)
{
dc.RefreshByJson(new Guid(DistributedCache.MediaCacheRefresherId),
MediaCacheRefresher.SerializeToJsonPayload(media));
}
///
/// Removes the cache amongst servers for a media item
///
///
///
///
/// Clearing by Id will never work for load balanced scenarios for media since we require a Path
/// to clear all of the cache but the media item will be removed before the other servers can
/// look it up. Only here for legacy purposes.
///
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.RemoveByJson(new Guid(DistributedCache.MediaCacheRefresherId),
MediaCacheRefresher.SerializeToJsonPayload(media));
}
#endregion
#region Macro Cache
///
/// 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);
}
///
/// 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.RefreshByJson(new Guid(DistributedCache.MacroCacheRefresherId),
MacroCacheRefresher.SerializeToJsonPayload(macro));
}
}
///
/// 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.RemoveByJson(new Guid(DistributedCache.MacroCacheRefresherId),
MacroCacheRefresher.SerializeToJsonPayload(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.RemoveByJson(new Guid(DistributedCache.MacroCacheRefresherId),
MacroCacheRefresher.SerializeToJsonPayload(macro));
}
}
#endregion
#region Content type cache
///
/// Remove all cache for a given content type
///
///
///
public static void RefreshContentTypeCache(this DistributedCache dc, IContentType contentType)
{
if (contentType != null)
{
//dc.Refresh(new Guid(DistributedCache.ContentTypeCacheRefresherId), x => x.Id, contentType);
dc.RefreshByJson(new Guid(DistributedCache.ContentTypeCacheRefresherId),
ContentTypeCacheRefresher.SerializeToJsonPayload(false, contentType));
}
}
///
/// Remove all cache for a given media type
///
///
///
public static void RefreshMediaTypeCache(this DistributedCache dc, IMediaType mediaType)
{
if (mediaType != null)
{
//dc.Refresh(new Guid(DistributedCache.ContentTypeCacheRefresherId), x => x.Id, mediaType);
dc.RefreshByJson(new Guid(DistributedCache.ContentTypeCacheRefresherId),
ContentTypeCacheRefresher.SerializeToJsonPayload(false, mediaType));
}
}
///
/// Remove all cache for a given content type
///
///
///
public static void RemoveContentTypeCache(this DistributedCache dc, IContentType contentType)
{
if (contentType != null)
{
//dc.Remove(new Guid(DistributedCache.ContentTypeCacheRefresherId), x => x.Id, contentType);
dc.RemoveByJson(new Guid(DistributedCache.ContentTypeCacheRefresherId),
ContentTypeCacheRefresher.SerializeToJsonPayload(true, contentType));
}
}
///
/// Remove all cache for a given media type
///
///
///
public static void RemoveMediaTypeCache(this DistributedCache dc, IMediaType mediaType)
{
if (mediaType != null)
{
//dc.Remove(new Guid(DistributedCache.ContentTypeCacheRefresherId), x => x.Id, mediaType);
dc.RemoveByJson(new Guid(DistributedCache.ContentTypeCacheRefresherId),
ContentTypeCacheRefresher.SerializeToJsonPayload(true, mediaType));
}
}
#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)
{
if (domain != null)
{
dc.Refresh(new Guid(DistributedCache.DomainCacheRefresherId), domain.Id);
}
}
public static void RemoveDomainCache(this DistributedCache dc, Domain domain)
{
if (domain != null)
{
dc.Remove(new Guid(DistributedCache.DomainCacheRefresherId), domain.Id);
}
}
#endregion
#region Language Cache
public static void RefreshLanguageCache(this DistributedCache dc, ILanguage language)
{
if (language != null)
{
dc.Refresh(new Guid(DistributedCache.LanguageCacheRefresherId), language.Id);
}
}
public static void RemoveLanguageCache(this DistributedCache dc, ILanguage language)
{
if (language != null)
{
dc.Remove(new Guid(DistributedCache.LanguageCacheRefresherId), language.Id);
}
}
public static void RefreshLanguageCache(this DistributedCache dc, global::umbraco.cms.businesslogic.language.Language language)
{
if (language != null)
{
dc.Refresh(new Guid(DistributedCache.LanguageCacheRefresherId), language.id);
}
}
public static void RemoveLanguageCache(this DistributedCache dc, global::umbraco.cms.businesslogic.language.Language language)
{
if (language != null)
{
dc.Remove(new Guid(DistributedCache.LanguageCacheRefresherId), language.id);
}
}
#endregion
public static void ClearXsltCacheOnCurrentServer(this DistributedCache dc)
{
if (UmbracoSettings.UmbracoLibraryCacheDuration > 0)
{
ApplicationContext.Current.ApplicationCache.ClearCacheObjectTypes("MS.Internal.Xml.XPath.XPathSelectionIterator");
}
}
}
}