using System; using Umbraco.Core.Events; using Umbraco.Core.Sync; using Umbraco.Core.Models.EntityBase; namespace Umbraco.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 : ICacheRefresher where TInstanceType : class, ICacheRefresher { /// /// Initializes a new instance of the . /// /// A cache helper. protected CacheRefresherBase(CacheHelper cacheHelper) { CacheHelper = cacheHelper; } /// /// Triggers when the cache is updated on the server. /// /// /// Triggers on each server configured for an Umbraco project whenever a cache refresher is updated. /// public static event TypedEventHandler CacheUpdated; #region Define /// /// Gets the typed 'this' for events. /// protected abstract TInstanceType This { get; } /// /// 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(This, new CacheRefresherEventArgs(null, MessageType.RefreshAll)); } /// /// Refreshes an entity. /// /// The entity's identifier. public virtual void Refresh(int id) { OnCacheUpdated(This, new CacheRefresherEventArgs(id, MessageType.RefreshById)); } /// /// Refreshes an entity. /// /// The entity's identifier. public virtual void Refresh(Guid id) { OnCacheUpdated(This, new CacheRefresherEventArgs(id, MessageType.RefreshById)); } /// /// Removes an entity. /// /// The entity's identifier. public virtual void Remove(int id) { OnCacheUpdated(This, new CacheRefresherEventArgs(id, MessageType.RemoveById)); } #endregion #region Protected /// /// Gets the cache helper. /// protected CacheHelper CacheHelper { get; } /// /// Clears the cache for all repository entities of a specified type. /// /// The type of the entities. protected void ClearAllIsolatedCacheByEntityType() where TEntity : class, IAggregateRoot { CacheHelper.IsolatedRuntimeCache.ClearCache(); } /// /// Raises the CacheUpdated event. /// /// The event sender. /// The event arguments. protected static void OnCacheUpdated(TInstanceType sender, CacheRefresherEventArgs args) { CacheUpdated?.Invoke(sender, args); } #endregion } }