2013-03-21 22:53:58 +06:00
|
|
|
|
using System;
|
|
|
|
|
|
using Umbraco.Core.Events;
|
|
|
|
|
|
using Umbraco.Core.Sync;
|
2016-01-06 18:08:14 +01:00
|
|
|
|
using Umbraco.Core.Models.EntityBase;
|
2013-03-21 22:53:58 +06:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core.Cache
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2016-05-26 17:12:04 +02:00
|
|
|
|
/// A base class for cache refreshers that handles events.
|
2013-03-21 22:53:58 +06:00
|
|
|
|
/// </summary>
|
2016-05-26 17:12:04 +02:00
|
|
|
|
/// <typeparam name="TInstanceType">The actual cache refresher type.</typeparam>
|
|
|
|
|
|
/// <remarks>The actual cache refresher type is used for strongly typed events.</remarks>
|
2013-03-21 22:53:58 +06:00
|
|
|
|
public abstract class CacheRefresherBase<TInstanceType> : ICacheRefresher
|
2016-05-26 17:12:04 +02:00
|
|
|
|
where TInstanceType : class, ICacheRefresher
|
2013-03-21 22:53:58 +06:00
|
|
|
|
{
|
2016-05-26 17:12:04 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="CacheRefresherBase{TInstanceType}"/>.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="cacheHelper">A cache helper.</param>
|
2016-05-18 18:44:08 +02:00
|
|
|
|
protected CacheRefresherBase(CacheHelper cacheHelper)
|
|
|
|
|
|
{
|
|
|
|
|
|
CacheHelper = cacheHelper;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-03-21 22:53:58 +06:00
|
|
|
|
/// <summary>
|
2016-05-26 17:12:04 +02:00
|
|
|
|
/// Triggers when the cache is updated on the server.
|
2013-03-21 22:53:58 +06:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>
|
2016-05-26 17:12:04 +02:00
|
|
|
|
/// Triggers on each server configured for an Umbraco project whenever a cache refresher is updated.
|
2013-03-21 22:53:58 +06:00
|
|
|
|
/// </remarks>
|
|
|
|
|
|
public static event TypedEventHandler<TInstanceType, CacheRefresherEventArgs> CacheUpdated;
|
|
|
|
|
|
|
2016-05-26 17:12:04 +02:00
|
|
|
|
#region Define
|
2013-03-21 22:53:58 +06:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2016-05-26 17:12:04 +02:00
|
|
|
|
/// Gets the typed 'this' for events.
|
2013-03-21 22:53:58 +06:00
|
|
|
|
/// </summary>
|
2017-07-11 19:21:13 +02:00
|
|
|
|
protected abstract TInstanceType This { get; }
|
2013-03-21 22:53:58 +06:00
|
|
|
|
|
2016-05-26 17:12:04 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the unique identifier of the refresher.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public abstract Guid RefresherUniqueId { get; }
|
2013-03-21 22:53:58 +06:00
|
|
|
|
|
2016-05-26 17:12:04 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the name of the refresher.
|
|
|
|
|
|
/// </summary>
|
2013-03-21 22:53:58 +06:00
|
|
|
|
public abstract string Name { get; }
|
|
|
|
|
|
|
2016-05-26 17:12:04 +02:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Refresher
|
2016-05-18 18:44:08 +02:00
|
|
|
|
|
2016-05-26 17:12:04 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Refreshes all entities.
|
|
|
|
|
|
/// </summary>
|
2013-03-21 22:53:58 +06:00
|
|
|
|
public virtual void RefreshAll()
|
|
|
|
|
|
{
|
2017-07-11 19:21:13 +02:00
|
|
|
|
OnCacheUpdated(This, new CacheRefresherEventArgs(null, MessageType.RefreshAll));
|
2013-03-21 22:53:58 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-26 17:12:04 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Refreshes an entity.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="id">The entity's identifier.</param>
|
2013-03-21 22:53:58 +06:00
|
|
|
|
public virtual void Refresh(int id)
|
|
|
|
|
|
{
|
2017-07-11 19:21:13 +02:00
|
|
|
|
OnCacheUpdated(This, new CacheRefresherEventArgs(id, MessageType.RefreshById));
|
2013-03-21 22:53:58 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-26 17:12:04 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Refreshes an entity.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="id">The entity's identifier.</param>
|
|
|
|
|
|
public virtual void Refresh(Guid id)
|
2013-03-21 22:53:58 +06:00
|
|
|
|
{
|
2017-07-11 19:21:13 +02:00
|
|
|
|
OnCacheUpdated(This, new CacheRefresherEventArgs(id, MessageType.RefreshById));
|
2013-03-21 22:53:58 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-05-26 17:12:04 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Removes an entity.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="id">The entity's identifier.</param>
|
|
|
|
|
|
public virtual void Remove(int id)
|
2013-03-21 22:53:58 +06:00
|
|
|
|
{
|
2017-07-11 19:21:13 +02:00
|
|
|
|
OnCacheUpdated(This, new CacheRefresherEventArgs(id, MessageType.RemoveById));
|
2013-03-21 22:53:58 +06:00
|
|
|
|
}
|
2016-01-06 18:08:14 +01:00
|
|
|
|
|
2016-05-26 17:12:04 +02:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Protected
|
|
|
|
|
|
|
2016-01-06 18:08:14 +01:00
|
|
|
|
/// <summary>
|
2016-05-26 17:12:04 +02:00
|
|
|
|
/// Gets the cache helper.
|
2016-01-06 18:08:14 +01:00
|
|
|
|
/// </summary>
|
2016-05-26 17:12:04 +02:00
|
|
|
|
protected CacheHelper CacheHelper { get; }
|
|
|
|
|
|
|
|
|
|
|
|
/// <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>()
|
2016-01-06 18:08:14 +01:00
|
|
|
|
where TEntity : class, IAggregateRoot
|
|
|
|
|
|
{
|
2016-05-18 18:44:08 +02:00
|
|
|
|
CacheHelper.IsolatedRuntimeCache.ClearCache<TEntity>();
|
2016-01-06 18:08:14 +01:00
|
|
|
|
}
|
2016-05-26 17:12:04 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Raises the CacheUpdated event.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sender">The event sender.</param>
|
|
|
|
|
|
/// <param name="args">The event arguments.</param>
|
|
|
|
|
|
protected static void OnCacheUpdated(TInstanceType sender, CacheRefresherEventArgs args)
|
|
|
|
|
|
{
|
|
|
|
|
|
CacheUpdated?.Invoke(sender, args);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
2013-03-21 22:53:58 +06:00
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|