From ce0c72d9f8e34f09b2a45ef796ff47fd689f87e6 Mon Sep 17 00:00:00 2001 From: Stephan Date: Tue, 19 Mar 2013 17:51:55 -0100 Subject: [PATCH] Web.PublishedCache - introduce contextual caches --- .../PublishedContentCacheTests.cs | 11 ++- .../PublishedMediaCacheTests.cs | 5 +- .../DynamicPublishedContentTests.cs | 5 +- .../PublishedContent/PublishedContentTests.cs | 5 +- .../PublishedContent/PublishedMediaTests.cs | 63 ++++++------ .../StronglyTypedQueryTests.cs | 5 +- .../Routing/RenderRouteHandlerTests.cs | 4 +- src/Umbraco.Web/ExamineExtensions.cs | 6 +- .../Mvc/RedirectToUmbracoPageResult.cs | 2 +- .../ContextualPublishedCache.cs | 50 ++++++++++ .../ContextualPublishedContentCache.cs | 90 +++++++++++++++++ .../ContextualPublishedMediaCache.cs | 68 +++++++++++++ .../PublishedCache/IPublishedCache.cs | 42 ++++++-- .../PublishedCache/IPublishedContentCache.cs | 14 +-- .../LegacyXmlCache/PublishedContentCache.cs | 91 ++++++++++++++---- .../LegacyXmlCache/PublishedMediaCache.cs | 5 +- src/Umbraco.Web/PublishedContentExtensions.cs | 4 +- src/Umbraco.Web/Routing/AliasUrlProvider.cs | 2 +- .../Routing/ContentFinderByIdPath.cs | 4 +- .../Routing/ContentFinderByLegacy404.cs | 4 +- .../Routing/ContentFinderByNiceUrl.cs | 8 +- .../Routing/ContentFinderByNotFoundHandler.cs | 4 +- .../ContentFinderByNotFoundHandlers.cs | 4 +- .../Routing/ContentFinderByPageIdQuery.cs | 4 +- .../Routing/ContentFinderByUrlAlias.cs | 1 - src/Umbraco.Web/Routing/DefaultUrlProvider.cs | 6 +- .../Routing/PublishedContentRequestEngine.cs | 6 +- src/Umbraco.Web/Templates/TemplateRenderer.cs | 4 +- src/Umbraco.Web/Umbraco.Web.csproj | 3 + src/Umbraco.Web/UmbracoContext.cs | 8 +- src/Umbraco.Web/UmbracoHelper.cs | 96 +++++++++---------- src/Umbraco.Web/UmbracoModule.cs | 2 +- .../umbraco.presentation/library.cs | 4 +- .../umbraco/templateControls/ItemRenderer.cs | 4 +- 34 files changed, 450 insertions(+), 184 deletions(-) create mode 100644 src/Umbraco.Web/PublishedCache/ContextualPublishedCache.cs create mode 100644 src/Umbraco.Web/PublishedCache/ContextualPublishedContentCache.cs create mode 100644 src/Umbraco.Web/PublishedCache/ContextualPublishedMediaCache.cs diff --git a/src/Umbraco.Tests/PublishedCache/PublishedContentCacheTests.cs b/src/Umbraco.Tests/PublishedCache/PublishedContentCacheTests.cs index c0e8c75a9b..4da1a34359 100644 --- a/src/Umbraco.Tests/PublishedCache/PublishedContentCacheTests.cs +++ b/src/Umbraco.Tests/PublishedCache/PublishedContentCacheTests.cs @@ -5,6 +5,7 @@ using Umbraco.Core; using Umbraco.Core.Configuration; using Umbraco.Tests.TestHelpers; using Umbraco.Web; +using Umbraco.Web.PublishedCache; using Umbraco.Web.PublishedCache.LegacyXmlCache; using Umbraco.Web.Routing; using umbraco.BusinessLogic; @@ -16,7 +17,7 @@ namespace Umbraco.Tests.PublishedCache { private FakeHttpContextFactory _httpContextFactory; private UmbracoContext _umbracoContext; - private PublishedContentCache _cache; + private ContextualPublishedContentCache _cache; private string GetLegacyXml() { @@ -89,7 +90,7 @@ namespace Umbraco.Tests.PublishedCache return xDoc; }; - _cache = new PublishedContentCache(); + _cache = new ContextualPublishedContentCache(new PublishedContentCache(), _umbracoContext); } private void SetupForLegacy() @@ -135,7 +136,7 @@ namespace Umbraco.Tests.PublishedCache [Test] public void Get_Root_Docs() { - var result = _cache.GetAtRoot(_umbracoContext); + var result = _cache.GetAtRoot(); Assert.AreEqual(2, result.Count()); Assert.AreEqual(1046, result.ElementAt(0).Id); Assert.AreEqual(1172, result.ElementAt(1).Id); @@ -162,7 +163,7 @@ namespace Umbraco.Tests.PublishedCache [TestCase("/home/Sub'Apostrophe", 1177)] public void Get_Node_By_Route(string route, int nodeId) { - var result = _cache.GetByRoute(_umbracoContext, route, false); + var result = _cache.GetByRoute(route, false); Assert.IsNotNull(result); Assert.AreEqual(nodeId, result.Id); } @@ -181,7 +182,7 @@ namespace Umbraco.Tests.PublishedCache [TestCase("/Sub1", 1173)] public void Get_Node_By_Route_Hiding_Top_Level_Nodes(string route, int nodeId) { - var result = _cache.GetByRoute(_umbracoContext, route, true); + var result = _cache.GetByRoute(route, true); Assert.IsNotNull(result); Assert.AreEqual(nodeId, result.Id); } diff --git a/src/Umbraco.Tests/PublishedCache/PublishedMediaCacheTests.cs b/src/Umbraco.Tests/PublishedCache/PublishedMediaCacheTests.cs index f99747a02b..5fc9d7659a 100644 --- a/src/Umbraco.Tests/PublishedCache/PublishedMediaCacheTests.cs +++ b/src/Umbraco.Tests/PublishedCache/PublishedMediaCacheTests.cs @@ -9,6 +9,7 @@ using Umbraco.Core.Models; using Umbraco.Tests.PublishedContent; using Umbraco.Tests.TestHelpers; using Umbraco.Web; +using Umbraco.Web.PublishedCache; using Umbraco.Web.PublishedCache.LegacyXmlCache; using umbraco.BusinessLogic; @@ -41,8 +42,8 @@ namespace Umbraco.Tests.PublishedCache var mChild2 = global::umbraco.cms.businesslogic.media.Media.MakeNew("Child2", mType, user, mRoot2.Id); var ctx = GetUmbracoContext("/test", 1234); - var mediaStore = new PublishedMediaCache(); - var roots = mediaStore.GetAtRoot(ctx); + var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(), ctx); + var roots = cache.GetAtRoot(); Assert.AreEqual(2, roots.Count()); Assert.IsTrue(roots.Select(x => x.Id).ContainsAll(new[] {mRoot1.Id, mRoot2.Id})); diff --git a/src/Umbraco.Tests/PublishedContent/DynamicPublishedContentTests.cs b/src/Umbraco.Tests/PublishedContent/DynamicPublishedContentTests.cs index fb39ed947b..6910ca0eb0 100644 --- a/src/Umbraco.Tests/PublishedContent/DynamicPublishedContentTests.cs +++ b/src/Umbraco.Tests/PublishedContent/DynamicPublishedContentTests.cs @@ -4,6 +4,7 @@ using Umbraco.Core; using Umbraco.Core.Models; using Umbraco.Web; using Umbraco.Web.Models; +using Umbraco.Web.PublishedCache; using Umbraco.Web.PublishedCache.LegacyXmlCache; namespace Umbraco.Tests.PublishedContent @@ -27,8 +28,8 @@ namespace Umbraco.Tests.PublishedContent //var template = Template.MakeNew("test", new User(0)); //var ctx = GetUmbracoContext("/test", template.Id); var ctx = GetUmbracoContext("/test", 1234); - var contentStore = new PublishedContentCache(); - var doc = contentStore.GetById(ctx, id); + var cache = new ContextualPublishedContentCache(new PublishedContentCache(), ctx); + var doc = cache.GetById(id); Assert.IsNotNull(doc); var dynamicNode = new DynamicPublishedContent(doc); Assert.IsNotNull(dynamicNode); diff --git a/src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs b/src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs index 7153a5225f..6f7be801ce 100644 --- a/src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs +++ b/src/Umbraco.Tests/PublishedContent/PublishedContentTests.cs @@ -8,6 +8,7 @@ using Umbraco.Core.Models; using Umbraco.Core.PropertyEditors; using Umbraco.Tests.TestHelpers; using Umbraco.Web; +using Umbraco.Web.PublishedCache; using Umbraco.Web.PublishedCache.LegacyXmlCache; namespace Umbraco.Tests.PublishedContent @@ -82,8 +83,8 @@ namespace Umbraco.Tests.PublishedContent internal IPublishedContent GetNode(int id) { var ctx = GetUmbracoContext("/test", 1234); - var cache = new PublishedContentCache(); - var doc = cache.GetById(ctx, id); + var cache = new ContextualPublishedContentCache(new PublishedContentCache(), ctx); + var doc = cache.GetById(id); Assert.IsNotNull(doc); return doc; } diff --git a/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs b/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs index 461eb77b20..129149deef 100644 --- a/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs +++ b/src/Umbraco.Tests/PublishedContent/PublishedMediaTests.cs @@ -18,6 +18,7 @@ using Umbraco.Core.PropertyEditors; using Umbraco.Tests.TestHelpers; using Umbraco.Tests.UmbracoExamine; using Umbraco.Web; +using Umbraco.Web.PublishedCache; using Umbraco.Web.PublishedCache.LegacyXmlCache; using UmbracoExamine; using UmbracoExamine.DataServices; @@ -59,8 +60,8 @@ namespace Umbraco.Tests.PublishedContent internal static IPublishedContent GetNode(int id, UmbracoContext umbracoContext) { var ctx = umbracoContext; - var mediaStore = new PublishedMediaCache(); - var doc = mediaStore.GetById(ctx, id); + var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(), ctx); + var doc = cache.GetById(id); Assert.IsNotNull(doc); return doc; } @@ -78,12 +79,11 @@ namespace Umbraco.Tests.PublishedContent var indexer = IndexInitializer.GetUmbracoIndexer(luceneDir); indexer.RebuildIndex(); var searcher = IndexInitializer.GetUmbracoSearcher(luceneDir); - var cache = new PublishedMediaCache( - searcher, - indexer); + var ctx = GetUmbracoContext("/test", 1234); + var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(searcher, indexer), ctx); //we are using the media.xml media to test the examine results implementation, see the media.xml file in the ExamineHelpers namespace - var publishedMedia = cache.GetById(GetUmbracoContext("/test", 1234), 1111); + var publishedMedia = cache.GetById(1111); var rootChildren = publishedMedia.Children().ToArray(); var currSort = 0; for (var i = 0; i < rootChildren.Count(); i++) @@ -108,11 +108,11 @@ namespace Umbraco.Tests.PublishedContent var indexer = IndexInitializer.GetUmbracoIndexer(luceneDir); indexer.RebuildIndex(); var searcher = IndexInitializer.GetUmbracoSearcher(luceneDir); - var cache = new PublishedMediaCache(searcher, indexer); var ctx = GetUmbracoContext("/test", 1234); + var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(searcher, indexer), ctx); //ensure it is found - var publishedMedia = cache.GetById(ctx, 3113); + var publishedMedia = cache.GetById(3113); Assert.IsNotNull(publishedMedia); //move item to recycle bin @@ -133,7 +133,7 @@ namespace Umbraco.Tests.PublishedContent Assert.AreEqual(1, found.TotalItemCount); //ensure it does not show up in the published media store - var recycledMedia = cache.GetById(ctx, 3113); + var recycledMedia = cache.GetById(3113); Assert.IsNull(recycledMedia); } @@ -148,16 +148,15 @@ namespace Umbraco.Tests.PublishedContent var indexer = IndexInitializer.GetUmbracoIndexer(luceneDir); indexer.RebuildIndex(); var searcher = IndexInitializer.GetUmbracoSearcher(luceneDir); - var cache = new PublishedMediaCache( - searcher, - indexer); + var ctx = GetUmbracoContext("/test", 1234); + var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(searcher, indexer), ctx); //we are using the media.xml media to test the examine results implementation, see the media.xml file in the ExamineHelpers namespace - var publishedMedia = cache.GetById(GetUmbracoContext("/test", 1234), 1111); + var publishedMedia = cache.GetById(1111); var rootChildren = publishedMedia.Children(); Assert.IsTrue(rootChildren.Select(x => x.Id).ContainsAll(new[] { 2222, 1113, 1114, 1115, 1116 })); - var publishedChild1 = cache.GetById(GetUmbracoContext("/test", 1234), 2222); + var publishedChild1 = cache.GetById(2222); var subChildren = publishedChild1.Children(); Assert.IsTrue(subChildren.Select(x => x.Id).ContainsAll(new[] { 2112 })); } @@ -171,16 +170,15 @@ namespace Umbraco.Tests.PublishedContent var indexer = IndexInitializer.GetUmbracoIndexer(luceneDir); indexer.RebuildIndex(); var searcher = IndexInitializer.GetUmbracoSearcher(luceneDir); - var cache = new PublishedMediaCache( - searcher, - indexer); + var ctx = GetUmbracoContext("/test", 1234); + var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(searcher, indexer), ctx); //we are using the media.xml media to test the examine results implementation, see the media.xml file in the ExamineHelpers namespace - var publishedMedia = cache.GetById(GetUmbracoContext("/test", 1234), 1111); + var publishedMedia = cache.GetById(1111); var rootDescendants = publishedMedia.Descendants(); Assert.IsTrue(rootDescendants.Select(x => x.Id).ContainsAll(new[] { 2112, 2222, 1113, 1114, 1115, 1116 })); - var publishedChild1 = cache.GetById(GetUmbracoContext("/test", 1234), 2222); + var publishedChild1 = cache.GetById(2222); var subDescendants = publishedChild1.Descendants(); Assert.IsTrue(subDescendants.Select(x => x.Id).ContainsAll(new[] { 2112, 3113 })); } @@ -194,16 +192,15 @@ namespace Umbraco.Tests.PublishedContent var indexer = IndexInitializer.GetUmbracoIndexer(luceneDir); indexer.RebuildIndex(); var searcher = IndexInitializer.GetUmbracoSearcher(luceneDir); - var cache = new PublishedMediaCache( - searcher, - indexer); + var ctx = GetUmbracoContext("/test", 1234); + var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(searcher, indexer), ctx); //we are using the media.xml media to test the examine results implementation, see the media.xml file in the ExamineHelpers namespace - var publishedMedia = cache.GetById(GetUmbracoContext("/test", 1234), 1111); + var publishedMedia = cache.GetById(1111); var rootDescendants = publishedMedia.DescendantsOrSelf(); Assert.IsTrue(rootDescendants.Select(x => x.Id).ContainsAll(new[] { 1111, 2112, 2222, 1113, 1114, 1115, 1116 })); - var publishedChild1 = cache.GetById(GetUmbracoContext("/test", 1234), 2222); + var publishedChild1 = cache.GetById(2222); var subDescendants = publishedChild1.DescendantsOrSelf(); Assert.IsTrue(subDescendants.Select(x => x.Id).ContainsAll(new[] { 2222, 2112, 3113 })); } @@ -216,13 +213,12 @@ namespace Umbraco.Tests.PublishedContent { var indexer = IndexInitializer.GetUmbracoIndexer(luceneDir); indexer.RebuildIndex(); - - var cache = new PublishedMediaCache( - IndexInitializer.GetUmbracoSearcher(luceneDir), - indexer); + var ctx = GetUmbracoContext("/test", 1234); + var searcher = IndexInitializer.GetUmbracoSearcher(luceneDir); + var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(searcher, indexer), ctx); //we are using the media.xml media to test the examine results implementation, see the media.xml file in the ExamineHelpers namespace - var publishedMedia = cache.GetById(GetUmbracoContext("/test", 1234), 3113); + var publishedMedia = cache.GetById(3113); var ancestors = publishedMedia.Ancestors(); Assert.IsTrue(ancestors.Select(x => x.Id).ContainsAll(new[] { 2112, 2222, 1111 })); } @@ -236,13 +232,12 @@ namespace Umbraco.Tests.PublishedContent { var indexer = IndexInitializer.GetUmbracoIndexer(luceneDir); indexer.RebuildIndex(); - - var cache = new PublishedMediaCache( - IndexInitializer.GetUmbracoSearcher(luceneDir), - indexer); + var ctx = GetUmbracoContext("/test", 1234); + var searcher = IndexInitializer.GetUmbracoSearcher(luceneDir); + var cache = new ContextualPublishedMediaCache(new PublishedMediaCache(searcher, indexer), ctx); //we are using the media.xml media to test the examine results implementation, see the media.xml file in the ExamineHelpers namespace - var publishedMedia = cache.GetById(GetUmbracoContext("/test", 1234), 3113); + var publishedMedia = cache.GetById(3113); var ancestors = publishedMedia.AncestorsOrSelf(); Assert.IsTrue(ancestors.Select(x => x.Id).ContainsAll(new[] { 3113, 2112, 2222, 1111 })); } diff --git a/src/Umbraco.Tests/PublishedContent/StronglyTypedQueryTests.cs b/src/Umbraco.Tests/PublishedContent/StronglyTypedQueryTests.cs index 096c31d450..66f8737a76 100644 --- a/src/Umbraco.Tests/PublishedContent/StronglyTypedQueryTests.cs +++ b/src/Umbraco.Tests/PublishedContent/StronglyTypedQueryTests.cs @@ -8,6 +8,7 @@ using Umbraco.Core.PropertyEditors; using Umbraco.Tests.TestHelpers; using Umbraco.Web; using Umbraco.Web.Models; +using Umbraco.Web.PublishedCache; using Umbraco.Web.PublishedCache.LegacyXmlCache; namespace Umbraco.Tests.PublishedContent @@ -75,8 +76,8 @@ namespace Umbraco.Tests.PublishedContent internal IPublishedContent GetNode(int id) { var ctx = UmbracoContext.Current; - var contentStore = new PublishedContentCache(); - var doc = contentStore.GetById(ctx, id); + var cache = new ContextualPublishedContentCache(new PublishedContentCache(), ctx); + var doc = cache.GetById(id); Assert.IsNotNull(doc); return doc; } diff --git a/src/Umbraco.Tests/Routing/RenderRouteHandlerTests.cs b/src/Umbraco.Tests/Routing/RenderRouteHandlerTests.cs index 4c2bca139b..5e09893485 100644 --- a/src/Umbraco.Tests/Routing/RenderRouteHandlerTests.cs +++ b/src/Umbraco.Tests/Routing/RenderRouteHandlerTests.cs @@ -76,7 +76,7 @@ namespace Umbraco.Tests.Routing var routingContext = GetRoutingContext("~/dummy-page", template.Id, routeData); var docRequest = new PublishedContentRequest(routingContext.UmbracoContext.CleanedUmbracoUrl, routingContext) { - PublishedContent = routingContext.UmbracoContext.ContentCache.GetById(routingContext.UmbracoContext, 1174), + PublishedContent = routingContext.UmbracoContext.ContentCache.GetById(1174), TemplateModel = template }; @@ -112,7 +112,7 @@ namespace Umbraco.Tests.Routing var routingContext = GetRoutingContext("~/dummy-page", template.Id, routeData, true); var docRequest = new PublishedContentRequest(routingContext.UmbracoContext.CleanedUmbracoUrl, routingContext) { - PublishedContent = routingContext.UmbracoContext.ContentCache.GetById(routingContext.UmbracoContext, 1172), + PublishedContent = routingContext.UmbracoContext.ContentCache.GetById(1172), TemplateModel = template }; diff --git a/src/Umbraco.Web/ExamineExtensions.cs b/src/Umbraco.Web/ExamineExtensions.cs index 5bc305e3b4..e24cf1c9b0 100644 --- a/src/Umbraco.Web/ExamineExtensions.cs +++ b/src/Umbraco.Web/ExamineExtensions.cs @@ -17,7 +17,7 @@ namespace Umbraco.Web { internal static IEnumerable ConvertSearchResultToPublishedContent( this IEnumerable results, - IPublishedCache contentCache) + ContextualPublishedCache cache) { //TODO: The search result has already returned a result which SHOULD include all of the data to create an IPublishedContent, // however thsi is currently not the case: @@ -27,9 +27,7 @@ namespace Umbraco.Web foreach (var result in results.OrderByDescending(x => x.Score)) { - var doc = contentCache.GetById( - UmbracoContext.Current, - result.Id); + var doc = cache.GetById(result.Id); if (doc == null) continue; //skip if this doesn't exist in the cache doc.Properties.Add( new PropertyResult("examineScore", result.Score.ToString(), Guid.Empty, PropertyResultType.CustomProperty)); diff --git a/src/Umbraco.Web/Mvc/RedirectToUmbracoPageResult.cs b/src/Umbraco.Web/Mvc/RedirectToUmbracoPageResult.cs index d41dd8d85f..917191f03f 100644 --- a/src/Umbraco.Web/Mvc/RedirectToUmbracoPageResult.cs +++ b/src/Umbraco.Web/Mvc/RedirectToUmbracoPageResult.cs @@ -46,7 +46,7 @@ namespace Umbraco.Web.Mvc if (_publishedContent != null) return _publishedContent; //need to get the URL for the page - _publishedContent = PublishedContentCacheResolver.Current.PublishedContentCache.GetById(_umbracoContext, _pageId); + _publishedContent = UmbracoContext.Current.ContentCache.GetById(_pageId); return _publishedContent; } diff --git a/src/Umbraco.Web/PublishedCache/ContextualPublishedCache.cs b/src/Umbraco.Web/PublishedCache/ContextualPublishedCache.cs new file mode 100644 index 0000000000..9b15b975da --- /dev/null +++ b/src/Umbraco.Web/PublishedCache/ContextualPublishedCache.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Umbraco.Core.Models; + +namespace Umbraco.Web.PublishedCache +{ + /// + /// Provides access to cached contents in a specified context. + /// + internal abstract class ContextualPublishedCache + { + protected readonly UmbracoContext UmbracoContext; + + protected ContextualPublishedCache(UmbracoContext umbracoContext) + { + UmbracoContext = umbracoContext; + } + + /// + /// Gets a content identified by its unique identifier. + /// + /// The content unique identifier. + /// The content, or null. + public abstract IPublishedContent GetById(int contentId); + + /// + /// Gets contents at root. + /// + /// The contents. + public abstract IEnumerable GetAtRoot(); + + /// + /// Gets a content resulting from an XPath query. + /// + /// The XPath query. + /// Optional XPath variables. + /// The content, or null. + public abstract IPublishedContent GetSingleByXPath(string xpath, Core.Xml.XPathVariable[] vars); + + /// + /// Gets contents resulting from an XPath query. + /// + /// The XPath query. + /// Optional XPath variables. + /// The contents. + public abstract IEnumerable GetByXPath(string xpath, Core.Xml.XPathVariable[] vars); + } +} diff --git a/src/Umbraco.Web/PublishedCache/ContextualPublishedContentCache.cs b/src/Umbraco.Web/PublishedCache/ContextualPublishedContentCache.cs new file mode 100644 index 0000000000..0a32ad8e8d --- /dev/null +++ b/src/Umbraco.Web/PublishedCache/ContextualPublishedContentCache.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Umbraco.Core.Models; + +namespace Umbraco.Web.PublishedCache +{ + /// + /// Provides access to cached documents in a specified context. + /// + internal class ContextualPublishedContentCache : ContextualPublishedCache + { + private readonly IPublishedContentCache _cache; + + /// + /// Initializes a new instance of the class with a published content cache and a context. + /// + /// A published content cache. + /// A context. + public ContextualPublishedContentCache(IPublishedContentCache cache, UmbracoContext umbracoContext) + : base(umbracoContext) + { + _cache = cache; + } + + /// + /// Gets a content identified by its unique identifier. + /// + /// The content unique identifier. + /// The content, or null. + public override IPublishedContent GetById(int contentId) + { + return _cache.GetById(UmbracoContext, contentId); + } + + /// + /// Gets contents at root. + /// + /// The contents. + public override IEnumerable GetAtRoot() + { + return _cache.GetAtRoot(UmbracoContext); + } + + /// + /// Gets a content resulting from an XPath query. + /// + /// The XPath query. + /// Optional XPath variables. + /// The content, or null. + public override IPublishedContent GetSingleByXPath(string xpath, Core.Xml.XPathVariable[] vars) + { + return _cache.GetSingleByXPath(UmbracoContext, xpath, vars); + } + + /// + /// Gets contents resulting from an XPath query. + /// + /// The XPath query. + /// Optional XPath variables. + /// The contents. + public override IEnumerable GetByXPath(string xpath, Core.Xml.XPathVariable[] vars) + { + return _cache.GetByXPath(UmbracoContext, xpath, vars); + } + + // FIXME do we want that one here? + public IPublishedContent GetByRoute(string route, bool? hideTopLevelNode = null) + { + return _cache.GetByRoute(UmbracoContext, route, hideTopLevelNode); + } + + // FIXME do we want that one here? + public IPublishedContent GetByUrlAlias(int rootNodeId, string alias) + { + return _cache.GetByUrlAlias(UmbracoContext, rootNodeId, alias); + } + + /// + /// Gets a value indicating whether the underlying non-contextual cache contains published content. + /// + /// The context. + /// A value indicating whether the underlying non-contextual cache contains published content. + public bool HasContent(UmbracoContext umbracoContext) + { + return _cache.HasContent(umbracoContext); + } + } +} diff --git a/src/Umbraco.Web/PublishedCache/ContextualPublishedMediaCache.cs b/src/Umbraco.Web/PublishedCache/ContextualPublishedMediaCache.cs new file mode 100644 index 0000000000..94cc37567a --- /dev/null +++ b/src/Umbraco.Web/PublishedCache/ContextualPublishedMediaCache.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Umbraco.Core.Models; + +namespace Umbraco.Web.PublishedCache +{ + /// + /// Provides access to cached medias in a specified context. + /// + internal class ContextualPublishedMediaCache : ContextualPublishedCache + { + private readonly IPublishedMediaCache _cache; + + /// + /// Initializes a new instance of the class with a published media cache and a context. + /// + /// A published media cache. + /// A context. + public ContextualPublishedMediaCache(IPublishedMediaCache cache, UmbracoContext umbracoContext) + : base(umbracoContext) + { + _cache = cache; + } + + /// + /// Gets a content identified by its unique identifier. + /// + /// The content unique identifier. + /// The content, or null. + public override IPublishedContent GetById(int contentId) + { + return _cache.GetById(UmbracoContext, contentId); + } + + /// + /// Gets contents at root. + /// + /// The contents. + public override IEnumerable GetAtRoot() + { + return _cache.GetAtRoot(UmbracoContext); + } + + /// + /// Gets a content resulting from an XPath query. + /// + /// The XPath query. + /// Optional XPath variables. + /// The content, or null. + public override IPublishedContent GetSingleByXPath(string xpath, Core.Xml.XPathVariable[] vars) + { + return _cache.GetSingleByXPath(UmbracoContext, xpath, vars); + } + + /// + /// Gets contents resulting from an XPath query. + /// + /// The XPath query. + /// Optional XPath variables. + /// The contents. + public override IEnumerable GetByXPath(string xpath, Core.Xml.XPathVariable[] vars) + { + return _cache.GetByXPath(UmbracoContext, xpath, vars); + } + } +} diff --git a/src/Umbraco.Web/PublishedCache/IPublishedCache.cs b/src/Umbraco.Web/PublishedCache/IPublishedCache.cs index 9efd785ec1..92e78c84b0 100644 --- a/src/Umbraco.Web/PublishedCache/IPublishedCache.cs +++ b/src/Umbraco.Web/PublishedCache/IPublishedCache.cs @@ -9,20 +9,44 @@ using Umbraco.Core.Xml; namespace Umbraco.Web.PublishedCache { - /// - /// Defines the methods for published documents - /// - [UmbracoExperimentalFeature("http://issues.umbraco.org/issue/U4-1153", + /// + /// Provides access to cached contents. + /// + [UmbracoExperimentalFeature("http://issues.umbraco.org/issue/U4-1153", "We need to create something like the IPublishListener interface to have proper published content storage.")] internal interface IPublishedCache { - // FIXME do we need the context here?! - // FIXME write documentation + /// + /// Gets a content identified by its unique identifier. + /// + /// The context. + /// The content unique identifier. + /// The content, or null. + IPublishedContent GetById(UmbracoContext umbracoContext, int contentId); - IPublishedContent GetById(UmbracoContext umbracoContext, int nodeId); - IEnumerable GetAtRoot(UmbracoContext umbracoContext); + /// + /// Gets contents at root. + /// + /// The context. + /// The contents. + IEnumerable GetAtRoot(UmbracoContext umbracoContext); - IPublishedContent GetSingleByXPath(UmbracoContext umbracoContext, string xpath, XPathVariable[] vars); + /// + /// Gets a content resulting from an XPath query. + /// + /// The context. + /// The XPath query. + /// Optional XPath variables. + /// The content, or null. + IPublishedContent GetSingleByXPath(UmbracoContext umbracoContext, string xpath, XPathVariable[] vars); + + /// + /// Gets contents resulting from an XPath query. + /// + /// The context. + /// The XPath query. + /// Optional XPath variables. + /// The contents. IEnumerable GetByXPath(UmbracoContext umbracoContext, string xpath, XPathVariable[] vars); // ... GetXPath single or multi diff --git a/src/Umbraco.Web/PublishedCache/IPublishedContentCache.cs b/src/Umbraco.Web/PublishedCache/IPublishedContentCache.cs index 9e00068b3b..42457426dc 100644 --- a/src/Umbraco.Web/PublishedCache/IPublishedContentCache.cs +++ b/src/Umbraco.Web/PublishedCache/IPublishedContentCache.cs @@ -9,15 +9,17 @@ namespace Umbraco.Web.PublishedCache { internal interface IPublishedContentCache : IPublishedCache { - // do we want that one? + // FIXME do we want that one? IPublishedContent GetByRoute(UmbracoContext umbracoContext, string route, bool? hideTopLevelNode = null); - // do we want that one? + + // FIXME do we want that one? IPublishedContent GetByUrlAlias(UmbracoContext umbracoContext, int rootNodeId, string alias); - // vs. having a GetDocumentByXPath ?! - - // do we want to get-by-xpath? - + /// + /// Gets a value indicating whether the cache contains published content. + /// + /// The context. + /// A value indicating whether the cache contains published content. bool HasContent(UmbracoContext umbracoContext); } } diff --git a/src/Umbraco.Web/PublishedCache/LegacyXmlCache/PublishedContentCache.cs b/src/Umbraco.Web/PublishedCache/LegacyXmlCache/PublishedContentCache.cs index f6d44bf544..29e9946eea 100644 --- a/src/Umbraco.Web/PublishedCache/LegacyXmlCache/PublishedContentCache.cs +++ b/src/Umbraco.Web/PublishedCache/LegacyXmlCache/PublishedContentCache.cs @@ -6,11 +6,13 @@ using Umbraco.Core.Models; using Umbraco.Core.Xml; using umbraco; using System.Linq; +using umbraco.BusinessLogic; +using umbraco.presentation.preview; namespace Umbraco.Web.PublishedCache.LegacyXmlCache { internal class PublishedContentCache : IPublishedContentCache - { + { #region XPath Strings class XPathStringsDefinition @@ -82,7 +84,9 @@ namespace Umbraco.Web.PublishedCache.LegacyXmlCache #endregion - private static IPublishedContent ConvertToDocument(XmlNode xmlNode) + #region Converters + + private static IPublishedContent ConvertToDocument(XmlNode xmlNode) { return xmlNode == null ? null : new Models.XmlPublishedContent(xmlNode); } @@ -91,11 +95,13 @@ namespace Umbraco.Web.PublishedCache.LegacyXmlCache { return xmlNodes.Cast().Select(xmlNode => new Models.XmlPublishedContent(xmlNode)); } - - public virtual IPublishedContent GetById(UmbracoContext umbracoContext, int nodeId) - { - if (umbracoContext == null) throw new ArgumentNullException("umbracoContext"); + #endregion + + #region Getters + + public virtual IPublishedContent GetById(UmbracoContext umbracoContext, int nodeId) + { return ConvertToDocument(GetXml(umbracoContext).GetElementById(nodeId.ToString())); } @@ -106,7 +112,6 @@ namespace Umbraco.Web.PublishedCache.LegacyXmlCache public IPublishedContent GetSingleByXPath(UmbracoContext umbracoContext, string xpath, params XPathVariable[] vars) { - if (umbracoContext == null) throw new ArgumentNullException("umbracoContext"); if (xpath == null) throw new ArgumentNullException("xpath"); if (string.IsNullOrWhiteSpace(xpath)) return null; @@ -119,7 +124,6 @@ namespace Umbraco.Web.PublishedCache.LegacyXmlCache public IEnumerable GetByXPath(UmbracoContext umbracoContext, string xpath, params XPathVariable[] vars) { - if (umbracoContext == null) throw new ArgumentNullException("umbracoContext"); if (xpath == null) throw new ArgumentNullException("xpath"); if (string.IsNullOrWhiteSpace(xpath)) return Enumerable.Empty(); @@ -131,9 +135,8 @@ namespace Umbraco.Web.PublishedCache.LegacyXmlCache } //FIXME keep here or remove? - public IPublishedContent GetByRoute(UmbracoContext umbracoContext, string route, bool? hideTopLevelNode = null) + public IPublishedContent GetByRoute(UmbracoContext umbracoContext, string route, bool? hideTopLevelNode = null) { - if (umbracoContext == null) throw new ArgumentNullException("umbracoContext"); if (route == null) throw new ArgumentNullException("route"); //set the default to be what is in the settings @@ -165,9 +168,8 @@ namespace Umbraco.Web.PublishedCache.LegacyXmlCache } // FIXME MOVE THAT ONE OUT OF HERE? - public IPublishedContent GetByUrlAlias(UmbracoContext umbracoContext, int rootNodeId, string alias) + public IPublishedContent GetByUrlAlias(UmbracoContext umbracoContext, int rootNodeId, string alias) { - if (umbracoContext == null) throw new ArgumentNullException("umbracoContext"); if (alias == null) throw new ArgumentNullException("alias"); // the alias may be "foo/bar" or "/foo/bar" @@ -197,21 +199,70 @@ namespace Umbraco.Web.PublishedCache.LegacyXmlCache public bool HasContent(UmbracoContext umbracoContext) { - var xml = GetXml(umbracoContext); + var xml = GetXml(umbracoContext, false); if (xml == null) return false; var node = xml.SelectSingleNode(XPathStrings.RootDocuments); return node != null; } - static XmlDocument GetXml(UmbracoContext umbracoContext) - { - if (umbracoContext == null) throw new ArgumentNullException("umbracoContext"); + #endregion - return umbracoContext.GetXml(); - } + #region Legacy Xml - static readonly char[] SlashChar = new[] { '/' }; + private PreviewContent _previewContent; + private Func _xmlDelegate; + + /// + /// Gets/sets the delegate used to retreive the Xml content, generally the setter is only used for unit tests + /// and by default if it is not set will use the standard delegate which ONLY works when in the context an Http Request + /// + /// + /// If not defined, we will use the standard delegate which ONLY works when in the context an Http Request + /// mostly because the 'content' object heavily relies on HttpContext, SQL connections and a bunch of other stuff + /// that when run inside of a unit test fails. + /// + internal Func GetXmlDelegate + { + get + { + return _xmlDelegate ?? (_xmlDelegate = (user, preview) => + { + if (preview) + { + if (_previewContent == null) + { + _previewContent = new PreviewContent(user, new Guid(global::umbraco.BusinessLogic.StateHelper.Cookies.Preview.GetValue()), true); + if (_previewContent.ValidPreviewSet) + _previewContent.LoadPreviewset(); + } + if (_previewContent.ValidPreviewSet) + return _previewContent.XmlContent; + } + return content.Instance.XmlContent; + }); + } + set + { + _xmlDelegate = value; + } + } + + internal XmlDocument GetXml(UmbracoContext umbracoContext) + { + return GetXmlDelegate(umbracoContext.UmbracoUser, umbracoContext.InPreviewMode); + } + + internal XmlDocument GetXml(UmbracoContext umbracoContext, bool inPreviewMode) + { + return GetXmlDelegate(umbracoContext.UmbracoUser, inPreviewMode); + } + + #endregion + + #region XPathQuery + + static readonly char[] SlashChar = new[] { '/' }; protected string CreateXpathQuery(int startNodeId, string path, bool hideTopLevelNodeFromPath, out IEnumerable vars) { @@ -290,5 +341,7 @@ namespace Umbraco.Web.PublishedCache.LegacyXmlCache return xpath; } + + #endregion } } \ No newline at end of file diff --git a/src/Umbraco.Web/PublishedCache/LegacyXmlCache/PublishedMediaCache.cs b/src/Umbraco.Web/PublishedCache/LegacyXmlCache/PublishedMediaCache.cs index cb4bed448c..ab5d2b231a 100644 --- a/src/Umbraco.Web/PublishedCache/LegacyXmlCache/PublishedMediaCache.cs +++ b/src/Umbraco.Web/PublishedCache/LegacyXmlCache/PublishedMediaCache.cs @@ -29,7 +29,6 @@ namespace Umbraco.Web.PublishedCache.LegacyXmlCache /// internal class PublishedMediaCache : IPublishedMediaCache { - public PublishedMediaCache() { } @@ -39,8 +38,8 @@ namespace Umbraco.Web.PublishedCache.LegacyXmlCache /// /// /// - internal PublishedMediaCache(BaseSearchProvider searchProvider, BaseIndexProvider indexProvider) - { + internal PublishedMediaCache(BaseSearchProvider searchProvider, BaseIndexProvider indexProvider) + { _searchProvider = searchProvider; _indexProvider = indexProvider; } diff --git a/src/Umbraco.Web/PublishedContentExtensions.cs b/src/Umbraco.Web/PublishedContentExtensions.cs index fa89ab3bd7..1510b33ba1 100644 --- a/src/Umbraco.Web/PublishedContentExtensions.cs +++ b/src/Umbraco.Web/PublishedContentExtensions.cs @@ -270,7 +270,7 @@ namespace Umbraco.Web s = searchProvider; var results = s.Search(criteria); - return results.ConvertSearchResultToPublishedContent(PublishedContentCacheResolver.Current.PublishedContentCache); + return results.ConvertSearchResultToPublishedContent(UmbracoContext.Current.ContentCache); } #endregion @@ -1128,7 +1128,7 @@ namespace Umbraco.Web { //get the root docs if parent is null return content.Parent == null - ? PublishedContentCacheResolver.Current.PublishedContentCache.GetAtRoot(UmbracoContext.Current) + ? UmbracoContext.Current.ContentCache.GetAtRoot() : content.Parent.Children; } diff --git a/src/Umbraco.Web/Routing/AliasUrlProvider.cs b/src/Umbraco.Web/Routing/AliasUrlProvider.cs index f47ceaac4b..8ef14d86d2 100644 --- a/src/Umbraco.Web/Routing/AliasUrlProvider.cs +++ b/src/Umbraco.Web/Routing/AliasUrlProvider.cs @@ -55,7 +55,7 @@ namespace Umbraco.Web.Routing if (!FindByUrlAliasEnabled) return Enumerable.Empty(); // we have nothing to say - var node = umbracoContext.ContentCache.GetById(umbracoContext, id); + var node = umbracoContext.ContentCache.GetById(id); string umbracoUrlName = null; if (node.HasProperty(Constants.Conventions.Content.UrlAlias)) umbracoUrlName = node.GetPropertyValue(Constants.Conventions.Content.UrlAlias); diff --git a/src/Umbraco.Web/Routing/ContentFinderByIdPath.cs b/src/Umbraco.Web/Routing/ContentFinderByIdPath.cs index a7f1bf7bf3..ecccdf097f 100644 --- a/src/Umbraco.Web/Routing/ContentFinderByIdPath.cs +++ b/src/Umbraco.Web/Routing/ContentFinderByIdPath.cs @@ -34,9 +34,7 @@ namespace Umbraco.Web.Routing if (nodeId > 0) { LogHelper.Debug("Id={0}", () => nodeId); - node = docRequest.RoutingContext.UmbracoContext.ContentCache.GetById( - docRequest.RoutingContext.UmbracoContext, - nodeId); + node = docRequest.RoutingContext.UmbracoContext.ContentCache.GetById(nodeId); if (node != null) { diff --git a/src/Umbraco.Web/Routing/ContentFinderByLegacy404.cs b/src/Umbraco.Web/Routing/ContentFinderByLegacy404.cs index 8d4c63964d..a42a7b6978 100644 --- a/src/Umbraco.Web/Routing/ContentFinderByLegacy404.cs +++ b/src/Umbraco.Web/Routing/ContentFinderByLegacy404.cs @@ -27,9 +27,7 @@ namespace Umbraco.Web.Routing { LogHelper.Debug("Got id={0}.", () => id); - content = pcr.RoutingContext.UmbracoContext.ContentCache.GetById( - pcr.RoutingContext.UmbracoContext, - id); + content = pcr.RoutingContext.UmbracoContext.ContentCache.GetById(id); LogHelper.Debug(content == null ? "Could not find content with that id." diff --git a/src/Umbraco.Web/Routing/ContentFinderByNiceUrl.cs b/src/Umbraco.Web/Routing/ContentFinderByNiceUrl.cs index ed0d155f74..61d362a25e 100644 --- a/src/Umbraco.Web/Routing/ContentFinderByNiceUrl.cs +++ b/src/Umbraco.Web/Routing/ContentFinderByNiceUrl.cs @@ -66,9 +66,7 @@ namespace Umbraco.Web.Routing IPublishedContent node = null; if (nodeId > 0) { - node = docreq.RoutingContext.UmbracoContext.ContentCache.GetById( - docreq.RoutingContext.UmbracoContext, - nodeId); + node = docreq.RoutingContext.UmbracoContext.ContentCache.GetById(nodeId); if (node != null) { @@ -85,9 +83,7 @@ namespace Umbraco.Web.Routing if (node == null) { LogHelper.Debug("Cache miss, query"); - node = docreq.RoutingContext.UmbracoContext.ContentCache.GetByRoute( - docreq.RoutingContext.UmbracoContext, - route); + node = docreq.RoutingContext.UmbracoContext.ContentCache.GetByRoute(route); if (node != null) { diff --git a/src/Umbraco.Web/Routing/ContentFinderByNotFoundHandler.cs b/src/Umbraco.Web/Routing/ContentFinderByNotFoundHandler.cs index de53583062..38036b5969 100644 --- a/src/Umbraco.Web/Routing/ContentFinderByNotFoundHandler.cs +++ b/src/Umbraco.Web/Routing/ContentFinderByNotFoundHandler.cs @@ -30,9 +30,7 @@ namespace Umbraco.Web.Routing { LogHelper.Debug>("Handler '{0}' returned id={1}.", () => type.FullName, () => handler.redirectID); - var content = pcr.RoutingContext.UmbracoContext.ContentCache.GetById( - pcr.RoutingContext.UmbracoContext, - handler.redirectID); + var content = pcr.RoutingContext.UmbracoContext.ContentCache.GetById(handler.redirectID); LogHelper.Debug>(content == null ? "Could not find content with that id." diff --git a/src/Umbraco.Web/Routing/ContentFinderByNotFoundHandlers.cs b/src/Umbraco.Web/Routing/ContentFinderByNotFoundHandlers.cs index efd83ca415..7dcf8733fa 100644 --- a/src/Umbraco.Web/Routing/ContentFinderByNotFoundHandlers.cs +++ b/src/Umbraco.Web/Routing/ContentFinderByNotFoundHandlers.cs @@ -76,9 +76,7 @@ namespace Umbraco.Web.Routing if (handler.Execute(url) && handler.redirectID > 0) { var redirectId = handler.redirectID; - docRequest.PublishedContent = docRequest.RoutingContext.UmbracoContext.ContentCache.GetById( - docRequest.RoutingContext.UmbracoContext, - redirectId); + docRequest.PublishedContent = docRequest.RoutingContext.UmbracoContext.ContentCache.GetById(redirectId); if (!docRequest.HasPublishedContent) { diff --git a/src/Umbraco.Web/Routing/ContentFinderByPageIdQuery.cs b/src/Umbraco.Web/Routing/ContentFinderByPageIdQuery.cs index 10a85ae0d6..8139679438 100644 --- a/src/Umbraco.Web/Routing/ContentFinderByPageIdQuery.cs +++ b/src/Umbraco.Web/Routing/ContentFinderByPageIdQuery.cs @@ -14,9 +14,7 @@ namespace Umbraco.Web.Routing int pageId; if (int.TryParse(docRequest.RoutingContext.UmbracoContext.HttpContext.Request["umbPageID"], out pageId)) { - var doc = docRequest.RoutingContext.UmbracoContext.ContentCache.GetById( - docRequest.RoutingContext.UmbracoContext, - pageId); + var doc = docRequest.RoutingContext.UmbracoContext.ContentCache.GetById(pageId); if (doc != null) { diff --git a/src/Umbraco.Web/Routing/ContentFinderByUrlAlias.cs b/src/Umbraco.Web/Routing/ContentFinderByUrlAlias.cs index bff69b24c0..5dc367c2c3 100644 --- a/src/Umbraco.Web/Routing/ContentFinderByUrlAlias.cs +++ b/src/Umbraco.Web/Routing/ContentFinderByUrlAlias.cs @@ -25,7 +25,6 @@ namespace Umbraco.Web.Routing if (docRequest.Uri.AbsolutePath != "/") // no alias if "/" { node = docRequest.RoutingContext.UmbracoContext.ContentCache.GetByUrlAlias( - docRequest.RoutingContext.UmbracoContext, docRequest.HasDomain ? docRequest.Domain.RootNodeId : 0, docRequest.Uri.GetAbsolutePathDecoded()); diff --git a/src/Umbraco.Web/Routing/DefaultUrlProvider.cs b/src/Umbraco.Web/Routing/DefaultUrlProvider.cs index be5a23b89e..6347985aeb 100644 --- a/src/Umbraco.Web/Routing/DefaultUrlProvider.cs +++ b/src/Umbraco.Web/Routing/DefaultUrlProvider.cs @@ -55,7 +55,7 @@ namespace Umbraco.Web.Routing else { // there was no route in the cache - create a route - var node = umbracoContext.ContentCache.GetById(umbracoContext, id); + var node = umbracoContext.ContentCache.GetById(id); if (node == null) { LogHelper.Warn( @@ -135,7 +135,7 @@ namespace Umbraco.Web.Routing else { // there was no route in the cache - create a route - var node = umbracoContext.ContentCache.GetById(umbracoContext, id); + var node = umbracoContext.ContentCache.GetById(id); if (node == null) { LogHelper.Warn( @@ -283,7 +283,7 @@ namespace Umbraco.Web.Routing // that's the way it works pre-4.10 and we try to be backward compat for the time being if (node.Parent == null) { - var rootNode = umbracoContext.ContentCache.GetByRoute(umbracoContext, "/", true); + var rootNode = umbracoContext.ContentCache.GetByRoute("/", true); if (rootNode.Id == node.Id) // remove only if we're the default node pathParts.RemoveAt(pathParts.Count - 1); } diff --git a/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs b/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs index 177a90ad42..307bcfd28a 100644 --- a/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs +++ b/src/Umbraco.Web/Routing/PublishedContentRequestEngine.cs @@ -444,7 +444,7 @@ namespace Umbraco.Web.Routing else { // redirect to another page - var node = _routingContext.UmbracoContext.ContentCache.GetById(_routingContext.UmbracoContext, internalRedirectId); + var node = _routingContext.UmbracoContext.ContentCache.GetById(internalRedirectId); _pcr.SetInternalRedirectPublishedContent(node); // don't use .PublishedContent here if (node != null) @@ -494,14 +494,14 @@ namespace Umbraco.Web.Routing LogHelper.Debug("{0}Not logged in, redirect to login page", () => tracePrefix); var loginPageId = Access.GetLoginPage(path); if (loginPageId != _pcr.PublishedContent.Id) - _pcr.PublishedContent = _routingContext.UmbracoContext.ContentCache.GetById(_routingContext.UmbracoContext, loginPageId); + _pcr.PublishedContent = _routingContext.UmbracoContext.ContentCache.GetById(loginPageId); } else if (!Access.HasAccces(_pcr.PublishedContent.Id, user.ProviderUserKey)) { LogHelper.Debug("{0}Current member has not access, redirect to error page", () => tracePrefix); var errorPageId = Access.GetErrorPage(path); if (errorPageId != _pcr.PublishedContent.Id) - _pcr.PublishedContent = _routingContext.UmbracoContext.ContentCache.GetById(_routingContext.UmbracoContext, errorPageId); + _pcr.PublishedContent = _routingContext.UmbracoContext.ContentCache.GetById(errorPageId); } else { diff --git a/src/Umbraco.Web/Templates/TemplateRenderer.cs b/src/Umbraco.Web/Templates/TemplateRenderer.cs index d5abcc6ee7..fe60c6959e 100644 --- a/src/Umbraco.Web/Templates/TemplateRenderer.cs +++ b/src/Umbraco.Web/Templates/TemplateRenderer.cs @@ -54,9 +54,7 @@ namespace Umbraco.Web.Templates // terribly much for this implementation since we are just creating a doc content request to modify it's properties manually. var contentRequest = new PublishedContentRequest(_umbracoContext.CleanedUmbracoUrl, _umbracoContext.RoutingContext); - var doc = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetById( - contentRequest.RoutingContext.UmbracoContext, - PageId); + var doc = contentRequest.RoutingContext.UmbracoContext.ContentCache.GetById(PageId); if (doc == null) { diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index d26d3a4f49..2c04005a20 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -320,6 +320,9 @@ + + + diff --git a/src/Umbraco.Web/UmbracoContext.cs b/src/Umbraco.Web/UmbracoContext.cs index 53aea8c6ca..cf7131eb15 100644 --- a/src/Umbraco.Web/UmbracoContext.cs +++ b/src/Umbraco.Web/UmbracoContext.cs @@ -131,8 +131,8 @@ namespace Umbraco.Web HttpContext = httpContext; Application = applicationContext; - ContentCache = contentCache; - MediaCache = mediaCache; + ContentCache = new ContextualPublishedContentCache(contentCache, this); + MediaCache = new ContextualPublishedMediaCache(mediaCache, this); // set the urls... //original request url @@ -225,12 +225,12 @@ namespace Umbraco.Web /// /// Gets or sets the published content cache. /// - internal IPublishedContentCache ContentCache { get; private set; } + internal ContextualPublishedContentCache ContentCache { get; private set; } /// /// Gets or sets the published media cache. /// - internal IPublishedMediaCache MediaCache { get; private set; } + internal ContextualPublishedMediaCache MediaCache { get; private set; } private Func _xmlDelegate; diff --git a/src/Umbraco.Web/UmbracoHelper.cs b/src/Umbraco.Web/UmbracoHelper.cs index 7c98071d0e..7a19ed737a 100644 --- a/src/Umbraco.Web/UmbracoHelper.cs +++ b/src/Umbraco.Web/UmbracoHelper.cs @@ -438,32 +438,32 @@ namespace Umbraco.Web public IPublishedContent TypedContent(object id) { - return TypedDocumentById(id, PublishedContentCacheResolver.Current.PublishedContentCache); + return TypedDocumentById(id, _umbracoContext.ContentCache); } public IPublishedContent TypedContent(int id) { - return TypedDocumentById(id, PublishedContentCacheResolver.Current.PublishedContentCache); + return TypedDocumentById(id, _umbracoContext.ContentCache); } public IPublishedContent TypedContent(string id) { - return TypedDocumentById(id, PublishedContentCacheResolver.Current.PublishedContentCache); + return TypedDocumentById(id, _umbracoContext.ContentCache); } public IEnumerable TypedContent(params object[] ids) { - return TypedDocumentsbyIds(PublishedContentCacheResolver.Current.PublishedContentCache, ids); + return TypedDocumentsbyIds(_umbracoContext.ContentCache, ids); } public IEnumerable TypedContent(params int[] ids) { - return TypedDocumentsbyIds(PublishedContentCacheResolver.Current.PublishedContentCache, ids); + return TypedDocumentsbyIds(_umbracoContext.ContentCache, ids); } public IEnumerable TypedContent(params string[] ids) { - return TypedDocumentsbyIds(PublishedContentCacheResolver.Current.PublishedContentCache, ids); + return TypedDocumentsbyIds(_umbracoContext.ContentCache, ids); } public IEnumerable TypedContent(IEnumerable ids) @@ -483,37 +483,37 @@ namespace Umbraco.Web public IEnumerable TypedContentAtRoot() { - return TypedDocumentsAtRoot(PublishedContentCacheResolver.Current.PublishedContentCache); + return TypedDocumentsAtRoot(_umbracoContext.ContentCache); } public dynamic Content(object id) { - return DocumentById(id, PublishedContentCacheResolver.Current.PublishedContentCache, new DynamicNull()); + return DocumentById(id, _umbracoContext.ContentCache, new DynamicNull()); } public dynamic Content(int id) { - return DocumentById(id, PublishedContentCacheResolver.Current.PublishedContentCache, new DynamicNull()); + return DocumentById(id, _umbracoContext.ContentCache, new DynamicNull()); } public dynamic Content(string id) { - return DocumentById(id, PublishedContentCacheResolver.Current.PublishedContentCache, new DynamicNull()); + return DocumentById(id, _umbracoContext.ContentCache, new DynamicNull()); } public dynamic Content(params object[] ids) { - return DocumentByIds(PublishedContentCacheResolver.Current.PublishedContentCache, ids); + return DocumentByIds(_umbracoContext.ContentCache, ids); } public dynamic Content(params int[] ids) { - return DocumentByIds(PublishedContentCacheResolver.Current.PublishedContentCache, ids); + return DocumentByIds(_umbracoContext.ContentCache, ids); } public dynamic Content(params string[] ids) { - return DocumentByIds(PublishedContentCacheResolver.Current.PublishedContentCache, ids); + return DocumentByIds(_umbracoContext.ContentCache, ids); } public dynamic Content(IEnumerable ids) @@ -533,7 +533,7 @@ namespace Umbraco.Web public dynamic ContentAtRoot() { - return DocumentsAtRoot(PublishedContentCacheResolver.Current.PublishedContentCache); + return DocumentsAtRoot(_umbracoContext.ContentCache); } #endregion @@ -552,32 +552,32 @@ namespace Umbraco.Web /// public IPublishedContent TypedMedia(object id) { - return TypedDocumentById(id, PublishedMediaCacheResolver.Current.PublishedMediaCache); + return TypedDocumentById(id, _umbracoContext.MediaCache); } public IPublishedContent TypedMedia(int id) { - return TypedDocumentById(id, PublishedMediaCacheResolver.Current.PublishedMediaCache); + return TypedDocumentById(id, _umbracoContext.MediaCache); } public IPublishedContent TypedMedia(string id) { - return TypedDocumentById(id, PublishedMediaCacheResolver.Current.PublishedMediaCache); + return TypedDocumentById(id, _umbracoContext.MediaCache); } public IEnumerable TypedMedia(params object[] ids) { - return TypedDocumentsbyIds(PublishedMediaCacheResolver.Current.PublishedMediaCache, ids); + return TypedDocumentsbyIds(_umbracoContext.MediaCache, ids); } public IEnumerable TypedMedia(params int[] ids) { - return TypedDocumentsbyIds(PublishedMediaCacheResolver.Current.PublishedMediaCache, ids); + return TypedDocumentsbyIds(_umbracoContext.MediaCache, ids); } public IEnumerable TypedMedia(params string[] ids) { - return TypedDocumentsbyIds(PublishedMediaCacheResolver.Current.PublishedMediaCache, ids); + return TypedDocumentsbyIds(_umbracoContext.MediaCache, ids); } public IEnumerable TypedMedia(IEnumerable ids) @@ -597,37 +597,37 @@ namespace Umbraco.Web public IEnumerable TypedMediaAtRoot() { - return TypedDocumentsAtRoot(PublishedMediaCacheResolver.Current.PublishedMediaCache); + return TypedDocumentsAtRoot(_umbracoContext.MediaCache); } public dynamic Media(object id) { - return DocumentById(id, PublishedMediaCacheResolver.Current.PublishedMediaCache, new DynamicNull()); + return DocumentById(id, _umbracoContext.MediaCache, new DynamicNull()); } public dynamic Media(int id) { - return DocumentById(id, PublishedMediaCacheResolver.Current.PublishedMediaCache, new DynamicNull()); + return DocumentById(id, _umbracoContext.MediaCache, new DynamicNull()); } public dynamic Media(string id) { - return DocumentById(id, PublishedMediaCacheResolver.Current.PublishedMediaCache, new DynamicNull()); + return DocumentById(id, _umbracoContext.MediaCache, new DynamicNull()); } public dynamic Media(params object[] ids) { - return DocumentByIds(PublishedMediaCacheResolver.Current.PublishedMediaCache, ids); + return DocumentByIds(_umbracoContext.MediaCache, ids); } public dynamic Media(params int[] ids) { - return DocumentByIds(PublishedMediaCacheResolver.Current.PublishedMediaCache, ids); + return DocumentByIds(_umbracoContext.MediaCache, ids); } public dynamic Media(params string[] ids) { - return DocumentByIds(PublishedMediaCacheResolver.Current.PublishedMediaCache, ids); + return DocumentByIds(_umbracoContext.MediaCache, ids); } public dynamic Media(IEnumerable ids) @@ -647,7 +647,7 @@ namespace Umbraco.Web public dynamic MediaAtRoot() { - return DocumentsAtRoot(PublishedMediaCacheResolver.Current.PublishedMediaCache); + return DocumentsAtRoot(_umbracoContext.MediaCache); } #endregion @@ -665,7 +665,7 @@ namespace Umbraco.Web /// this result in to this method. /// This method will throw an exception if the value is not of type int or string. /// - private IPublishedContent TypedDocumentById(object id, IPublishedCache cache) + private IPublishedContent TypedDocumentById(object id, ContextualPublishedCache cache) { if (id is string) return TypedDocumentById((string)id, cache); @@ -674,13 +674,13 @@ namespace Umbraco.Web throw new InvalidOperationException("The value of parameter 'id' must be either a string or an integer"); } - private IPublishedContent TypedDocumentById(int id, IPublishedCache cache) + private IPublishedContent TypedDocumentById(int id, ContextualPublishedCache cache) { - var doc = cache.GetById(UmbracoContext.Current, id); + var doc = cache.GetById(id); return doc; } - private IPublishedContent TypedDocumentById(string id, IPublishedCache cache) + private IPublishedContent TypedDocumentById(string id, ContextualPublishedCache cache) { int docId; return int.TryParse(id, out docId) @@ -699,24 +699,24 @@ namespace Umbraco.Web /// this result in to this method. /// This method will throw an exception if the value is not of type int or string. /// - private IEnumerable TypedDocumentsbyIds(IPublishedCache cache, params object[] ids) + private IEnumerable TypedDocumentsbyIds(ContextualPublishedCache cache, params object[] ids) { return ids.Select(eachId => TypedDocumentById(eachId, cache)); } - private IEnumerable TypedDocumentsbyIds(IPublishedCache cache, params int[] ids) + private IEnumerable TypedDocumentsbyIds(ContextualPublishedCache cache, params int[] ids) { return ids.Select(eachId => TypedDocumentById(eachId, cache)); } - private IEnumerable TypedDocumentsbyIds(IPublishedCache cache, params string[] ids) + private IEnumerable TypedDocumentsbyIds(ContextualPublishedCache cache, params string[] ids) { return ids.Select(eachId => TypedDocumentById(eachId, cache)); } - private IEnumerable TypedDocumentsAtRoot(IPublishedCache cache) + private IEnumerable TypedDocumentsAtRoot(ContextualPublishedCache cache) { - return cache.GetAtRoot(_umbracoContext); + return cache.GetAtRoot(); } /// @@ -731,7 +731,7 @@ namespace Umbraco.Web /// this result in to this method. /// This method will throw an exception if the value is not of type int or string. /// - private dynamic DocumentById(object id, IPublishedCache cache, object ifNotFound) + private dynamic DocumentById(object id, ContextualPublishedCache cache, object ifNotFound) { if (id is string) return DocumentById((string)id, cache, ifNotFound); @@ -740,15 +740,15 @@ namespace Umbraco.Web throw new InvalidOperationException("The value of parameter 'id' must be either a string or an integer"); } - private dynamic DocumentById(int id, IPublishedCache cache, object ifNotFound) + private dynamic DocumentById(int id, ContextualPublishedCache cache, object ifNotFound) { - var doc = cache.GetById(UmbracoContext.Current, id); + var doc = cache.GetById(id); return doc == null ? ifNotFound : new DynamicPublishedContent(doc).AsDynamic(); } - private dynamic DocumentById(string id, IPublishedCache cache, object ifNotFound) + private dynamic DocumentById(string id, ContextualPublishedCache cache, object ifNotFound) { int docId; return int.TryParse(id, out docId) @@ -756,10 +756,10 @@ namespace Umbraco.Web : ifNotFound; } - private dynamic DocumentsAtRoot(IPublishedCache cache) + private dynamic DocumentsAtRoot(ContextualPublishedCache cache) { return new DynamicPublishedContentList( - cache.GetAtRoot(_umbracoContext) + cache.GetAtRoot() .Select(publishedContent => new DynamicPublishedContent(publishedContent)) ); } @@ -775,7 +775,7 @@ namespace Umbraco.Web /// this result in to this method. /// This method will throw an exception if the value is not of type int or string. /// - private dynamic DocumentByIds(IPublishedCache cache, params object[] ids) + private dynamic DocumentByIds(ContextualPublishedCache cache, params object[] ids) { var dNull = new DynamicNull(); var nodes = ids.Select(eachId => DocumentById(eachId, cache, dNull)) @@ -784,7 +784,7 @@ namespace Umbraco.Web return new DynamicPublishedContentList(nodes); } - private dynamic DocumentByIds(IPublishedCache cache, params int[] ids) + private dynamic DocumentByIds(ContextualPublishedCache cache, params int[] ids) { var dNull = new DynamicNull(); var nodes = ids.Select(eachId => DocumentById(eachId, cache, dNull)) @@ -793,7 +793,7 @@ namespace Umbraco.Web return new DynamicPublishedContentList(nodes); } - private dynamic DocumentByIds(IPublishedCache cache, params string[] ids) + private dynamic DocumentByIds(ContextualPublishedCache cache, params string[] ids) { var dNull = new DynamicNull(); var nodes = ids.Select(eachId => DocumentById(eachId, cache, dNull)) @@ -845,7 +845,7 @@ namespace Umbraco.Web searcher = Examine.ExamineManager.Instance.SearchProviderCollection[searchProvider]; var results = searcher.Search(term, useWildCards); - return results.ConvertSearchResultToPublishedContent(PublishedContentCacheResolver.Current.PublishedContentCache); + return results.ConvertSearchResultToPublishedContent(_umbracoContext.ContentCache); } /// @@ -861,7 +861,7 @@ namespace Umbraco.Web s = searchProvider; var results = s.Search(criteria); - return results.ConvertSearchResultToPublishedContent(PublishedContentCacheResolver.Current.PublishedContentCache); + return results.ConvertSearchResultToPublishedContent(_umbracoContext.ContentCache); } #endregion diff --git a/src/Umbraco.Web/UmbracoModule.cs b/src/Umbraco.Web/UmbracoModule.cs index 36ed373b70..10ed48957d 100644 --- a/src/Umbraco.Web/UmbracoModule.cs +++ b/src/Umbraco.Web/UmbracoModule.cs @@ -317,7 +317,7 @@ namespace Umbraco.Web // if yes, return true private static bool EnsureHasContent(UmbracoContext context, HttpContextBase httpContext) { - var store = context.RoutingContext.UmbracoContext.ContentCache; + var store = context.ContentCache; if (store.HasContent(context)) return true; diff --git a/src/Umbraco.Web/umbraco.presentation/library.cs b/src/Umbraco.Web/umbraco.presentation/library.cs index a5b45db945..05be52da1d 100644 --- a/src/Umbraco.Web/umbraco.presentation/library.cs +++ b/src/Umbraco.Web/umbraco.presentation/library.cs @@ -327,9 +327,7 @@ namespace umbraco /// Returns a string with the data from the given element of a node public static string GetItem(int nodeID, String alias) { - var doc = PublishedContentCacheResolver.Current.PublishedContentCache.GetById( - Umbraco.Web.UmbracoContext.Current, - nodeID); + var doc = Umbraco.Web.UmbracoContext.Current.ContentCache.GetById(nodeID); if (doc == null) return string.Empty; diff --git a/src/Umbraco.Web/umbraco.presentation/umbraco/templateControls/ItemRenderer.cs b/src/Umbraco.Web/umbraco.presentation/umbraco/templateControls/ItemRenderer.cs index cbf15fa7f2..54803dd96d 100644 --- a/src/Umbraco.Web/umbraco.presentation/umbraco/templateControls/ItemRenderer.cs +++ b/src/Umbraco.Web/umbraco.presentation/umbraco/templateControls/ItemRenderer.cs @@ -103,9 +103,7 @@ namespace umbraco.presentation.templateControls //moved the following from the catch block up as this will allow fallback options alt text etc to work //get the publishedcontent item - var publishedContent = PublishedContentCacheResolver.Current.PublishedContentCache.GetById( - Umbraco.Web.UmbracoContext.Current, - tempNodeId.Value); + var publishedContent = Umbraco.Web.UmbracoContext.Current.ContentCache.GetById(tempNodeId.Value); var itemPage = new page(publishedContent); tempElementContent = new item(publishedContent, itemPage.Elements, item.LegacyAttributes).FieldContent;