2018-06-29 19:52:40 +02:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Umbraco.Core;
|
|
|
|
|
using Umbraco.Core.Cache;
|
2019-12-19 10:43:00 +01:00
|
|
|
using Umbraco.Core.Sync;
|
2018-06-29 19:52:40 +02:00
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Cache
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents the entry point into Umbraco's distributed cache infrastructure.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// The distributed cache infrastructure ensures that distributed caches are
|
|
|
|
|
/// invalidated properly in load balancing environments.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// Distribute caches include static (in-memory) cache, runtime cache, front-end content cache, Examine/Lucene indexes
|
|
|
|
|
/// </para>
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public sealed class DistributedCache
|
|
|
|
|
{
|
2019-12-19 10:43:00 +01:00
|
|
|
private readonly IServerMessenger _serverMessenger;
|
|
|
|
|
private readonly CacheRefresherCollection _cacheRefreshers;
|
|
|
|
|
|
|
|
|
|
public DistributedCache(IServerMessenger serverMessenger, CacheRefresherCollection cacheRefreshers)
|
|
|
|
|
{
|
|
|
|
|
_serverMessenger = serverMessenger;
|
|
|
|
|
_cacheRefreshers = cacheRefreshers;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-29 19:52:40 +02:00
|
|
|
#region Core notification methods
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-01-26 10:52:19 -05:00
|
|
|
/// Notifies the distributed cache of specified item invalidation, for a specified <see cref="ICacheRefresher"/>.
|
2018-06-29 19:52:40 +02:00
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">The type of the invalidated items.</typeparam>
|
|
|
|
|
/// <param name="refresherGuid">The unique identifier of the ICacheRefresher.</param>
|
|
|
|
|
/// <param name="getNumericId">A function returning the unique identifier of items.</param>
|
|
|
|
|
/// <param name="instances">The invalidated items.</param>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This method is much better for performance because it does not need to re-lookup object instances.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public void Refresh<T>(Guid refresherGuid, Func<T, int> getNumericId, params T[] instances)
|
|
|
|
|
{
|
|
|
|
|
if (refresherGuid == Guid.Empty || instances.Length == 0 || getNumericId == null) return;
|
|
|
|
|
|
2020-12-24 14:44:42 +11:00
|
|
|
_serverMessenger.QueueRefresh(
|
2018-06-29 19:52:40 +02:00
|
|
|
GetRefresherById(refresherGuid),
|
|
|
|
|
getNumericId,
|
|
|
|
|
instances);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Notifies the distributed cache of a specified item invalidation, for a specified <see cref="ICacheRefresher"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="refresherGuid">The unique identifier of the ICacheRefresher.</param>
|
|
|
|
|
/// <param name="id">The unique identifier of the invalidated item.</param>
|
|
|
|
|
public void Refresh(Guid refresherGuid, int id)
|
|
|
|
|
{
|
|
|
|
|
if (refresherGuid == Guid.Empty || id == default(int)) return;
|
|
|
|
|
|
2020-12-24 14:44:42 +11:00
|
|
|
_serverMessenger.QueueRefresh(
|
2018-06-29 19:52:40 +02:00
|
|
|
GetRefresherById(refresherGuid),
|
|
|
|
|
id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Notifies the distributed cache of a specified item invalidation, for a specified <see cref="ICacheRefresher"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="refresherGuid">The unique identifier of the ICacheRefresher.</param>
|
|
|
|
|
/// <param name="id">The unique identifier of the invalidated item.</param>
|
|
|
|
|
public void Refresh(Guid refresherGuid, Guid id)
|
|
|
|
|
{
|
|
|
|
|
if (refresherGuid == Guid.Empty || id == Guid.Empty) return;
|
|
|
|
|
|
2020-12-24 14:44:42 +11:00
|
|
|
_serverMessenger.QueueRefresh(
|
2018-06-29 19:52:40 +02:00
|
|
|
GetRefresherById(refresherGuid),
|
|
|
|
|
id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// payload should be an object, or array of objects, NOT a
|
|
|
|
|
// Linq enumerable of some sort (IEnumerable, query...)
|
|
|
|
|
public void RefreshByPayload<TPayload>(Guid refresherGuid, TPayload[] payload)
|
|
|
|
|
{
|
|
|
|
|
if (refresherGuid == Guid.Empty || payload == null) return;
|
|
|
|
|
|
2020-12-24 14:44:42 +11:00
|
|
|
_serverMessenger.QueueRefresh(
|
2018-06-29 19:52:40 +02:00
|
|
|
GetRefresherById(refresherGuid),
|
|
|
|
|
payload);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// so deal with IEnumerable
|
|
|
|
|
public void RefreshByPayload<TPayload>(Guid refresherGuid, IEnumerable<TPayload> payloads)
|
|
|
|
|
where TPayload : class
|
|
|
|
|
{
|
|
|
|
|
if (refresherGuid == Guid.Empty || payloads == null) return;
|
|
|
|
|
|
2020-12-24 14:44:42 +11:00
|
|
|
_serverMessenger.QueueRefresh(
|
2018-06-29 19:52:40 +02:00
|
|
|
GetRefresherById(refresherGuid),
|
|
|
|
|
payloads.ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
///// <summary>
|
|
|
|
|
///// Notifies the distributed cache, for a specified <see cref="ICacheRefresher"/>.
|
|
|
|
|
///// </summary>
|
|
|
|
|
///// <param name="refresherId">The unique identifier of the ICacheRefresher.</param>
|
|
|
|
|
///// <param name="payload">The notification content.</param>
|
|
|
|
|
//internal void Notify(Guid refresherId, object payload)
|
|
|
|
|
//{
|
|
|
|
|
// if (refresherId == Guid.Empty || payload == null) return;
|
|
|
|
|
|
2019-12-19 10:43:00 +01:00
|
|
|
// _serverMessenger.Notify(
|
2018-06-29 19:52:40 +02:00
|
|
|
// Current.ServerRegistrar.Registrations,
|
|
|
|
|
// GetRefresherById(refresherId),
|
|
|
|
|
// json);
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Notifies the distributed cache of a global invalidation for a specified <see cref="ICacheRefresher"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="refresherGuid">The unique identifier of the ICacheRefresher.</param>
|
|
|
|
|
public void RefreshAll(Guid refresherGuid)
|
|
|
|
|
{
|
|
|
|
|
if (refresherGuid == Guid.Empty) return;
|
|
|
|
|
|
2020-12-24 14:44:42 +11:00
|
|
|
_serverMessenger.QueueRefreshAll(
|
2018-06-29 19:52:40 +02:00
|
|
|
GetRefresherById(refresherGuid));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Notifies the distributed cache of a specified item removal, for a specified <see cref="ICacheRefresher"/>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="refresherGuid">The unique identifier of the ICacheRefresher.</param>
|
|
|
|
|
/// <param name="id">The unique identifier of the removed item.</param>
|
|
|
|
|
public void Remove(Guid refresherGuid, int id)
|
|
|
|
|
{
|
|
|
|
|
if (refresherGuid == Guid.Empty || id == default(int)) return;
|
|
|
|
|
|
2020-12-24 14:44:42 +11:00
|
|
|
_serverMessenger.QueueRemove(
|
2018-06-29 19:52:40 +02:00
|
|
|
GetRefresherById(refresherGuid),
|
|
|
|
|
id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2019-01-26 10:52:19 -05:00
|
|
|
/// Notifies the distributed cache of specified item removal, for a specified <see cref="ICacheRefresher"/>.
|
2018-06-29 19:52:40 +02:00
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">The type of the removed items.</typeparam>
|
|
|
|
|
/// <param name="refresherGuid">The unique identifier of the ICacheRefresher.</param>
|
|
|
|
|
/// <param name="getNumericId">A function returning the unique identifier of items.</param>
|
|
|
|
|
/// <param name="instances">The removed items.</param>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This method is much better for performance because it does not need to re-lookup object instances.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public void Remove<T>(Guid refresherGuid, Func<T, int> getNumericId, params T[] instances)
|
|
|
|
|
{
|
2020-12-24 14:44:42 +11:00
|
|
|
_serverMessenger.QueueRemove(
|
2018-06-29 19:52:40 +02:00
|
|
|
GetRefresherById(refresherGuid),
|
|
|
|
|
getNumericId,
|
|
|
|
|
instances);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
// helper method to get an ICacheRefresher by its unique identifier
|
2020-12-24 14:29:26 +11:00
|
|
|
private ICacheRefresher GetRefresherById(Guid refresherGuid)
|
2018-06-29 19:52:40 +02:00
|
|
|
{
|
2020-12-24 14:29:26 +11:00
|
|
|
ICacheRefresher refresher = _cacheRefreshers[refresherGuid];
|
|
|
|
|
if (refresher == null)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException($"No cache refresher found with id {refresherGuid}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return refresher;
|
2018-06-29 19:52:40 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|