using System; using System.Web.Caching; using System.Web; namespace umbraco.cms.businesslogic.cache { /// /// Used to easily store and retreive items from the cache. /// public class Cache { /// /// Clears everything in umbraco's runtime cache, which means that not only /// umbraco content is removed, but also other cache items from pages running in /// the same application / website. Use with care :-) /// public static void ClearAllCache() { System.Web.Caching.Cache c = System.Web.HttpRuntime.Cache; if (c != null) { System.Collections.IDictionaryEnumerator cacheEnumerator = c.GetEnumerator(); while ( cacheEnumerator.MoveNext() ) { c.Remove(cacheEnumerator.Key.ToString()); } } } /// /// Clears the item in umbraco's runtime cache with the given key /// /// Key public static void ClearCacheItem(string Key) { if (System.Web.HttpRuntime.Cache[Key] != null) { HttpRuntime.Cache.Remove(Key); HttpContext.Current.Trace.Warn("Cache", "Item " + Key + " removed from cache"); } } /// /// 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 static void ClearCacheObjectTypes(string TypeName) { System.Web.Caching.Cache c = System.Web.HttpRuntime.Cache; try { if (c != null) { System.Collections.IDictionaryEnumerator cacheEnumerator = c.GetEnumerator(); while (cacheEnumerator.MoveNext()) { if (cacheEnumerator.Key != null && c[cacheEnumerator.Key.ToString()] != null && c[cacheEnumerator.Key.ToString()].GetType() != null && c[cacheEnumerator.Key.ToString()].GetType().ToString() == TypeName) { c.Remove(cacheEnumerator.Key.ToString()); } } } } catch (Exception CacheE) { BusinessLogic.Log.Add(BusinessLogic.LogTypes.Error, BusinessLogic.User.GetUser(0), -1, "CacheClearing : " + CacheE.ToString()); } } /// /// Clears all cache items that starts with the key passed. /// /// The start of the key public static void ClearCacheByKeySearch(string KeyStartsWith) { System.Web.Caching.Cache c = System.Web.HttpRuntime.Cache; if (c != null) { System.Collections.IDictionaryEnumerator cacheEnumerator = c.GetEnumerator(); while (cacheEnumerator.MoveNext()) { if (cacheEnumerator.Key is string && ((string)cacheEnumerator.Key).StartsWith(KeyStartsWith)) { Cache.ClearCacheItem((string)cacheEnumerator.Key); } } } } /// /// Retrieve all cached items /// /// A hastable containing all cacheitems public static System.Collections.Hashtable ReturnCacheItemsOrdred() { System.Collections.Hashtable ht = new System.Collections.Hashtable(); System.Web.Caching.Cache c = System.Web.HttpRuntime.Cache; if (c != null) { System.Collections.IDictionaryEnumerator cacheEnumerator = c.GetEnumerator(); while ( cacheEnumerator.MoveNext() ) { if (ht[c[cacheEnumerator.Key.ToString()].GetType().ToString()] == null) ht.Add(c[cacheEnumerator.Key.ToString()].GetType().ToString(), new System.Collections.ArrayList()); ((System.Collections.ArrayList) ht[c[cacheEnumerator.Key.ToString()].GetType().ToString()]).Add(cacheEnumerator.Key.ToString()); } } return ht; } public delegate TT GetCacheItemDelegate(); public static TT GetCacheItem(string cacheKey, object syncLock, TimeSpan timeout, GetCacheItemDelegate getCacheItem) { return GetCacheItem(cacheKey, syncLock, null, timeout, getCacheItem); } public static TT GetCacheItem(string cacheKey, object syncLock, CacheItemRemovedCallback refreshAction, TimeSpan timeout, GetCacheItemDelegate getCacheItem) { return GetCacheItem(cacheKey, syncLock, CacheItemPriority.Normal, refreshAction, timeout, getCacheItem); } public static TT GetCacheItem(string cacheKey, object syncLock, CacheItemPriority priority, CacheItemRemovedCallback refreshAction, TimeSpan timeout, GetCacheItemDelegate getCacheItem) { return GetCacheItem(cacheKey, syncLock, priority, refreshAction, null, timeout, getCacheItem); } public static TT GetCacheItem(string cacheKey, object syncLock, CacheItemPriority priority, CacheItemRemovedCallback refreshAction, CacheDependency cacheDependency, TimeSpan timeout, GetCacheItemDelegate getCacheItem) { object result = System.Web.HttpRuntime.Cache.Get(cacheKey); if (result == null) { lock (syncLock) { result = System.Web.HttpRuntime.Cache.Get(cacheKey); if (result == null) { result = getCacheItem(); if (result != null) { System.Web.HttpRuntime.Cache.Add(cacheKey, result, cacheDependency, DateTime.Now.Add(timeout), TimeSpan.Zero, priority, refreshAction); } } } } return (TT)result; } } }