using System; using Umbraco.Cms.Core.Events; using Umbraco.Cms.Core.Models.Entities; using Umbraco.Cms.Core.Sync; namespace Umbraco.Cms.Core.Cache { /// /// A base class for cache refreshers that handles events. /// /// The actual cache refresher type. /// The actual cache refresher type is used for strongly typed events. public abstract class CacheRefresherBase< TNotification> : ICacheRefresher where TNotification : CacheRefresherNotificationBase, new() { /// /// Initializes a new instance of the . /// /// A cache helper. protected CacheRefresherBase(AppCaches appCaches, IEventAggregator eventAggregator) { AppCaches = appCaches; EventAggregator = eventAggregator; } #region Define /// /// Gets the unique identifier of the refresher. /// public abstract Guid RefresherUniqueId { get; } /// /// Gets the name of the refresher. /// public abstract string Name { get; } #endregion #region Refresher /// /// Refreshes all entities. /// public virtual void RefreshAll() { OnCacheUpdated(new TNotification().Init(null, MessageType.RefreshAll)); } /// /// Refreshes an entity. /// /// The entity's identifier. public virtual void Refresh(int id) { OnCacheUpdated(new TNotification().Init(id, MessageType.RefreshById)); } /// /// Refreshes an entity. /// /// The entity's identifier. public virtual void Refresh(Guid id) { OnCacheUpdated(new TNotification().Init(id, MessageType.RefreshById)); } /// /// Removes an entity. /// /// The entity's identifier. public virtual void Remove(int id) { OnCacheUpdated(new TNotification().Init(id, MessageType.RemoveById)); } #endregion #region Protected /// /// Gets the cache helper. /// protected AppCaches AppCaches { get; } protected IEventAggregator EventAggregator { get; } /// /// Clears the cache for all repository entities of a specified type. /// /// The type of the entities. protected void ClearAllIsolatedCacheByEntityType() where TEntity : class, IEntity { AppCaches.IsolatedCaches.ClearCache(); } /// /// Raises the CacheUpdated event. /// /// The event sender. /// The event arguments. protected void OnCacheUpdated(CacheRefresherNotificationBase notification) { EventAggregator.Publish(notification); } #endregion } }