Skip to content

Commit d01c869

Browse files
committed
#2168 Performance optimization. Categories in top menu and category navigation block can use the same model (cachable). Hence loaded only once.
1 parent 593db35 commit d01c869

File tree

2 files changed

+31
-43
lines changed

2 files changed

+31
-43
lines changed

src/Presentation/Nop.Web/Factories/CatalogModelFactory.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -306,14 +306,28 @@ protected virtual List<int> GetChildCategoryIds(int parentCategoryId)
306306
});
307307
}
308308

309+
/// <summary>
310+
/// Prepare category (simple) models
311+
/// </summary>
312+
/// <returns>Categories</returns>
313+
protected virtual List<CategorySimpleModel> PrepareCategorySimpleModels()
314+
{
315+
//load and cache them
316+
string cacheKey = string.Format(ModelCacheEventConsumer.CATEGORY_ALL_MODEL_KEY,
317+
_workContext.WorkingLanguage.Id,
318+
string.Join(",", _workContext.CurrentCustomer.GetCustomerRoleIds()),
319+
_storeContext.CurrentStore.Id);
320+
return _cacheManager.Get(cacheKey, () => PrepareCategorySimpleModels(0));
321+
}
322+
309323
/// <summary>
310324
/// Prepare category (simple) models
311325
/// </summary>
312326
/// <param name="rootCategoryId">Root category identifier</param>
313327
/// <param name="loadSubCategories">A value indicating whether subcategories should be loaded</param>
314328
/// <param name="allCategories">All available categories; pass null to load them internally</param>
315329
/// <returns>Category models</returns>
316-
protected virtual IList<CategorySimpleModel> PrepareCategorySimpleModels(int rootCategoryId,
330+
protected virtual List<CategorySimpleModel> PrepareCategorySimpleModels(int rootCategoryId,
317331
bool loadSubCategories = true,IList<Category> allCategories = null)
318332
{
319333
var result = new List<CategorySimpleModel>();
@@ -373,7 +387,7 @@ protected virtual IList<CategorySimpleModel> PrepareCategorySimpleModels(int roo
373387
}
374388

375389
#endregion
376-
390+
377391
#region Categories
378392

379393
public virtual CategoryModel PrepareCategoryModel(Category category, CatalogPagingFilteringModel command)
@@ -594,16 +608,11 @@ public virtual CategoryNavigationModel PrepareCategoryNavigationModel(int curren
594608
activeCategoryId = productCategories[0].CategoryId;
595609
}
596610

597-
string cacheKey = string.Format(ModelCacheEventConsumer.CATEGORY_NAVIGATION_MODEL_KEY,
598-
_workContext.WorkingLanguage.Id,
599-
string.Join(",", _workContext.CurrentCustomer.GetCustomerRoleIds()),
600-
_storeContext.CurrentStore.Id);
601-
var cachedModel = _cacheManager.Get(cacheKey, () => PrepareCategorySimpleModels(0).ToList());
602-
611+
var cachedCategoriesModel = PrepareCategorySimpleModels();
603612
var model = new CategoryNavigationModel
604613
{
605614
CurrentCategoryId = activeCategoryId,
606-
Categories = cachedModel
615+
Categories = cachedCategoriesModel
607616
};
608617

609618
return model;
@@ -612,11 +621,7 @@ public virtual CategoryNavigationModel PrepareCategoryNavigationModel(int curren
612621
public virtual TopMenuModel PrepareTopMenuModel()
613622
{
614623
//categories
615-
string categoryCacheKey = string.Format(ModelCacheEventConsumer.CATEGORY_MENU_MODEL_KEY,
616-
_workContext.WorkingLanguage.Id,
617-
string.Join(",", _workContext.CurrentCustomer.GetCustomerRoleIds()),
618-
_storeContext.CurrentStore.Id);
619-
var cachedCategoriesModel = _cacheManager.Get(categoryCacheKey, () => PrepareCategorySimpleModels(0));
624+
var cachedCategoriesModel = PrepareCategorySimpleModels();
620625

621626
//top menu topics
622627
string topicCacheKey = string.Format(ModelCacheEventConsumer.TOPIC_TOP_MENU_MODEL_KEY,

src/Presentation/Nop.Web/Infrastructure/Cache/ModelCacheEventConsumer.cs

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -206,26 +206,15 @@ public ModelCacheEventConsumer(CatalogSettings catalogSettings)
206206
public const string MANUFACTURER_HAS_FEATURED_PRODUCTS_PATTERN_KEY_BY_ID = "Nop.pres.manufacturer.hasfeaturedproducts-{0}-";
207207

208208
/// <summary>
209-
/// Key for CategoryNavigationModel caching
209+
/// Key for list of CategorySimpleModel caching
210210
/// </summary>
211211
/// <remarks>
212212
/// {0} : language id
213213
/// {1} : comma separated list of customer roles
214214
/// {2} : current store ID
215215
/// </remarks>
216-
public const string CATEGORY_NAVIGATION_MODEL_KEY = "Nop.pres.category.navigation-{0}-{1}-{2}";
217-
public const string CATEGORY_NAVIGATION_PATTERN_KEY = "Nop.pres.category.navigation";
218-
219-
/// <summary>
220-
/// Key for TopMenuModel caching
221-
/// </summary>
222-
/// <remarks>
223-
/// {0} : language id
224-
/// {1} : comma separated list of customer roles
225-
/// {2} : current store ID
226-
/// </remarks>
227-
public const string CATEGORY_MENU_MODEL_KEY = "Nop.pres.category.menu-{0}-{1}-{2}";
228-
public const string CATEGORY_MENU_PATTERN_KEY = "Nop.pres.category.menu";
216+
public const string CATEGORY_ALL_MODEL_KEY = "Nop.pres.category.all-{0}-{1}-{2}";
217+
public const string CATEGORY_ALL_PATTERN_KEY = "Nop.pres.category.all";
229218

230219
/// <summary>
231220
/// Key for caching
@@ -794,7 +783,7 @@ public void HandleEvent(EntityInserted<Language> eventMessage)
794783
_cacheManager.RemoveByPattern(SPECS_FILTER_PATTERN_KEY);
795784
_cacheManager.RemoveByPattern(TOPIC_PATTERN_KEY);
796785
_cacheManager.RemoveByPattern(PRODUCT_BREADCRUMB_PATTERN_KEY);
797-
_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY);
786+
_cacheManager.RemoveByPattern(CATEGORY_ALL_PATTERN_KEY);
798787
_cacheManager.RemoveByPattern(PRODUCT_MANUFACTURERS_PATTERN_KEY);
799788
_cacheManager.RemoveByPattern(STATEPROVINCES_PATTERN_KEY);
800789
_cacheManager.RemoveByPattern(AVAILABLE_LANGUAGES_PATTERN_KEY);
@@ -809,7 +798,7 @@ public void HandleEvent(EntityUpdated<Language> eventMessage)
809798
_cacheManager.RemoveByPattern(SPECS_FILTER_PATTERN_KEY);
810799
_cacheManager.RemoveByPattern(TOPIC_PATTERN_KEY);
811800
_cacheManager.RemoveByPattern(PRODUCT_BREADCRUMB_PATTERN_KEY);
812-
_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY);
801+
_cacheManager.RemoveByPattern(CATEGORY_ALL_PATTERN_KEY);
813802
_cacheManager.RemoveByPattern(PRODUCT_MANUFACTURERS_PATTERN_KEY);
814803
_cacheManager.RemoveByPattern(STATEPROVINCES_PATTERN_KEY);
815804
_cacheManager.RemoveByPattern(AVAILABLE_LANGUAGES_PATTERN_KEY);
@@ -824,7 +813,7 @@ public void HandleEvent(EntityDeleted<Language> eventMessage)
824813
_cacheManager.RemoveByPattern(SPECS_FILTER_PATTERN_KEY);
825814
_cacheManager.RemoveByPattern(TOPIC_PATTERN_KEY);
826815
_cacheManager.RemoveByPattern(PRODUCT_BREADCRUMB_PATTERN_KEY);
827-
_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY);
816+
_cacheManager.RemoveByPattern(CATEGORY_ALL_PATTERN_KEY);
828817
_cacheManager.RemoveByPattern(PRODUCT_MANUFACTURERS_PATTERN_KEY);
829818
_cacheManager.RemoveByPattern(STATEPROVINCES_PATTERN_KEY);
830819
_cacheManager.RemoveByPattern(AVAILABLE_LANGUAGES_PATTERN_KEY);
@@ -851,8 +840,7 @@ public void HandleEvent(EntityUpdated<Setting> eventMessage)
851840
_cacheManager.RemoveByPattern(PRODUCTTAG_POPULAR_PATTERN_KEY); //depends on CatalogSettings.NumberOfProductTags
852841
_cacheManager.RemoveByPattern(MANUFACTURER_NAVIGATION_PATTERN_KEY); //depends on CatalogSettings.ManufacturersBlockItemsToDisplay
853842
_cacheManager.RemoveByPattern(VENDOR_NAVIGATION_PATTERN_KEY); //depends on VendorSettings.VendorBlockItemsToDisplay
854-
_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY); //depends on CatalogSettings.ShowCategoryProductNumber and CatalogSettings.ShowCategoryProductNumberIncludingSubcategories
855-
_cacheManager.RemoveByPattern(CATEGORY_MENU_PATTERN_KEY); //depends on CatalogSettings.ShowCategoryProductNumber and CatalogSettings.ShowCategoryProductNumberIncludingSubcategories
843+
_cacheManager.RemoveByPattern(CATEGORY_ALL_PATTERN_KEY); //depends on CatalogSettings.ShowCategoryProductNumber and CatalogSettings.ShowCategoryProductNumberIncludingSubcategories
856844
_cacheManager.RemoveByPattern(CATEGORY_NUMBER_OF_PRODUCTS_PATTERN_KEY); //depends on CatalogSettings.ShowCategoryProductNumberIncludingSubcategories
857845
_cacheManager.RemoveByPattern(HOMEPAGE_BESTSELLERS_IDS_PATTERN_KEY); //depends on CatalogSettings.NumberOfBestsellersOnHomepage
858846
_cacheManager.RemoveByPattern(PRODUCTS_ALSO_PURCHASED_IDS_PATTERN_KEY); //depends on CatalogSettings.ProductsAlsoPurchasedNumber
@@ -921,8 +909,7 @@ public void HandleEvent(EntityDeleted<ProductManufacturer> eventMessage)
921909
public void HandleEvent(EntityInserted<Category> eventMessage)
922910
{
923911
_cacheManager.RemoveByPattern(SEARCH_CATEGORIES_PATTERN_KEY);
924-
_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY);
925-
_cacheManager.RemoveByPattern(CATEGORY_MENU_PATTERN_KEY);
912+
_cacheManager.RemoveByPattern(CATEGORY_ALL_PATTERN_KEY);
926913
_cacheManager.RemoveByPattern(CATEGORY_CHILD_IDENTIFIERS_PATTERN_KEY);
927914
_cacheManager.RemoveByPattern(CATEGORY_SUBCATEGORIES_PATTERN_KEY);
928915
_cacheManager.RemoveByPattern(CATEGORY_HOMEPAGE_PATTERN_KEY);
@@ -932,8 +919,7 @@ public void HandleEvent(EntityUpdated<Category> eventMessage)
932919
{
933920
_cacheManager.RemoveByPattern(SEARCH_CATEGORIES_PATTERN_KEY);
934921
_cacheManager.RemoveByPattern(PRODUCT_BREADCRUMB_PATTERN_KEY);
935-
_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY);
936-
_cacheManager.RemoveByPattern(CATEGORY_MENU_PATTERN_KEY);
922+
_cacheManager.RemoveByPattern(CATEGORY_ALL_PATTERN_KEY);
937923
_cacheManager.RemoveByPattern(CATEGORY_CHILD_IDENTIFIERS_PATTERN_KEY);
938924
_cacheManager.RemoveByPattern(CATEGORY_BREADCRUMB_PATTERN_KEY);
939925
_cacheManager.RemoveByPattern(CATEGORY_SUBCATEGORIES_PATTERN_KEY);
@@ -945,8 +931,7 @@ public void HandleEvent(EntityDeleted<Category> eventMessage)
945931
{
946932
_cacheManager.RemoveByPattern(SEARCH_CATEGORIES_PATTERN_KEY);
947933
_cacheManager.RemoveByPattern(PRODUCT_BREADCRUMB_PATTERN_KEY);
948-
_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY);
949-
_cacheManager.RemoveByPattern(CATEGORY_MENU_PATTERN_KEY);
934+
_cacheManager.RemoveByPattern(CATEGORY_ALL_PATTERN_KEY);
950935
_cacheManager.RemoveByPattern(CATEGORY_CHILD_IDENTIFIERS_PATTERN_KEY);
951936
_cacheManager.RemoveByPattern(CATEGORY_BREADCRUMB_PATTERN_KEY);
952937
_cacheManager.RemoveByPattern(CATEGORY_SUBCATEGORIES_PATTERN_KEY);
@@ -962,8 +947,7 @@ public void HandleEvent(EntityInserted<ProductCategory> eventMessage)
962947
{
963948
//depends on CatalogSettings.ShowCategoryProductNumber (when enabled)
964949
//so there's no need to clear this cache in other cases
965-
_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY);
966-
_cacheManager.RemoveByPattern(CATEGORY_MENU_PATTERN_KEY);
950+
_cacheManager.RemoveByPattern(CATEGORY_ALL_PATTERN_KEY);
967951
}
968952
_cacheManager.RemoveByPattern(CATEGORY_NUMBER_OF_PRODUCTS_PATTERN_KEY);
969953
_cacheManager.RemoveByPattern(string.Format(CATEGORY_HAS_FEATURED_PRODUCTS_PATTERN_KEY_BY_ID, eventMessage.Entity.CategoryId));
@@ -981,8 +965,7 @@ public void HandleEvent(EntityDeleted<ProductCategory> eventMessage)
981965
{
982966
//depends on CatalogSettings.ShowCategoryProductNumber (when enabled)
983967
//so there's no need to clear this cache in other cases
984-
_cacheManager.RemoveByPattern(CATEGORY_NAVIGATION_PATTERN_KEY);
985-
_cacheManager.RemoveByPattern(CATEGORY_MENU_PATTERN_KEY);
968+
_cacheManager.RemoveByPattern(CATEGORY_ALL_PATTERN_KEY);
986969
}
987970
_cacheManager.RemoveByPattern(CATEGORY_NUMBER_OF_PRODUCTS_PATTERN_KEY);
988971
_cacheManager.RemoveByPattern(string.Format(CATEGORY_HAS_FEATURED_PRODUCTS_PATTERN_KEY_BY_ID, eventMessage.Entity.CategoryId));

0 commit comments

Comments
 (0)