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 /// /// /// This class may be opened publicly at some point but needs a review of what is absoletely necessary. /// public class CacheHelper //: CacheProviderBase { private readonly bool _enableCache; private readonly HttpRuntimeCacheProvider _httpCache; private readonly NullCacheProvider _nullCache = new NullCacheProvider(); public CacheHelper(System.Web.Caching.Cache cache) : this(cache, true) { } internal CacheHelper(System.Web.Caching.Cache cache, bool enableCache) { _httpCache = new HttpRuntimeCacheProvider(cache); _enableCache = enableCache; } public void ClearAllCache() { if (!_enableCache) { _nullCache.ClearAllCache(); } else { _httpCache.ClearAllCache(); } } /// /// Clears the item in umbraco's runtime cache with the given key /// /// Key public void ClearCacheItem(string key) { if (!_enableCache) { _nullCache.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) { _nullCache.ClearCacheObjectTypes(typeName); } else { _httpCache.ClearCacheObjectTypes(typeName); } } /// /// Clears all objects in the System.Web.Cache with the System.Type specified /// public void ClearCacheObjectTypes() { if (!_enableCache) { _nullCache.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) { _nullCache.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) { _nullCache.ClearCacheByKeyExpression(regexString); } else { _httpCache.ClearCacheByKeyExpression(regexString); } } public IEnumerable GetCacheItemsByKeySearch(string keyStartsWith) { if (!_enableCache) { return _nullCache.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 _nullCache.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 _nullCache.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 _nullCache.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 _nullCache.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 _nullCache.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 _nullCache.GetCacheItem(cacheKey, priority, refreshAction, cacheDependency, timeout, getCacheItem); } else { return _httpCache.GetCacheItem(cacheKey, priority, refreshAction, cacheDependency, timeout, 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) { _nullCache.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) { _nullCache.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) { _nullCache.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) { _nullCache.InsertCacheItem(cacheKey, priority, refreshAction, cacheDependency, timeout, getCacheItem); } else { _httpCache.InsertCacheItem(cacheKey, priority, refreshAction, cacheDependency, timeout, getCacheItem); } } } }