using System; using System.Web; namespace Umbraco.Core.Cache { /// /// Represents the application-wide caches. /// public class CacheHelper { public static CacheHelper NoCache { get; } = new CacheHelper(NullCacheProvider.Instance, NullCacheProvider.Instance, NullCacheProvider.Instance, new IsolatedRuntimeCache(_ => NullCacheProvider.Instance)); /// /// Creates a cache helper with disabled caches /// /// /// /// Good for unit testing /// public static CacheHelper CreateDisabledCacheHelper() { // 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(); } /// /// Initializes a new instance for use in the web /// public CacheHelper() : this( new HttpRuntimeCacheProvider(HttpRuntime.Cache), new StaticCacheProvider(), new HttpRequestCacheProvider(), new IsolatedRuntimeCache(t => new ObjectCacheRuntimeCacheProvider())) { } /// /// Initializes a new instance for use in the web /// public CacheHelper(System.Web.Caching.Cache cache) : this( new HttpRuntimeCacheProvider(cache), new StaticCacheProvider(), new HttpRequestCacheProvider(), new IsolatedRuntimeCache(t => new ObjectCacheRuntimeCacheProvider())) { } /// /// Initializes a new instance based on the provided providers /// public CacheHelper( IRuntimeCacheProvider httpCacheProvider, ICacheProvider staticCacheProvider, ICacheProvider requestCacheProvider, 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)); } /// /// Returns the current Request cache /// public ICacheProvider RequestCache { get; internal set; } /// /// Returns the current Runtime cache /// public ICacheProvider StaticCache { get; internal set; } /// /// Returns the current Runtime cache /// public IRuntimeCacheProvider RuntimeCache { get; internal set; } /// /// Returns the current Isolated Runtime cache manager /// public IsolatedRuntimeCache IsolatedRuntimeCache { get; internal set; } } }