Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

perf(catalog): assemble the category tree outside the cache (#769)

Krzysztof Pajakaa008ff08fd1
diff --git a/src/Business/Grand.Business.Catalog/Services/Categories/CategoryService.cs b/src/Business/Grand.Business.Catalog/Services/Categories/CategoryService.cs
index 59f3b4f20..92dcb5cc8 100644
--- a/src/Business/Grand.Business.Catalog/Services/Categories/CategoryService.cs
+++ b/src/Business/Grand.Business.Catalog/Services/Categories/CategoryService.cs
@@ -158,10 +158,28 @@ public class CategoryService : ICategoryService
/// <returns>Categories</returns>
public virtual async Task<IList<Category>> GetAllCategoriesByParentCategoryId(string parentCategoryId = "",
bool showHidden = false, bool includeAllLevels = false)
+ {
+ var categories = await GetChildCategories(parentCategoryId, showHidden);
+ if (!includeAllLevels) return categories;
+
+ //walk the tree outside the cache: every level resolves through its own cache entry, so no
+ //semaphore is held while the next level is fetched, and a subtree is stored once rather than
+ //once per ancestor
+ var allLevels = new List<Category>(categories);
+ foreach (var category in categories)
+ allLevels.AddRange(await GetAllCategoriesByParentCategoryId(category.Id, showHidden, true));
+
+ return allLevels;
+ }
+
+ /// <summary>
+ /// Gets the direct children of a category, filtered by the current customer's groups and store
+ /// </summary>
+ private async Task<IList<Category>> GetChildCategories(string parentCategoryId, bool showHidden)
{
var key = string.Format(CacheKey.CATEGORIES_BY_PARENT_CATEGORY_ID_KEY, parentCategoryId, showHidden,
- string.Join(",", CurrentCustomer.GetCustomerGroupIds()), CurrentStore.Id, includeAllLevels);
- return await _cacheBase.GetAsync(key, async () =>
+ string.Join(",", CurrentCustomer.GetCustomerGroupIds()), CurrentStore.Id);
+ return await _cacheBase.GetAsync(key, () =>
{
var query = _categoryRepository.Table.Where(c => c.ParentCategoryId == parentCategoryId);
if (!showHidden)
@@ -185,14 +203,7 @@ public class CategoryService : ICategoryService
select p;
}
- var categories = query.OrderBy(x => x.DisplayOrder).ToList();
- if (!includeAllLevels) return categories;
- var childCategories = new List<Category>();
- //add child levels
- foreach (var category in categories)
- childCategories.AddRange(await GetAllCategoriesByParentCategoryId(category.Id, showHidden, true));
- categories.AddRange(childCategories);
- return categories;
+ return Task.FromResult<IList<Category>>(query.OrderBy(x => x.DisplayOrder).ToList());
});
}
diff --git a/src/Core/Grand.Infrastructure/Caching/Constants/CategoryCacheKey.cs b/src/Core/Grand.Infrastructure/Caching/Constants/CategoryCacheKey.cs
index cd19dfa4d..f2708c945 100644
--- a/src/Core/Grand.Infrastructure/Caching/Constants/CategoryCacheKey.cs
+++ b/src/Core/Grand.Infrastructure/Caching/Constants/CategoryCacheKey.cs
@@ -25,7 +25,7 @@ public static partial class CacheKey
/// {3} : store ID
/// {4} : include all levels (child)
/// </remarks>
- public static string CATEGORIES_BY_PARENT_CATEGORY_ID_KEY => "Grand.category.byparent-{0}-{1}-{2}-{3}-{4}";
+ public static string CATEGORIES_BY_PARENT_CATEGORY_ID_KEY => "Grand.category.byparent-{0}-{1}-{2}-{3}";
/// <summary>
/// Key pattern to clear cache
diff --git a/src/Tests/Grand.Business.Catalog.Tests/Services/Categories/CategoryServiceTreeTests.cs b/src/Tests/Grand.Business.Catalog.Tests/Services/Categories/CategoryServiceTreeTests.cs
new file mode 100644
index 000000000..49c552eaf
--- /dev/null
+++ b/src/Tests/Grand.Business.Catalog.Tests/Services/Categories/CategoryServiceTreeTests.cs
@@ -0,0 +1,92 @@
+using Grand.Business.Catalog.Services.Categories;
+using Grand.Business.Common.Services.Security;
+using Grand.Data;
+using Grand.Data.Tests.MongoDb;
+using Grand.Domain.Catalog;
+using Grand.Domain.Customers;
+using Grand.Domain.Stores;
+using Grand.Infrastructure;
+using Grand.Infrastructure.Caching;
+using Grand.Infrastructure.Configuration;
+using Grand.Infrastructure.Tests.Caching;
+using Grand.Mediator;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using Moq;
+
+namespace Grand.Business.Catalog.Tests.Services.Categories;
+
+/// <summary>
+/// The tree is assembled outside the cache, one level per entry. These tests pin the two
+/// properties that depends on: the order callers already relied on, and the fact that the
+/// assembled list is the caller's own rather than a shared cache entry.
+/// </summary>
+[TestClass]
+public class CategoryServiceTreeTests
+{
+ private CategoryService _categoryService;
+ private IRepository<Category> _categoryRepository;
+
+ [TestInitialize]
+ public void InitializeTests()
+ {
+ _categoryRepository = new MongoDBRepositoryTest<Category>();
+ //one customer for the whole fixture: a fresh instance per access would give every call its
+ //own id, hence its own cache key, and the caching under test would never be exercised
+ var customer = new Customer { Id = "customer" };
+ var contextAccessor = new Mock<IContextAccessor>();
+ contextAccessor.Setup(c => c.StoreContext.CurrentStore).Returns(() => new Store { Id = "store" });
+ contextAccessor.Setup(c => c.WorkContext.CurrentCustomer).Returns(() => customer);
+ var mediator = new Mock<IMediator>();
+ var cacheBase = new MemoryCacheBase(MemoryCacheTest.Get(), mediator.Object,
+ new CacheConfig { DefaultCacheTimeMinutes = 1 });
+ _categoryService = new CategoryService(cacheBase, _categoryRepository, contextAccessor.Object,
+ mediator.Object, new AclService(new AccessControlConfig()), new AccessControlConfig());
+ }
+
+ /// <summary>
+ /// root -> a -> a1, root -> b -> b1
+ /// </summary>
+ private async Task GivenATwoLevelTree()
+ {
+ await _categoryService.InsertCategory(new Category { Id = "a", ParentCategoryId = "root", Published = true, DisplayOrder = 1 });
+ await _categoryService.InsertCategory(new Category { Id = "b", ParentCategoryId = "root", Published = true, DisplayOrder = 2 });
+ await _categoryService.InsertCategory(new Category { Id = "a1", ParentCategoryId = "a", Published = true });
+ await _categoryService.InsertCategory(new Category { Id = "b1", ParentCategoryId = "b", Published = true });
+ }
+
+ [TestMethod]
+ public async Task AllLevels_KeepsTheWholeLevelBeforeEachSubtree()
+ {
+ await GivenATwoLevelTree();
+
+ var result = await _categoryService.GetAllCategoriesByParentCategoryId("root", includeAllLevels: true);
+
+ CollectionAssert.AreEqual(new[] { "a", "b", "a1", "b1" }, result.Select(x => x.Id).ToArray());
+ }
+
+ [TestMethod]
+ public async Task WithoutAllLevels_ReturnsOnlyTheDirectChildren()
+ {
+ await GivenATwoLevelTree();
+
+ var result = await _categoryService.GetAllCategoriesByParentCategoryId("root");
+
+ CollectionAssert.AreEqual(new[] { "a", "b" }, result.Select(x => x.Id).ToArray());
+ }
+
+ /// <summary>
+ /// The assembled list is built per call, so a caller that mutates it cannot corrupt what the
+ /// next caller sees.
+ /// </summary>
+ [TestMethod]
+ public async Task AllLevels_HandsBackAListTheCallerOwns()
+ {
+ await GivenATwoLevelTree();
+
+ var first = await _categoryService.GetAllCategoriesByParentCategoryId("root", includeAllLevels: true);
+ first.Clear();
+ var second = await _categoryService.GetAllCategoriesByParentCategoryId("root", includeAllLevels: true);
+
+ Assert.HasCount(4, second);
+ }
+}