Cache needs to be introduced for product with large variation size

Created on 20 June 2024, 5 months ago
Updated 21 June 2024, 5 months ago

Hello,

We've discovered a performance issue in the loadEnabledVariations method of the ProductVariationAttribtesWidgetCustom class when dealing with a large number of product variations (around 4000+).

The issue seems to be related to the way the method handles caching. When the cache is empty, the method loads all enabled variations for a product and then sets them in the cache. However, when dealing with a large number of variations, this operation can be quite slow and resource-intensive.

Here's the relevant code snippet:

protected function loadEnabledVariations(ProductInterface $product) {
    $cache_key = 'custom_module:enabled_variations:' . $product->id();
    $cacheBackend = \Drupal::service('cache.default');
    $langcode = $product->language()->getId();
    if ($cache = $cacheBackend->get($cache_key)) {
        $variations = $cache->data;
    } else {
        $variations = $this->variationStorage->loadEnabled($product);
        $cacheBackend->set($cache_key, $variations, CacheBackendInterface::CACHE_PERMANENT, ['commerce_product:' . $product->id()]);
    }
    foreach ($variations as $key => $variation) {
        $variations[$key] = $this->entityRepository->getTranslationFromContext($variation, $langcode);
    }
    return $variations;
}

One potential solution could be to implement pagination or some form of lazy loading for the variations, so that only a subset of the variations is loaded and cached at a time. This could significantly reduce the amount of data that needs to be processed and stored in the cache, improving performance.

We would appreciate any feedback or suggestions on how to best address this issue.

Thank you.

Feature request
Status

Closed: won't fix

Version

2.39

Component

Product

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @inteeka-help
  • Status changed to Closed: won't fix 5 months ago
  • 🇮🇱Israel jsacksick

    There's no such thing as a "major" feature request.
    Furthermore, ProductVariationAttribtesWidgetCustom isn't provided by Commerce, this must be a custom widget present in your project.

    The ProductVariationWidgetBase doesn't have caching around the loadEnabledVariations() method. Also there is no way loading 4000 variations at once on a single page can work properly.

    You'd have to write your own widget with your own logic... Not something that can be provided as part of Commerce core...

Production build 0.71.5 2024