Files
Umbraco-CMS/src/Umbraco.Core/Cache/CacheHelper.cs

90 lines
3.3 KiB
C#
Raw Normal View History

using System;
using System.Web;
namespace Umbraco.Core.Cache
{
2017-07-20 11:21:28 +02:00
/// <summary>
/// Represents the application-wide caches.
2017-07-20 11:21:28 +02:00
/// </summary>
public class CacheHelper
2017-07-20 11:21:28 +02:00
{
public static CacheHelper NoCache { get; } = new CacheHelper(NullCacheProvider.Instance, NullCacheProvider.Instance, NullCacheProvider.Instance, new IsolatedRuntimeCache(_ => NullCacheProvider.Instance));
2017-05-12 14:49:44 +02:00
2017-07-20 11:21:28 +02:00
/// <summary>
/// Creates a cache helper with disabled caches
/// </summary>
/// <returns></returns>
/// <remarks>
/// Good for unit testing
/// </remarks>
public static CacheHelper CreateDisabledCacheHelper()
{
2017-05-12 14:49:44 +02:00
// do *not* return NoCache
// NoCache is a special instance that is detected by RepositoryBase and disables all cache policies
// CreateDisabledCacheHelper is used in tests to use no cache, *but* keep all cache policies
return new DisabledCacheHelper();
}
2017-07-20 11:21:28 +02:00
/// <summary>
/// Initializes a new instance for use in the web
/// </summary>
public CacheHelper()
: this(
new HttpRuntimeCacheProvider(HttpRuntime.Cache),
new StaticCacheProvider(),
new HttpRequestCacheProvider(),
new IsolatedRuntimeCache(t => new ObjectCacheRuntimeCacheProvider()))
2017-07-20 11:21:28 +02:00
{
}
/// <summary>
/// Initializes a new instance for use in the web
/// </summary>
public CacheHelper(System.Web.Caching.Cache cache)
: this(
new HttpRuntimeCacheProvider(cache),
new StaticCacheProvider(),
new HttpRequestCacheProvider(),
new IsolatedRuntimeCache(t => new ObjectCacheRuntimeCacheProvider()))
2017-07-20 11:21:28 +02:00
{
}
2017-07-20 11:21:28 +02:00
/// <summary>
/// Initializes a new instance based on the provided providers
/// </summary>
public CacheHelper(
IRuntimeCacheProvider httpCacheProvider,
ICacheProvider staticCacheProvider,
ICacheProvider requestCacheProvider,
2017-07-20 11:21:28 +02:00
IsolatedRuntimeCache isolatedCacheManager)
{
RuntimeCache = httpCacheProvider ?? throw new ArgumentNullException(nameof(httpCacheProvider));
StaticCache = staticCacheProvider ?? throw new ArgumentNullException(nameof(staticCacheProvider));
RequestCache = requestCacheProvider ?? throw new ArgumentNullException(nameof(requestCacheProvider));
IsolatedRuntimeCache = isolatedCacheManager ?? throw new ArgumentNullException(nameof(isolatedCacheManager));
}
/// <summary>
/// Returns the current Request cache
/// </summary>
public ICacheProvider RequestCache { get; internal set; }
2017-07-20 11:21:28 +02:00
/// <summary>
/// Returns the current Runtime cache
/// </summary>
public ICacheProvider StaticCache { get; internal set; }
/// <summary>
/// Returns the current Runtime cache
/// </summary>
2017-07-20 11:21:28 +02:00
public IRuntimeCacheProvider RuntimeCache { get; internal set; }
/// <summary>
/// Returns the current Isolated Runtime cache manager
/// </summary>
public IsolatedRuntimeCache IsolatedRuntimeCache { get; internal set; }
}
}