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 to inherit from that ensures the correct events are raised /// when cache refreshing occurs. /// /// The real cache refresher type, this is used for raising strongly typed events public abstract class CacheRefresherBase : ICacheRefresher where TInstanceType : ICacheRefresher { protected CacheRefresherBase(CacheHelper cacheHelper) { CacheHelper = cacheHelper; } /// /// An event that is raised when cache is updated on an individual server /// /// /// This event will fire on each server configured for an Umbraco project whenever a cache refresher /// is updated. /// public static event TypedEventHandler CacheUpdated; /// /// Raises the event /// /// /// protected static void OnCacheUpdated(TInstanceType sender, CacheRefresherEventArgs args) { if (CacheUpdated != null) { CacheUpdated(sender, args); } } /// /// Returns the real instance of the object ('this') for use in strongly typed events /// protected abstract TInstanceType Instance { get; } public abstract Guid UniqueIdentifier { get; } public abstract string Name { get; } protected CacheHelper CacheHelper { get; } public virtual void RefreshAll() { OnCacheUpdated(Instance, new CacheRefresherEventArgs(null, MessageType.RefreshAll)); } public virtual void Refresh(int id) { OnCacheUpdated(Instance, new CacheRefresherEventArgs(id, MessageType.RefreshById)); } public virtual void Remove(int id) { OnCacheUpdated(Instance, new CacheRefresherEventArgs(id, MessageType.RemoveById)); } public virtual void Refresh(Guid id) { OnCacheUpdated(Instance, new CacheRefresherEventArgs(id, MessageType.RefreshById)); } /// /// Clears the cache for all repository entities of this type /// /// internal void ClearAllIsolatedCacheByEntityType() where TEntity : class, IAggregateRoot { CacheHelper.IsolatedRuntimeCache.ClearCache(); } } }