Updates all cache refreshers to reference the IsolatedRuntimeCache where required, refactors how IsolatedRuntimeCache is exposed on the CacheHelper, ensures that IsolatedRuntimeCache is used in all repositories instead of the global RuntimeCache, changes all obsolete usages of CacheHelper to the non obsolete equivalent, obsoletes many of the cache keys, obsoletes a couple cache refreshers that aren't used anymore in 7.3, simplifies CacheHelper with regards to it's 'disabled' cache state.

This commit is contained in:
Shannon
2016-01-06 18:08:14 +01:00
parent 4f40fff5ee
commit ff829d49a3
55 changed files with 527 additions and 497 deletions

View File

@@ -0,0 +1,91 @@
using System;
using System.Collections.Concurrent;
namespace Umbraco.Core.Cache
{
/// <summary>
/// Used to get/create/manipulate isolated runtime cache
/// </summary>
/// <remarks>
/// This is useful for repository level caches to ensure that cache lookups by key are fast so
/// that the repository doesn't need to search through all keys on a global scale.
/// </remarks>
public class IsolatedRuntimeCache
{
private readonly Func<Type, IRuntimeCacheProvider> _cacheFactory;
/// <summary>
/// Constructor that allows specifying a factory for the type of runtime isolated cache to create
/// </summary>
/// <param name="cacheFactory"></param>
public IsolatedRuntimeCache(Func<Type, IRuntimeCacheProvider> cacheFactory)
{
_cacheFactory = cacheFactory;
}
private readonly ConcurrentDictionary<Type, IRuntimeCacheProvider> _isolatedCache = new ConcurrentDictionary<Type, IRuntimeCacheProvider>();
/// <summary>
/// Returns an isolated runtime cache for a given type
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public IRuntimeCacheProvider GetOrCreateCache<T>()
{
return _isolatedCache.GetOrAdd(typeof(T), type => _cacheFactory(type));
}
/// <summary>
/// Returns an isolated runtime cache for a given type
/// </summary>
/// <returns></returns>
public IRuntimeCacheProvider GetOrCreateCache(Type type)
{
return _isolatedCache.GetOrAdd(type, t => _cacheFactory(t));
}
/// <summary>
/// Tries to get a cache by the type specified
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public Attempt<IRuntimeCacheProvider> GetCache<T>()
{
IRuntimeCacheProvider cache;
if (_isolatedCache.TryGetValue(typeof(T), out cache))
{
return Attempt.Succeed(cache);
}
return Attempt<IRuntimeCacheProvider>.Fail();
}
/// <summary>
/// Clears all values inside this isolated runtime cache
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public void ClearCache<T>()
{
IRuntimeCacheProvider cache;
if (_isolatedCache.TryGetValue(typeof(T), out cache))
{
cache.ClearAllCache();
}
}
/// <summary>
/// Clears all of the isolated caches
/// </summary>
public void ClearAllCaches()
{
foreach (var key in _isolatedCache.Keys)
{
IRuntimeCacheProvider cache;
if (_isolatedCache.TryRemove(key, out cache))
{
cache.ClearAllCache();
}
}
}
}
}