using System.Linq;
using Umbraco.Core.Collections;
using Umbraco.Core.Models.EntityBase;
namespace Umbraco.Core.Cache
{
///
/// A caching policy that caches an entire dataset as a single collection
///
///
///
internal class FullDataSetRepositoryCachePolicy : DefaultRepositoryCachePolicy
where TEntity : class, IAggregateRoot
{
public FullDataSetRepositoryCachePolicy(IRuntimeCacheProvider cache) : base(cache, new RepositoryCachePolicyOptions())
{
}
///
/// For this type of caching policy, we don't cache individual items
///
///
///
protected override void SetCacheAction(string cacheKey, TEntity entity)
{
//do nothing
}
///
/// Sets the action to execute on disposal for an entity collection
///
///
///
protected override void SetCacheAction(TId[] ids, TEntity[] entityCollection)
{
//for this type of caching policy, we don't want to cache any GetAll request containing specific Ids
if (ids.Any()) return;
//set the disposal action
SetCacheAction(() =>
{
//We want to cache the result as a single collection
Cache.InsertCacheItem(GetCacheTypeKey(), () => new DeepCloneableList(entityCollection));
});
}
///
/// This policy will cache the full data set as a single collection
///
///
protected override TEntity[] GetAllFromCache()
{
var found = Cache.GetCacheItem>(GetCacheTypeKey());
return found == null ? new TEntity[] { } : found.WhereNotNull().ToArray();
}
}
}