U4-9322 - begin implementing scoped repository caches

This commit is contained in:
Stephan
2017-01-19 19:47:09 +01:00
parent 4ab7f768fa
commit 434703f4ec
14 changed files with 384 additions and 35 deletions

View File

@@ -9,6 +9,7 @@ using Umbraco.Core.Models.EntityBase;
using Umbraco.Core.Persistence.Querying;
using Umbraco.Core.Persistence.UnitOfWork;
using Umbraco.Core.Scoping;
namespace Umbraco.Core.Persistence.Repositories
{
@@ -84,7 +85,7 @@ namespace Umbraco.Core.Persistence.Repositories
#region Static Queries
private IQuery<TEntity> _hasIdQuery;
private static IQuery<TEntity> _hasIdQuery;
#endregion
@@ -101,23 +102,56 @@ namespace Umbraco.Core.Persistence.Repositories
get { return RepositoryCache.IsolatedRuntimeCache.GetOrCreateCache<TEntity>(); }
}
private IRepositoryCachePolicy<TEntity, TId> _cachePolicy;
private static RepositoryCachePolicyOptions _defaultOptions;
protected virtual RepositoryCachePolicyOptions DefaultOptions
{
get
{
return _defaultOptions ?? (_defaultOptions
= new RepositoryCachePolicyOptions(() =>
{
// get count of all entities of current type (TEntity) to ensure cached result is correct
// create query once if it is needed (no need for locking here) - query is static!
var query = _hasIdQuery ?? (_hasIdQuery = Query<TEntity>.Builder.Where(x => x.Id != 0));
return PerformCount(query);
}));
}
}
// this would be better for perfs BUT it breaks the tests - l8tr
//
//private static IRepositoryCachePolicy<TEntity, TId> _defaultCachePolicy;
//protected virtual IRepositoryCachePolicy<TEntity, TId> DefaultCachePolicy
//{
// get
// {
// return _defaultCachePolicy ?? (_defaultCachePolicy
// = new DefaultRepositoryCachePolicy<TEntity, TId>(RuntimeCache, DefaultOptions));
// }
//}
private IRepositoryCachePolicy<TEntity, TId> _cachePolicy;
protected virtual IRepositoryCachePolicy<TEntity, TId> CachePolicy
{
get
{
if (_cachePolicy != null) return _cachePolicy;
var options = new RepositoryCachePolicyOptions(() =>
var scope = ((PetaPocoUnitOfWork) UnitOfWork).Scope;
switch (scope.RepositoryCacheMode)
{
//Get count of all entities of current type (TEntity) to ensure cached result is correct
//create query once if it is needed (no need for locking here)
var query = _hasIdQuery ?? (_hasIdQuery = Query<TEntity>.Builder.Where(x => x.Id != 0));
return PerformCount(query);
});
_cachePolicy = new DefaultRepositoryCachePolicy<TEntity, TId>(RuntimeCache, options);
case RepositoryCacheMode.Default:
//_cachePolicy = DefaultCachePolicy;
_cachePolicy = new DefaultRepositoryCachePolicy<TEntity, TId>(RuntimeCache, DefaultOptions);
break;
case RepositoryCacheMode.Scoped:
var scopedCache = scope.IsolatedRuntimeCache.GetOrCreateCache<TEntity>();
var scopedPolicy = new DefaultRepositoryCachePolicy<TEntity, TId>(scopedCache, DefaultOptions);
_cachePolicy = new ScopedDefaultRepositoryCachePolicy<TEntity, TId>(scopedPolicy, RuntimeCache, scope);
break;
default:
throw new Exception();
}
return _cachePolicy;
}

View File

@@ -144,14 +144,14 @@ namespace Umbraco.Core.Persistence.UnitOfWork
get { return _key; }
}
private IScope ThisScope
public IScope Scope
{
get { return _scope ?? (_scope = _scopeProvider.CreateScope(_isolationLevel)); }
}
public UmbracoDatabase Database
{
get { return ThisScope.Database; }
get { return Scope.Database; }
}
#region Operation