2016-02-02 00:47:18 +01:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Umbraco.Core.Models.EntityBase;
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core.Cache
|
|
|
|
|
{
|
2016-06-01 10:35:44 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// A base class for repository cache policies.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="TEntity">The type of the entity.</typeparam>
|
|
|
|
|
/// <typeparam name="TId">The type of the identifier.</typeparam>
|
2016-02-02 00:47:18 +01:00
|
|
|
internal abstract class RepositoryCachePolicyBase<TEntity, TId> : DisposableObject, IRepositoryCachePolicy<TEntity, TId>
|
|
|
|
|
where TEntity : class, IAggregateRoot
|
|
|
|
|
{
|
|
|
|
|
private Action _action;
|
|
|
|
|
|
|
|
|
|
protected RepositoryCachePolicyBase(IRuntimeCacheProvider cache)
|
|
|
|
|
{
|
2016-06-01 10:35:44 +02:00
|
|
|
if (cache == null) throw new ArgumentNullException(nameof(cache));
|
2016-02-02 00:47:18 +01:00
|
|
|
Cache = cache;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-01 10:35:44 +02:00
|
|
|
protected IRuntimeCacheProvider Cache { get; }
|
2016-02-02 00:47:18 +01:00
|
|
|
|
|
|
|
|
/// <summary>
|
2016-06-01 10:35:44 +02:00
|
|
|
/// Disposing performs the actual caching action.
|
2016-02-02 00:47:18 +01:00
|
|
|
/// </summary>
|
|
|
|
|
protected override void DisposeResources()
|
|
|
|
|
{
|
2016-06-01 10:35:44 +02:00
|
|
|
_action?.Invoke();
|
2016-02-02 00:47:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2016-06-01 10:35:44 +02:00
|
|
|
/// Sets the action to execute when being disposed.
|
2016-02-02 00:47:18 +01:00
|
|
|
/// </summary>
|
2016-06-01 10:35:44 +02:00
|
|
|
/// <param name="action">An action to perform when being disposed.</param>
|
2016-02-02 00:47:18 +01:00
|
|
|
protected void SetCacheAction(Action action)
|
|
|
|
|
{
|
|
|
|
|
_action = action;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-01 10:35:44 +02:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public abstract TEntity Get(TId id, Func<TId, TEntity> repoGet);
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2016-02-02 00:47:18 +01:00
|
|
|
public abstract TEntity Get(TId id);
|
2016-06-01 10:35:44 +02:00
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public abstract bool Exists(TId id, Func<TId, bool> repoExists);
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public abstract void CreateOrUpdate(TEntity entity, Action<TEntity> repoCreateOrUpdate);
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public abstract void Remove(TEntity entity, Action<TEntity> repoRemove);
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public abstract TEntity[] GetAll(TId[] ids, Func<TId[], IEnumerable<TEntity>> repoGet);
|
2016-02-02 00:47:18 +01:00
|
|
|
}
|
|
|
|
|
}
|