2018-06-29 19:52:40 +02:00
|
|
|
|
using System;
|
2021-02-18 11:06:02 +01:00
|
|
|
|
using Umbraco.Cms.Core.Events;
|
|
|
|
|
|
using Umbraco.Cms.Core.Models.Entities;
|
|
|
|
|
|
using Umbraco.Cms.Core.Sync;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
2021-02-18 11:06:02 +01: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>
|
2021-03-12 21:48:24 +01:00
|
|
|
|
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>
|
2021-03-12 21:48:24 +01:00
|
|
|
|
protected CacheRefresherBase(AppCaches appCaches, IEventAggregator eventAggregator)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2019-01-17 08:34:29 +01:00
|
|
|
|
AppCaches = appCaches;
|
2021-03-12 21:48:24 +01:00
|
|
|
|
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()
|
|
|
|
|
|
{
|
2021-03-12 21:48:24 +01:00
|
|
|
|
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)
|
|
|
|
|
|
{
|
2021-03-12 21:48:24 +01:00
|
|
|
|
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)
|
|
|
|
|
|
{
|
2021-03-12 21:48:24 +01:00
|
|
|
|
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)
|
|
|
|
|
|
{
|
2021-03-12 21:48:24 +01:00
|
|
|
|
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
|
|
|
|
|
2021-03-12 21:48:24 +01: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>
|
2021-03-12 21:48:24 +01:00
|
|
|
|
protected void OnCacheUpdated(CacheRefresherNotificationBase notification)
|
2018-06-29 19:52:40 +02:00
|
|
|
|
{
|
2021-03-12 21:48:24 +01:00
|
|
|
|
EventAggregator.Publish(notification);
|
2018-06-29 19:52:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|