Every online store owner wants an online store that works quickly and efficiently, and as you may know, these factors directly affect conversions and customer satisfaction.
The Adobe Magento Open Source platform uses a caching system that makes pages load faster while reducing the server load. Magento Open Source caches product pages, category pages and CMS pages. These pages look the same for every user who visits the store, and their content, after being generated by the server, is stored in memory. In this way, subsequent visitors receive previously saved content (content is not generated natively each time for every user).
Magento Open Source mechanisms automatically remove the cache from memory if the content changes, e.g., by editing the product or when the product is sold out.
The initial situation
Category pages in Magento Open Source are among the most frequently visited pages. The cache of these pages is refreshed every time a product is sold out, meaning the product is no longer shown or is marked as unavailable.
The Alekseon development team noticed that the category cache in Magento Open Source is also refreshed when a product that is not visible individually, but only represents a configurable product option (e.g., size). In this case, the cache of all categories containing a configurable product is flushed, and in the case of many pages, it does not affect the content of category pages.
In practice, it turns out that the cache of category pages is flushed often, which means that customers end up on a category page without a built cache. Such pages open slower and cause an increased server load.
What we have changed
We have implemented a patch that stops the category cache refresh when the stock status of a product that is not visible individually has changed. This solution has noticeably improved performance and page loading speed for our clients.
Who will benefit most from it?
The beneficiaries of this patch are stores that:
- use products that are not shown individually as options for configurable products
- have categories with many products (SKUs)
- have products with a small amount of stock
- have high traffic and frequent sales
- experience frequent stock-outs
A “must-know” before implementation: A side effect of the patch
Unfortunately, in some cases our solution leads to the following consequences:
- the list of category filters may be outdated if their values depend on products that are not visible individually, e.g., when we have a filter of available sizes. (This does not affect the filter result)
- if we display product options on a category page, they may be out of date. (The product page is then correct)
How to install the patch
Our patch is free and available on GitHub.
Once installed, you need to enable it in your Magento configuration:
stores -> Configuration -> Catalog -> Cache -> Refresh the Category Cache only if stock status changed for visible products
Technical Notes
We override the native Magento 2 class by a preference in di.xml:
Magento\CatalogInventory\Model\Indexer\Stock\CacheCleaner
Our goal was to modify the $productIds parameter passed to the
getCategoryIdsByProductIds($productIds) w metodzie clean(...):
$productIdsForCategoryCacheClean = $this->getProductIdsForCategoryCacheClean($productStatusesBefore, $productStatusesAfter); $categoryIds = $this->getCategoryIdsByProductIds($productIdsForCategoryCacheClean);
The native CacheCleaner class has the following private methods that we had to copy, but they don't contain any of our changes:
private function getProductStockStatuses(array $productIds) private function getProductIdsForCacheClean(array $productStatusesBefore, array $productStatusesAfter) private function getCategoryIdsByProductIds(array $productIds) private function getConnection()