using System;
using System.Collections.Generic;
using Umbraco.Core.Models.Entities;
using Umbraco.Core.Scoping;
namespace Umbraco.Core.Cache
{
///
/// A base class for repository cache policies.
///
/// The type of the entity.
/// The type of the identifier.
internal abstract class RepositoryCachePolicyBase : IRepositoryCachePolicy
where TEntity : class, IEntity
{
private readonly IAppPolicyCache _globalCache;
private readonly IScopeAccessor _scopeAccessor;
protected RepositoryCachePolicyBase(IAppPolicyCache globalCache, IScopeAccessor scopeAccessor)
{
_globalCache = globalCache ?? throw new ArgumentNullException(nameof(globalCache));
_scopeAccessor = scopeAccessor ?? throw new ArgumentNullException(nameof(scopeAccessor));
}
protected IAppPolicyCache Cache
{
get
{
var ambientScope = _scopeAccessor.AmbientScope;
switch (ambientScope.RepositoryCacheMode)
{
case RepositoryCacheMode.Default:
return _globalCache;
case RepositoryCacheMode.Scoped:
return ambientScope.IsolatedCaches.GetOrCreate();
case RepositoryCacheMode.None:
return NoAppCache.Instance;
default:
throw new NotSupportedException($"Repository cache mode {ambientScope.RepositoryCacheMode} is not supported.");
}
}
}
///
public abstract TEntity Get(TId id, Func performGet, Func> performGetAll);
///
public abstract TEntity GetCached(TId id);
///
public abstract bool Exists(TId id, Func performExists, Func> performGetAll);
///
public abstract void Create(TEntity entity, Action persistNew);
///
public abstract void Update(TEntity entity, Action persistUpdated);
///
public abstract void Delete(TEntity entity, Action persistDeleted);
///
public abstract TEntity[] GetAll(TId[] ids, Func> performGetAll);
///
public abstract void ClearAll();
}
}