2016-01-07 16:31:20 +01:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Umbraco.Core.Models.EntityBase;
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core.Cache
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2016-06-01 10:35:44 +02:00
|
|
|
/// Represents the default cache policy.
|
2016-01-07 16:31:20 +01:00
|
|
|
/// </summary>
|
2016-06-01 10:35:44 +02:00
|
|
|
/// <typeparam name="TEntity">The type of the entity.</typeparam>
|
|
|
|
|
/// <typeparam name="TId">The type of the identifier.</typeparam>
|
2016-01-26 19:13:42 +01:00
|
|
|
/// <remarks>
|
2016-06-01 10:35:44 +02:00
|
|
|
/// <para>The default cache policy caches entities with a 5 minutes sliding expiration.</para>
|
|
|
|
|
/// <para>Each entity is cached individually.</para>
|
|
|
|
|
/// <para>If options.GetAllCacheAllowZeroCount then a 'zero-count' array is cached when GetAll finds nothing.</para>
|
|
|
|
|
/// <para>If options.GetAllCacheValidateCount then we check against the db when getting many entities.</para>
|
2016-01-26 19:13:42 +01:00
|
|
|
/// </remarks>
|
2016-02-02 00:47:18 +01:00
|
|
|
internal class DefaultRepositoryCachePolicy<TEntity, TId> : RepositoryCachePolicyBase<TEntity, TId>
|
2016-01-07 16:31:20 +01:00
|
|
|
where TEntity : class, IAggregateRoot
|
|
|
|
|
{
|
2016-06-01 10:35:44 +02:00
|
|
|
private static readonly TEntity[] EmptyEntities = new TEntity[0];
|
2016-01-07 16:31:20 +01:00
|
|
|
private readonly RepositoryCachePolicyOptions _options;
|
2016-06-01 10:35:44 +02:00
|
|
|
|
2016-01-07 16:31:20 +01:00
|
|
|
public DefaultRepositoryCachePolicy(IRuntimeCacheProvider cache, RepositoryCachePolicyOptions options)
|
2016-02-02 00:47:18 +01:00
|
|
|
: base(cache)
|
2016-06-01 10:35:44 +02:00
|
|
|
{
|
|
|
|
|
if (options == null) throw new ArgumentNullException(nameof(options));
|
|
|
|
|
_options = options;
|
2016-01-07 16:31:20 +01:00
|
|
|
}
|
|
|
|
|
|
2016-06-01 10:35:44 +02:00
|
|
|
protected string GetEntityCacheKey(object id)
|
2016-01-07 16:31:20 +01:00
|
|
|
{
|
2016-06-01 10:35:44 +02:00
|
|
|
if (id == null) throw new ArgumentNullException(nameof(id));
|
|
|
|
|
return GetEntityTypeCacheKey() + id;
|
|
|
|
|
}
|
2016-01-14 18:11:48 +01:00
|
|
|
|
2016-06-01 10:35:44 +02:00
|
|
|
protected string GetEntityTypeCacheKey()
|
|
|
|
|
{
|
|
|
|
|
return $"uRepo_{typeof (TEntity).Name}_";
|
2016-01-07 16:31:20 +01:00
|
|
|
}
|
|
|
|
|
|
2016-06-01 10:35:44 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the action to execute on disposal for a single entity
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="cacheKey"></param>
|
|
|
|
|
/// <param name="entity"></param>
|
|
|
|
|
protected virtual void SetCacheActionToInsertEntity(string cacheKey, TEntity entity)
|
2016-01-07 16:31:20 +01:00
|
|
|
{
|
2016-06-01 10:35:44 +02:00
|
|
|
SetCacheAction(() =>
|
|
|
|
|
{
|
|
|
|
|
Cache.InsertCacheItem(cacheKey, () => entity, TimeSpan.FromMinutes(5), true);
|
|
|
|
|
});
|
2016-01-07 16:31:20 +01:00
|
|
|
}
|
|
|
|
|
|
2016-06-01 10:35:44 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Sets the action to execute on disposal for an entity collection
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ids"></param>
|
|
|
|
|
/// <param name="entities"></param>
|
|
|
|
|
protected virtual void SetCacheActionToInsertEntities(TId[] ids, TEntity[] entities)
|
2016-01-07 16:31:20 +01:00
|
|
|
{
|
2016-06-01 10:35:44 +02:00
|
|
|
SetCacheAction(() =>
|
|
|
|
|
{
|
|
|
|
|
if (ids.Length == 0 && entities.Length == 0 && _options.GetAllCacheAllowZeroCount)
|
|
|
|
|
{
|
|
|
|
|
// getting all of them, and finding nothing.
|
|
|
|
|
// if we can cache a zero count, cache an empty array,
|
|
|
|
|
// for as long as the cache is not cleared (no expiration)
|
|
|
|
|
Cache.InsertCacheItem(GetEntityTypeCacheKey(), () => EmptyEntities);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// individually cache each item
|
|
|
|
|
foreach (var entity in entities)
|
|
|
|
|
{
|
|
|
|
|
var capture = entity;
|
|
|
|
|
Cache.InsertCacheItem(GetEntityCacheKey(entity.Id), () => capture, TimeSpan.FromMinutes(5), true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override void CreateOrUpdate(TEntity entity, Action<TEntity> repoCreateOrUpdate)
|
|
|
|
|
{
|
|
|
|
|
if (entity == null) throw new ArgumentNullException(nameof(entity));
|
|
|
|
|
if (repoCreateOrUpdate == null) throw new ArgumentNullException(nameof(repoCreateOrUpdate));
|
2016-01-14 18:11:48 +01:00
|
|
|
|
2016-01-07 16:31:20 +01:00
|
|
|
try
|
|
|
|
|
{
|
2016-06-01 10:35:44 +02:00
|
|
|
repoCreateOrUpdate(entity);
|
2016-01-07 16:31:20 +01:00
|
|
|
|
2016-01-07 17:54:55 +01:00
|
|
|
SetCacheAction(() =>
|
|
|
|
|
{
|
2016-06-01 10:35:44 +02:00
|
|
|
// just to be safe, we cannot cache an item without an identity
|
2016-01-14 16:36:03 +01:00
|
|
|
if (entity.HasIdentity)
|
|
|
|
|
{
|
2016-06-01 10:35:44 +02:00
|
|
|
Cache.InsertCacheItem(GetEntityCacheKey(entity.Id), () => entity, TimeSpan.FromMinutes(5), true);
|
2016-01-14 16:36:03 +01:00
|
|
|
}
|
2016-06-01 10:35:44 +02:00
|
|
|
|
|
|
|
|
// if there's a GetAllCacheAllowZeroCount cache, ensure it is cleared
|
|
|
|
|
Cache.ClearCacheItem(GetEntityTypeCacheKey());
|
2016-01-07 17:54:55 +01:00
|
|
|
});
|
2016-06-01 10:35:44 +02:00
|
|
|
|
2016-01-07 16:31:20 +01:00
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
2016-01-07 17:54:55 +01:00
|
|
|
SetCacheAction(() =>
|
|
|
|
|
{
|
2016-06-01 10:35:44 +02:00
|
|
|
// if an exception is thrown we need to remove the entry from cache,
|
|
|
|
|
// this is ONLY a work around because of the way
|
2016-01-07 17:54:55 +01:00
|
|
|
// that we cache entities: http://issues.umbraco.org/issue/U4-4259
|
2016-06-01 10:35:44 +02:00
|
|
|
Cache.ClearCacheItem(GetEntityCacheKey(entity.Id));
|
2016-01-07 16:31:20 +01:00
|
|
|
|
2016-06-01 10:35:44 +02:00
|
|
|
// if there's a GetAllCacheAllowZeroCount cache, ensure it is cleared
|
|
|
|
|
Cache.ClearCacheItem(GetEntityTypeCacheKey());
|
2016-01-07 17:54:55 +01:00
|
|
|
});
|
2016-06-01 10:35:44 +02:00
|
|
|
|
2016-01-07 16:31:20 +01:00
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-01 10:35:44 +02:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override void Remove(TEntity entity, Action<TEntity> repoRemove)
|
2016-01-07 16:31:20 +01:00
|
|
|
{
|
2016-06-01 10:35:44 +02:00
|
|
|
if (entity == null) throw new ArgumentNullException(nameof(entity));
|
|
|
|
|
if (repoRemove == null) throw new ArgumentNullException(nameof(repoRemove));
|
2016-01-14 18:11:48 +01:00
|
|
|
|
2016-02-02 00:47:18 +01:00
|
|
|
try
|
|
|
|
|
{
|
2016-06-01 10:35:44 +02:00
|
|
|
repoRemove(entity);
|
2016-02-02 00:47:18 +01:00
|
|
|
}
|
|
|
|
|
finally
|
2016-01-07 17:54:55 +01:00
|
|
|
{
|
2016-06-01 10:35:44 +02:00
|
|
|
// whatever happens, clear the cache
|
|
|
|
|
var cacheKey = GetEntityCacheKey(entity.Id);
|
2016-02-02 00:47:18 +01:00
|
|
|
SetCacheAction(() =>
|
|
|
|
|
{
|
|
|
|
|
Cache.ClearCacheItem(cacheKey);
|
2016-06-01 10:35:44 +02:00
|
|
|
|
|
|
|
|
// if there's a GetAllCacheAllowZeroCount cache, ensure it is cleared
|
|
|
|
|
Cache.ClearCacheItem(GetEntityTypeCacheKey());
|
2016-02-02 00:47:18 +01:00
|
|
|
});
|
|
|
|
|
}
|
2016-01-07 16:31:20 +01:00
|
|
|
}
|
|
|
|
|
|
2016-06-01 10:35:44 +02:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override TEntity Get(TId id, Func<TId, TEntity> repoGet)
|
2016-01-07 16:31:20 +01:00
|
|
|
{
|
2016-06-01 10:35:44 +02:00
|
|
|
if (repoGet == null) throw new ArgumentNullException(nameof(repoGet));
|
2016-01-14 18:11:48 +01:00
|
|
|
|
2016-06-01 10:35:44 +02:00
|
|
|
var cacheKey = GetEntityCacheKey(id);
|
2016-01-07 16:31:20 +01:00
|
|
|
var fromCache = Cache.GetCacheItem<TEntity>(cacheKey);
|
2016-06-01 10:35:44 +02:00
|
|
|
|
|
|
|
|
// if found in cache then return else fetch and cache
|
2016-01-07 16:31:20 +01:00
|
|
|
if (fromCache != null)
|
|
|
|
|
return fromCache;
|
2016-06-01 10:35:44 +02:00
|
|
|
var entity = repoGet(id);
|
2016-01-07 16:31:20 +01:00
|
|
|
|
2016-06-01 10:35:44 +02:00
|
|
|
if (entity != null && entity.HasIdentity)
|
|
|
|
|
SetCacheActionToInsertEntity(cacheKey, entity);
|
2016-01-07 16:31:20 +01:00
|
|
|
|
|
|
|
|
return entity;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-01 10:35:44 +02:00
|
|
|
/// <inheritdoc />
|
2016-02-02 00:47:18 +01:00
|
|
|
public override TEntity Get(TId id)
|
2016-01-07 16:31:20 +01:00
|
|
|
{
|
2016-06-01 10:35:44 +02:00
|
|
|
var cacheKey = GetEntityCacheKey(id);
|
2016-01-07 16:31:20 +01:00
|
|
|
return Cache.GetCacheItem<TEntity>(cacheKey);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-01 10:35:44 +02:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override bool Exists(TId id, Func<TId, bool> repoExists)
|
2016-01-07 16:31:20 +01:00
|
|
|
{
|
2016-06-01 10:35:44 +02:00
|
|
|
if (repoExists == null) throw new ArgumentNullException(nameof(repoExists));
|
2016-01-14 18:11:48 +01:00
|
|
|
|
2016-06-01 10:35:44 +02:00
|
|
|
// if found in cache the return else check
|
|
|
|
|
var cacheKey = GetEntityCacheKey(id);
|
2016-01-07 16:31:20 +01:00
|
|
|
var fromCache = Cache.GetCacheItem<TEntity>(cacheKey);
|
2016-06-01 10:35:44 +02:00
|
|
|
return fromCache != null || repoExists(id);
|
2016-01-07 16:31:20 +01:00
|
|
|
}
|
|
|
|
|
|
2016-06-01 10:35:44 +02:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public override TEntity[] GetAll(TId[] ids, Func<TId[], IEnumerable<TEntity>> repoGet)
|
2016-01-07 16:31:20 +01:00
|
|
|
{
|
2016-06-01 10:35:44 +02:00
|
|
|
if (repoGet == null) throw new ArgumentNullException(nameof(repoGet));
|
2016-01-14 18:11:48 +01:00
|
|
|
|
2016-06-01 10:35:44 +02:00
|
|
|
if (ids.Length > 0)
|
2016-01-07 16:31:20 +01:00
|
|
|
{
|
2016-06-01 10:35:44 +02:00
|
|
|
// try to get each entity from the cache
|
|
|
|
|
// if we can find all of them, return
|
2016-01-07 16:31:20 +01:00
|
|
|
var entities = ids.Select(Get).ToArray();
|
2016-06-01 10:35:44 +02:00
|
|
|
if (ids.Length.Equals(entities.Length))
|
|
|
|
|
return entities; // no need for null checks, we are not caching nulls
|
2016-01-07 16:31:20 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-06-01 10:35:44 +02:00
|
|
|
// get everything we have
|
|
|
|
|
var entities = Cache.GetCacheItemsByKeySearch<TEntity>(GetEntityTypeCacheKey())
|
|
|
|
|
.ToArray(); // no need for null checks, we are not caching nulls
|
|
|
|
|
|
|
|
|
|
if (entities.Length > 0)
|
2016-01-07 16:31:20 +01:00
|
|
|
{
|
2016-06-01 10:35:44 +02:00
|
|
|
// if some of them were in the cache...
|
2016-01-07 16:31:20 +01:00
|
|
|
if (_options.GetAllCacheValidateCount)
|
|
|
|
|
{
|
2016-06-01 10:35:44 +02:00
|
|
|
// need to validate the count, get the actual count and return if ok
|
2016-01-07 16:31:20 +01:00
|
|
|
var totalCount = _options.PerformCount();
|
2016-06-01 10:35:44 +02:00
|
|
|
if (entities.Length == totalCount)
|
|
|
|
|
return entities;
|
2016-01-07 16:31:20 +01:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-06-01 10:35:44 +02:00
|
|
|
// no need to validate, just return what we have and assume it's all there is
|
|
|
|
|
return entities;
|
2016-01-07 16:31:20 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (_options.GetAllCacheAllowZeroCount)
|
|
|
|
|
{
|
2016-06-01 10:35:44 +02:00
|
|
|
// if none of them were in the cache
|
|
|
|
|
// and we allow zero count - check for the special (empty) entry
|
|
|
|
|
var empty = Cache.GetCacheItem<TEntity[]>(GetEntityTypeCacheKey());
|
|
|
|
|
if (empty != null) return empty;
|
2016-01-07 16:31:20 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-01 10:35:44 +02:00
|
|
|
// cache failed, get from repo and cache
|
|
|
|
|
var repoEntities = repoGet(ids)
|
|
|
|
|
.WhereNotNull() // exclude nulls!
|
|
|
|
|
.Where(x => x.HasIdentity) // be safe, though would be weird...
|
2016-01-07 16:31:20 +01:00
|
|
|
.ToArray();
|
|
|
|
|
|
2016-06-01 10:35:44 +02:00
|
|
|
// note: if empty & allow zero count, will cache a special (empty) entry
|
|
|
|
|
SetCacheActionToInsertEntities(ids, repoEntities);
|
2016-01-07 16:31:20 +01:00
|
|
|
|
2016-06-01 10:35:44 +02:00
|
|
|
return repoEntities;
|
2016-01-07 16:31:20 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|