diff --git a/src/Umbraco.Core/Cache/HttpRequestCacheProvider.cs b/src/Umbraco.Core/Cache/HttpRequestCacheProvider.cs index 0a95ff6fd2..0ade5d55f4 100644 --- a/src/Umbraco.Core/Cache/HttpRequestCacheProvider.cs +++ b/src/Umbraco.Core/Cache/HttpRequestCacheProvider.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Web; namespace Umbraco.Core.Cache @@ -105,7 +106,7 @@ namespace Umbraco.Core.Cache if (result == null || GetSafeLazyValue(result, true) == null) // get non-created as NonCreatedValue & exceptions as null { - result = new Lazy(getCacheItem); + result = new Lazy(getCacheItem, LazyThreadSafetyMode.PublicationOnly); ContextItems[cacheKey] = result; } } diff --git a/src/Umbraco.Tests/Cache/CacheProviderTests.cs b/src/Umbraco.Tests/Cache/CacheProviderTests.cs index b0a8aec68c..8eab3b4bde 100644 --- a/src/Umbraco.Tests/Cache/CacheProviderTests.cs +++ b/src/Umbraco.Tests/Cache/CacheProviderTests.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using System.Web.UI; using NUnit.Framework; using Umbraco.Core.Cache; @@ -23,6 +24,59 @@ namespace Umbraco.Tests.Cache Provider.ClearAllCache(); } + [Test] + public void Does_Not_Cache_Exceptions() + { + var counter = 0; + + object result; + try + { + result = Provider.GetCacheItem("Blah", () => + { + counter++; + throw new Exception("Do not cache this"); + }); + } + catch (Exception){} + + try + { + result = Provider.GetCacheItem("Blah", () => + { + counter++; + throw new Exception("Do not cache this"); + }); + } + catch (Exception){} + + Assert.Greater(counter, 1); + + } + + [Test] + public void Ensures_Delegate_Result_Is_Cached_Once() + { + var counter = 0; + + object result; + + result = Provider.GetCacheItem("Blah", () => + { + counter++; + return ""; + }); + + result = Provider.GetCacheItem("Blah", () => + { + counter++; + return ""; + }); + + Assert.AreEqual(counter, 1); + + } + [Test] public void Can_Get_By_Search() {