using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Web; using System.Web.Caching; using Umbraco.Core.Cache; using Umbraco.Core.Logging; namespace Umbraco.Core { /// /// Class that is exposed by the ApplicationContext for application wide caching purposes /// public class CacheHelper { private readonly bool _enableCache; private readonly CacheProviderBase _staticCache; private readonly CacheProviderBase _nullStaticCache = new NullCacheProvider(); private readonly RuntimeCacheProviderBase _httpCache; private readonly RuntimeCacheProviderBase _nullHttpCache = new NullCacheProvider(); public CacheHelper(System.Web.Caching.Cache cache) : this(cache, true) { } internal CacheHelper(System.Web.Caching.Cache cache, bool enableCache) : this(new HttpRuntimeCacheProvider(cache), enableCache) { } internal CacheHelper(RuntimeCacheProviderBase httpCacheProvider, bool enableCache) : this(httpCacheProvider, new StaticCacheProvider(), enableCache) { } internal CacheHelper(RuntimeCacheProviderBase httpCacheProvider, CacheProviderBase staticCacheProvider, bool enableCache) { _httpCache = httpCacheProvider; _staticCache = staticCacheProvider; _enableCache = enableCache; } #region Static cache /// /// Clears the item in umbraco's static cache /// internal void ClearAllStaticCache() { if (!_enableCache) { _nullStaticCache.ClearAllCache(); } else { _staticCache.ClearAllCache(); } } /// /// Clears the item in umbraco's static cache with the given key /// /// Key internal void ClearStaticCacheItem(string key) { if (!_enableCache) { _nullStaticCache.ClearCacheItem(key); } else { _staticCache.ClearCacheItem(key); } } /// /// Clears all objects in the static cache with the System.Type name as the /// input parameter. (using [object].GetType()) /// /// The name of the System.Type which should be cleared from cache ex "System.Xml.XmlDocument" internal void ClearStaticCacheObjectTypes(string typeName) { if (!_enableCache) { _nullStaticCache.ClearCacheObjectTypes(typeName); } else { _staticCache.ClearCacheObjectTypes(typeName); } } /// /// Clears all objects in the static cache with the System.Type specified /// internal void ClearStaticCacheObjectTypes() { if (!_enableCache) { _nullStaticCache.ClearCacheObjectTypes(); } else { _staticCache.ClearCacheObjectTypes(); } } internal void ClearStaticCacheObjectTypes(Func predicate) { if (_enableCache) _staticCache.ClearCacheObjectTypes(predicate); else _nullStaticCache.ClearCacheObjectTypes(predicate); } /// /// Clears all static cache items that starts with the key passed. /// /// The start of the key internal void ClearStaticCacheByKeySearch(string keyStartsWith) { if (!_enableCache) { _nullStaticCache.ClearCacheByKeySearch(keyStartsWith); } else { _staticCache.ClearCacheByKeySearch(keyStartsWith); } } /// /// Clears all cache items that have a key that matches the regular expression /// /// internal void ClearStaticCacheByKeyExpression(string regexString) { if (!_enableCache) { _nullStaticCache.ClearCacheByKeyExpression(regexString); } else { _staticCache.ClearCacheByKeyExpression(regexString); } } internal IEnumerable GetStaticCacheItemsByKeySearch(string keyStartsWith) { if (!_enableCache) { return _nullStaticCache.GetCacheItemsByKeySearch(keyStartsWith); } else { return _staticCache.GetCacheItemsByKeySearch(keyStartsWith); } } /// /// Returns a static cache item by key, does not update the cache if it isn't there. /// /// /// /// internal TT GetStaticCacheItem(string cacheKey) { if (!_enableCache) { return _nullStaticCache.GetCacheItem(cacheKey); } else { return _staticCache.GetCacheItem(cacheKey); } } /// /// Gets (and adds if necessary) an item from the static cache with all of the default parameters /// /// /// /// /// internal TT GetStaticCacheItem(string cacheKey, Func getCacheItem) { if (!_enableCache) { return _nullStaticCache.GetCacheItem(cacheKey, getCacheItem); } else { return _staticCache.GetCacheItem(cacheKey, getCacheItem); } } #endregion #region Runtime/Http Cache /// /// Clears the item in umbraco's runtime cache /// public void ClearAllCache() { if (!_enableCache) { _nullHttpCache.ClearAllCache(); } else { _httpCache.ClearAllCache(); } } /// /// Clears the item in umbraco's runtime cache with the given key /// /// Key public void ClearCacheItem(string key) { if (!_enableCache) { _nullHttpCache.ClearCacheItem(key); } else { _httpCache.ClearCacheItem(key); } } /// /// Clears all objects in the System.Web.Cache with the System.Type name as the /// input parameter. (using [object].GetType()) /// /// The name of the System.Type which should be cleared from cache ex "System.Xml.XmlDocument" public void ClearCacheObjectTypes(string typeName) { if (!_enableCache) { _nullHttpCache.ClearCacheObjectTypes(typeName); } else { _httpCache.ClearCacheObjectTypes(typeName); } } /// /// Clears all objects in the System.Web.Cache with the System.Type specified /// public void ClearCacheObjectTypes() { if (!_enableCache) { _nullHttpCache.ClearCacheObjectTypes(); } else { _httpCache.ClearCacheObjectTypes(); } } /// /// Clears all cache items that starts with the key passed. /// /// The start of the key public void ClearCacheByKeySearch(string keyStartsWith) { if (!_enableCache) { _nullHttpCache.ClearCacheByKeySearch(keyStartsWith); } else { _httpCache.ClearCacheByKeySearch(keyStartsWith); } } /// /// Clears all cache items that have a key that matches the regular expression /// /// public void ClearCacheByKeyExpression(string regexString) { if (!_enableCache) { _nullHttpCache.ClearCacheByKeyExpression(regexString); } else { _httpCache.ClearCacheByKeyExpression(regexString); } } public IEnumerable GetCacheItemsByKeySearch(string keyStartsWith) { if (!_enableCache) { return _nullHttpCache.GetCacheItemsByKeySearch(keyStartsWith); } else { return _httpCache.GetCacheItemsByKeySearch(keyStartsWith); } } /// /// Returns a cache item by key, does not update the cache if it isn't there. /// /// /// /// public TT GetCacheItem(string cacheKey) { if (!_enableCache) { return _nullHttpCache.GetCacheItem(cacheKey); } else { return _httpCache.GetCacheItem(cacheKey); } } /// /// Gets (and adds if necessary) an item from the cache with all of the default parameters /// /// /// /// /// public TT GetCacheItem(string cacheKey, Func getCacheItem) { if (!_enableCache) { return _nullHttpCache.GetCacheItem(cacheKey, getCacheItem); } else { return _httpCache.GetCacheItem(cacheKey, getCacheItem); } } /// /// Gets (and adds if necessary) an item from the cache with the specified absolute expiration date (from NOW) /// /// /// /// This will set an absolute expiration from now until the timeout /// /// public TT GetCacheItem(string cacheKey, TimeSpan timeout, Func getCacheItem) { if (!_enableCache) { return _nullHttpCache.GetCacheItem(cacheKey, timeout, getCacheItem); } else { return _httpCache.GetCacheItem(cacheKey, timeout, getCacheItem); } } /// /// Gets (and adds if necessary) an item from the cache with the specified absolute expiration date (from NOW) /// /// /// /// /// This will set an absolute expiration from now until the timeout /// /// public TT GetCacheItem(string cacheKey, CacheItemRemovedCallback refreshAction, TimeSpan timeout, Func getCacheItem) { if (!_enableCache) { return _nullHttpCache.GetCacheItem(cacheKey, refreshAction, timeout, getCacheItem); } else { return _httpCache.GetCacheItem(cacheKey, refreshAction, timeout, getCacheItem); } } /// /// Gets (and adds if necessary) an item from the cache with the specified absolute expiration date (from NOW) /// /// /// /// /// /// This will set an absolute expiration from now until the timeout /// /// public TT GetCacheItem(string cacheKey, CacheItemPriority priority, CacheItemRemovedCallback refreshAction, TimeSpan timeout, Func getCacheItem) { if (!_enableCache) { return _nullHttpCache.GetCacheItem(cacheKey, priority, refreshAction, timeout, getCacheItem); } else { return _httpCache.GetCacheItem(cacheKey, priority, refreshAction, timeout, getCacheItem); } } /// /// Gets (and adds if necessary) an item from the cache with the specified absolute expiration date (from NOW) /// /// /// /// /// /// /// This will set an absolute expiration from now until the timeout /// /// public TT GetCacheItem(string cacheKey, CacheItemPriority priority, CacheItemRemovedCallback refreshAction, CacheDependency cacheDependency, TimeSpan timeout, Func getCacheItem) { if (!_enableCache) { return _nullHttpCache.GetCacheItem(cacheKey, priority, refreshAction, cacheDependency, timeout, getCacheItem); } else { return _httpCache.GetCacheItem(cacheKey, priority, refreshAction, cacheDependency, timeout, getCacheItem); } } /// /// Gets (and adds if necessary) an item from the cache /// /// /// /// /// /// /// public TT GetCacheItem(string cacheKey, CacheItemPriority priority, CacheDependency cacheDependency, Func getCacheItem) { if (!_enableCache) { return _nullHttpCache.GetCacheItem(cacheKey, priority, null, cacheDependency, null, getCacheItem); } else { return _httpCache.GetCacheItem(cacheKey, priority, null, cacheDependency, null, getCacheItem); } } /// /// Inserts an item into the cache, if it already exists in the cache it will be replaced /// /// /// /// /// public void InsertCacheItem(string cacheKey, CacheItemPriority priority, Func getCacheItem) { if (!_enableCache) { _nullHttpCache.InsertCacheItem(cacheKey, priority, getCacheItem); } else { _httpCache.InsertCacheItem(cacheKey, priority, getCacheItem); } } /// /// Inserts an item into the cache, if it already exists in the cache it will be replaced /// /// /// /// /// This will set an absolute expiration from now until the timeout /// public void InsertCacheItem(string cacheKey, CacheItemPriority priority, TimeSpan timeout, Func getCacheItem) { if (!_enableCache) { _nullHttpCache.InsertCacheItem(cacheKey, priority, timeout, getCacheItem); } else { _httpCache.InsertCacheItem(cacheKey, priority, timeout, getCacheItem); } } /// /// Inserts an item into the cache, if it already exists in the cache it will be replaced /// /// /// /// /// /// This will set an absolute expiration from now until the timeout /// public void InsertCacheItem(string cacheKey, CacheItemPriority priority, CacheDependency cacheDependency, TimeSpan timeout, Func getCacheItem) { if (!_enableCache) { _nullHttpCache.InsertCacheItem(cacheKey, priority, cacheDependency, timeout, getCacheItem); } else { _httpCache.InsertCacheItem(cacheKey, priority, cacheDependency, timeout, getCacheItem); } } /// /// Inserts an item into the cache, if it already exists in the cache it will be replaced /// /// /// /// /// /// /// This will set an absolute expiration from now until the timeout /// public void InsertCacheItem(string cacheKey, CacheItemPriority priority, CacheItemRemovedCallback refreshAction, CacheDependency cacheDependency, TimeSpan? timeout, Func getCacheItem) { if (!_enableCache) { _nullHttpCache.InsertCacheItem(cacheKey, priority, refreshAction, cacheDependency, timeout, getCacheItem); } else { _httpCache.InsertCacheItem(cacheKey, priority, refreshAction, cacheDependency, timeout, getCacheItem); } } #endregion } }