From 72da68c08c190001727725f1f5a1b6917fbfe4d5 Mon Sep 17 00:00:00 2001 From: hartvig Date: Wed, 11 Jan 2012 11:17:56 -0100 Subject: [PATCH] Fixes error in cache patch --- umbraco/cms/businesslogic/ContentType.cs | 2 +- umbraco/cms/businesslogic/cache/Cache.cs | 237 ++++++++++++----------- 2 files changed, 123 insertions(+), 116 deletions(-) diff --git a/umbraco/cms/businesslogic/ContentType.cs b/umbraco/cms/businesslogic/ContentType.cs index 26a661d033..70532eed4c 100644 --- a/umbraco/cms/businesslogic/ContentType.cs +++ b/umbraco/cms/businesslogic/ContentType.cs @@ -790,7 +790,7 @@ namespace umbraco.cms.businesslogic public PropertyType getPropertyType(string alias) { // NH 22-08-08, Get from the property type stack to ensure support of master document types - object o = this.PropertyTypes.Find(delegate(PropertyType pt) { return pt.Alias == alias; }); + object o = this.PropertyTypes.Find(pt => pt.Alias == alias); //object o = SqlHelper.ExecuteScalar( // "Select id from cmsPropertyType where contentTypeId=@contentTypeId And Alias=@alias", diff --git a/umbraco/cms/businesslogic/cache/Cache.cs b/umbraco/cms/businesslogic/cache/Cache.cs index 9da2d64a07..30d4ff160a 100644 --- a/umbraco/cms/businesslogic/cache/Cache.cs +++ b/umbraco/cms/businesslogic/cache/Cache.cs @@ -4,75 +4,82 @@ using System.Web; namespace umbraco.cms.businesslogic.cache { - /// - /// Used to easily store and retreive items from the cache. - /// - public class Cache - { + /// + /// Used to easily store and retreive items from the cache. + /// + public class Cache + { + private static readonly object m_Locker = new object(); - /// - /// 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) - { + /// + /// 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) + { // NH 10 jan 2012 // Patch by the always wonderful Stéphane Gay to avoid cache null refs - var cache = HttpRuntime.Cache; - if (cache[key] != null) + lock (m_Locker) { - var context = HttpContext.Current; - if (context != null) - context.Trace.Warn("Cache", "Item " + key + " removed from cache"); + var cache = HttpRuntime.Cache; + if (cache[key] != null) + { + cache.Remove(key); + var context = HttpContext.Current; + if (context != null) + { + context.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(); + } + + + /// + /// 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()); + c.Remove(cacheEnumerator.Key.ToString()); } } - } - } - catch (Exception CacheE) - { - BusinessLogic.Log.Add(BusinessLogic.LogTypes.Error, BusinessLogic.User.GetUser(0), -1, "CacheClearing : " + CacheE.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. @@ -95,73 +102,73 @@ namespace umbraco.cms.businesslogic.cache } - /// - /// 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()); + /// + /// 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; - } + ((System.Collections.ArrayList)ht[c[cacheEnumerator.Key.ToString()].GetType().ToString()]).Add(cacheEnumerator.Key.ToString()); + } + } + return ht; + } - public delegate TT GetCacheItemDelegate(); + 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, + 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, + 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, 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(); + 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; - } - } + } + } + } + return (TT)result; + } + } }