2017-07-20 11:21:28 +02:00
|
|
|
|
using System;
|
2016-02-02 00:47:18 +01:00
|
|
|
|
using System.Collections.Generic;
|
2018-01-15 11:32:30 +01:00
|
|
|
|
using Umbraco.Core.Models.Entities;
|
2017-05-12 14:49:44 +02:00
|
|
|
|
using Umbraco.Core.Scoping;
|
2016-02-02 00:47:18 +01:00
|
|
|
|
|
|
|
|
|
|
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-06-01 14:31:33 +02:00
|
|
|
|
internal abstract class RepositoryCachePolicyBase<TEntity, TId> : IRepositoryCachePolicy<TEntity, TId>
|
2018-01-10 12:48:51 +01:00
|
|
|
|
where TEntity : class, IEntity
|
2016-02-02 00:47:18 +01:00
|
|
|
|
{
|
2017-12-15 16:29:14 +01:00
|
|
|
|
private readonly IRuntimeCacheProvider _globalCache;
|
|
|
|
|
|
private readonly IScopeAccessor _scopeAccessor;
|
|
|
|
|
|
|
|
|
|
|
|
protected RepositoryCachePolicyBase(IRuntimeCacheProvider globalCache, IScopeAccessor scopeAccessor)
|
2016-02-02 00:47:18 +01:00
|
|
|
|
{
|
2017-12-15 16:29:14 +01:00
|
|
|
|
_globalCache = globalCache ?? throw new ArgumentNullException(nameof(globalCache));
|
|
|
|
|
|
_scopeAccessor = scopeAccessor ?? throw new ArgumentNullException(nameof(scopeAccessor));
|
2017-05-12 14:49:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-15 16:29:14 +01:00
|
|
|
|
protected IRuntimeCacheProvider Cache
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
var ambientScope = _scopeAccessor.AmbientScope;
|
|
|
|
|
|
switch (ambientScope.RepositoryCacheMode)
|
|
|
|
|
|
{
|
|
|
|
|
|
case RepositoryCacheMode.Default:
|
|
|
|
|
|
return _globalCache;
|
|
|
|
|
|
case RepositoryCacheMode.Scoped:
|
|
|
|
|
|
return ambientScope.IsolatedRuntimeCache.GetOrCreateCache<TEntity>();
|
|
|
|
|
|
case RepositoryCacheMode.None:
|
|
|
|
|
|
return NullCacheProvider.Instance;
|
|
|
|
|
|
default:
|
|
|
|
|
|
throw new NotSupportedException($"Repository cache mode {ambientScope.RepositoryCacheMode} is not supported.");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-02-02 00:47:18 +01:00
|
|
|
|
|
2016-06-01 14:31:33 +02:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public abstract TEntity Get(TId id, Func<TId, TEntity> performGet, Func<TId[], IEnumerable<TEntity>> performGetAll);
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public abstract TEntity GetCached(TId id);
|
2016-02-02 00:47:18 +01:00
|
|
|
|
|
2016-06-01 10:35:44 +02:00
|
|
|
|
/// <inheritdoc />
|
2016-06-01 14:31:33 +02:00
|
|
|
|
public abstract bool Exists(TId id, Func<TId, bool> performExists, Func<TId[], IEnumerable<TEntity>> performGetAll);
|
2016-06-01 10:35:44 +02:00
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2016-06-01 14:31:33 +02:00
|
|
|
|
public abstract void Create(TEntity entity, Action<TEntity> persistNew);
|
2016-06-01 10:35:44 +02:00
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2016-06-01 14:31:33 +02:00
|
|
|
|
public abstract void Update(TEntity entity, Action<TEntity> persistUpdated);
|
2016-06-01 10:35:44 +02:00
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2016-06-01 14:31:33 +02:00
|
|
|
|
public abstract void Delete(TEntity entity, Action<TEntity> persistDeleted);
|
2016-06-01 10:35:44 +02:00
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2016-06-01 14:31:33 +02:00
|
|
|
|
public abstract TEntity[] GetAll(TId[] ids, Func<TId[], IEnumerable<TEntity>> performGetAll);
|
2016-06-01 10:35:44 +02:00
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2016-06-01 14:31:33 +02:00
|
|
|
|
public abstract void ClearAll();
|
2016-02-02 00:47:18 +01:00
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|