Files
Umbraco-CMS/src/Umbraco.Core/Cache/CacheRefresherBase.cs

111 lines
3.4 KiB
C#
Raw Normal View History

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