How-To Speed Up Category Item Counts (Tutorial)

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 years ago
Hello all, on my website I have a lot of categories and a lot of items in each.
Well I really wanted to show the "Item Count" next to my categories but it was sooo slow.

So here is my update. Hope it helps:

Description
I am caching the item count only. The "Clear Cache" in the admin section will clear this out if you ever add a new category or new item to increase the count

Update
Open CategoryNavigation.ascx.cs
Go to the GetNumberOfProducts method and change it to this


protected int GetNumberOfProducts(Category category, bool includeSubCategories)
        {
            int numberOfProducts = 0;
            if (Cache.Get("numberOfProducts" + category.CategoryId) == null)
            {
                var products = this.ProductService.GetAllProducts(category.CategoryId,
                        0, 0, null, null, null, string.Empty, false, 1, 0,
                        null, ProductSortingEnum.Position, out numberOfProducts);

                if (includeSubCategories)
                {
                    var subCategories = this.CategoryService.GetAllCategoriesByParentCategoryId(category.CategoryId);
                    foreach (var subCategory in subCategories)
                    {
                        int tmp1 = GetNumberOfProducts(subCategory, includeSubCategories);
                        numberOfProducts += tmp1;
                    }
                }

                Cache.Insert("numberOfProducts" + category.CategoryId, numberOfProducts);
            }
            else
            {
                numberOfProducts = (int)Cache.Get("numberOfProducts" + category.CategoryId);
            }
            
            return numberOfProducts;
        }


Compile and go

End of How To


Hope this helps some people! Let me know!

Cody of CoJoBaby.com
13 years ago
We also thought about it. But it'll not work if you have enabled ACL per category. I suggest to check whether 'ACL per category' is enabled before caching.
13 years ago
where is "ACL per category"?
13 years ago
Go to a category details page (admin area), then 'Access control' tab
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.