diff --git a/src/Orchard.Web/Core/Navigation/Services/DefaultMenuProvider.cs b/src/Orchard.Web/Core/Navigation/Services/DefaultMenuProvider.cs index 5a25e62d7..b33ba88da 100644 --- a/src/Orchard.Web/Core/Navigation/Services/DefaultMenuProvider.cs +++ b/src/Orchard.Web/Core/Navigation/Services/DefaultMenuProvider.cs @@ -1,4 +1,5 @@ -using System.Web; +using System.Collections.Generic; +using System.Web; using Orchard.ContentManagement; using Orchard.ContentManagement.Aspects; using Orchard.Core.Navigation.Models; @@ -11,13 +12,22 @@ namespace Orchard.Core.Navigation.Services { public DefaultMenuProvider(IContentManager contentManager) { _contentManager = contentManager; + + _menuPartsMemory = new Dictionary>(); } + // Prevent doing the same query for MenuParts more than once on a same request + // in case we are building the same menu several times. + private Dictionary> _menuPartsMemory; + public void GetMenu(IContent menu, NavigationBuilder builder) { - var menuParts = _contentManager - .Query() - .Where(x => x.MenuId == menu.Id) - .List(); + if (!_menuPartsMemory.ContainsKey(menu.Id)) { + _menuPartsMemory[menu.Id] = _contentManager + .Query() + .Where(x => x.MenuId == menu.Id) + .List(); + } + var menuParts = _menuPartsMemory[menu.Id]; foreach (var menuPart in menuParts) { if (menuPart != null) { diff --git a/src/Orchard.Web/Modules/Orchard.Taxonomies/Navigation/TaxonomyNavigationProvider.cs b/src/Orchard.Web/Modules/Orchard.Taxonomies/Navigation/TaxonomyNavigationProvider.cs index 6ca41931c..681e67095 100644 --- a/src/Orchard.Web/Modules/Orchard.Taxonomies/Navigation/TaxonomyNavigationProvider.cs +++ b/src/Orchard.Web/Modules/Orchard.Taxonomies/Navigation/TaxonomyNavigationProvider.cs @@ -22,8 +22,16 @@ namespace Orchard.Taxonomies.Navigation { ITaxonomyService taxonomyService) { _contentManager = contentManager; _taxonomyService = taxonomyService; + + _termsMemory = new Dictionary(); } + // Prevent doing the same query for terms more than once on a same request + // in case we are building menus from the same starting taxonomies. Key is a + // string to "combine" the Id of the root TermPart and the flag telling to + // add that root to the results. + private Dictionary _termsMemory; + public IEnumerable Filter(IEnumerable items) { foreach (var item in items) { @@ -34,14 +42,21 @@ namespace Orchard.Taxonomies.Navigation { var rootTerm = _taxonomyService.GetTerm(taxonomyNavigationPart.TermId); TermPart[] allTerms; - + string memoryKey; if (rootTerm != null) { - // if DisplayRootTerm is specified add it to the menu items to render - allTerms = _taxonomyService.GetChildren(rootTerm, taxonomyNavigationPart.DisplayRootTerm).ToArray(); + memoryKey = $"{rootTerm.Id}_{taxonomyNavigationPart.DisplayRootTerm}"; + if (!_termsMemory.ContainsKey(memoryKey)) { + // if DisplayRootTerm is specified add it to the menu items to render + _termsMemory[memoryKey] = _taxonomyService.GetChildren(rootTerm, taxonomyNavigationPart.DisplayRootTerm).ToArray(); + } } else { - allTerms = _taxonomyService.GetTerms(taxonomyNavigationPart.TaxonomyId).ToArray(); + memoryKey = taxonomyNavigationPart.TaxonomyId.ToString(); + if (!_termsMemory.ContainsKey(memoryKey)) { + _termsMemory[memoryKey] = _taxonomyService.GetTerms(taxonomyNavigationPart.TaxonomyId).ToArray(); + } } + allTerms = _termsMemory[memoryKey]; var rootLevel = rootTerm != null ? rootTerm.GetLevels()