mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-26 12:03:16 +08:00
Added dictionaries to memorize results of queries and avoid repeating them (#8690)
within a request.
This commit is contained in:
committed by
GitHub
parent
e55dbb0db8
commit
a215e606b7
@@ -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<int, IEnumerable<MenuPart>>();
|
||||
}
|
||||
|
||||
// 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<int, IEnumerable<MenuPart>> _menuPartsMemory;
|
||||
|
||||
public void GetMenu(IContent menu, NavigationBuilder builder) {
|
||||
var menuParts = _contentManager
|
||||
.Query<MenuPart, MenuPartRecord>()
|
||||
.Where(x => x.MenuId == menu.Id)
|
||||
.List();
|
||||
if (!_menuPartsMemory.ContainsKey(menu.Id)) {
|
||||
_menuPartsMemory[menu.Id] = _contentManager
|
||||
.Query<MenuPart, MenuPartRecord>()
|
||||
.Where(x => x.MenuId == menu.Id)
|
||||
.List();
|
||||
}
|
||||
var menuParts = _menuPartsMemory[menu.Id];
|
||||
|
||||
foreach (var menuPart in menuParts) {
|
||||
if (menuPart != null) {
|
||||
|
||||
@@ -22,8 +22,16 @@ namespace Orchard.Taxonomies.Navigation {
|
||||
ITaxonomyService taxonomyService) {
|
||||
_contentManager = contentManager;
|
||||
_taxonomyService = taxonomyService;
|
||||
|
||||
_termsMemory = new Dictionary<string, TermPart[]>();
|
||||
}
|
||||
|
||||
// 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<string, TermPart[]> _termsMemory;
|
||||
|
||||
public IEnumerable<MenuItem> Filter(IEnumerable<MenuItem> 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()
|
||||
|
||||
Reference in New Issue
Block a user