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 : ICacheRefresher
where TNotification : CacheRefresherNotification
{
///
/// Initializes a new instance of the .
///
/// A cache helper.
protected CacheRefresherBase(AppCaches appCaches, IEventAggregator eventAggregator, ICacheRefresherNotificationFactory factory)
{
AppCaches = appCaches;
EventAggregator = eventAggregator;
NotificationFactory = factory;
}
#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; }
///
/// Gets the for
///
protected ICacheRefresherNotificationFactory NotificationFactory { get; }
#endregion
#region Refresher
///
/// Refreshes all entities.
///
public virtual void RefreshAll()
{
// NOTE: We pass in string.Empty here because if we pass in NULL this causes problems with
// the underlying ActivatorUtilities.CreateInstance which doesn't seem to support passing in
// null to an 'object' parameter and we end up with "A suitable constructor for type 'ZYZ' could not be located."
// In this case, all cache refreshers should be checking for the type first before checking for a msg value
// so this shouldn't cause any issues.
OnCacheUpdated(NotificationFactory.Create(string.Empty, MessageType.RefreshAll));
}
///
/// Refreshes an entity.
///
/// The entity's identifier.
public virtual void Refresh(int id)
{
OnCacheUpdated(NotificationFactory.Create(id, MessageType.RefreshById));
}
///
/// Refreshes an entity.
///
/// The entity's identifier.
public virtual void Refresh(Guid id)
{
OnCacheUpdated(NotificationFactory.Create(id, MessageType.RefreshById));
}
///
/// Removes an entity.
///
/// The entity's identifier.
public virtual void Remove(int id)
{
OnCacheUpdated(NotificationFactory.Create(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(CacheRefresherNotification notification)
{
EventAggregator.Publish(notification);
}
#endregion
}
}