using System; using System.Web; namespace Umbraco.Core.Cache { /// /// Class that is exposed by the ApplicationContext for application wide caching purposes /// 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 CacheHelper(NullCacheProvider.Instance, NullCacheProvider.Instance, NullCacheProvider.Instance, new IsolatedRuntimeCache(_ => NullCacheProvider.Instance)); } /// /// 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) { if (httpCacheProvider == null) throw new ArgumentNullException("httpCacheProvider"); if (staticCacheProvider == null) throw new ArgumentNullException("staticCacheProvider"); if (requestCacheProvider == null) throw new ArgumentNullException("requestCacheProvider"); if (isolatedCacheManager == null) throw new ArgumentNullException("isolatedCacheManager"); RuntimeCache = httpCacheProvider; StaticCache = staticCacheProvider; RequestCache = requestCacheProvider; IsolatedRuntimeCache = 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; } } }