HotcakesCommerce / hotcakes-commerce-core

The core of the e-commerce part of the overall solution. This is an ecommerce shopping cart solution built on top of the DNN (DotNetNuke) CMS. Anyone can do commerce online now!
https://mmmcommerce.com
MIT License
69 stars 55 forks source link

Update FindManyWithCache Method #414

Closed ArielBlanco1990 closed 1 year ago

ArielBlanco1990 commented 2 years ago

Optimization of the FindManyWithCache method.

Reduction of multiple traversals via foreach to input parameters. A reduction to only one was made.

The call to the FindMany method was changed by a call to the Find method that already inside it adds the product to the cache storage

ArielBlanco1990 commented 1 year ago

When the main view of the store is called, it goes to the “Index” action inside the CategoryController.cs Controller, inside of which the “LoadProducts” method is called

CategoryControllerindex

Actions are executed within it, one of them calls the "FindManyWithCache" method, which is in charge of searching if the products shown in the view are stored in the cache, if not, it stores them and loads them from it.

FindManyWithCacheOld

In the current code, this method makes two foreach passes to the input parameter list, the first to store the products separately in 2 different lists:

• cacheProducts (products that are already cached) • needRetrieval (products that are not cached)

then the "FindMany" method is called, passing the list of products (string list > Bvin) that are not in the cache as a parameter, to obtain a list of objects that is traversed once more by means of a forearch and stores them in the cache. The list of input parameters is then traversed one more time to store the cache data in a list of products and finally return the cache.

I've made an enhanced update to this code to kick start the performance optimizations.

FindManyWithCacheUpdate

In this update I have reduced the amount of traversals through the foreach methods that were made to the same input list in the previous code, in addition to eliminating the list of products that were not stored in the cache since it is possible to store them using the "Find" method

FindMethod

Which, as shown in the image above, automatically adds that searched product to the cache, something else we save doing, and finally we would only have 1 unique list of products that are all already stored and we can return as a result. I have done several tests and the code works perfectly. It is a small improvement.

Testing with the dotMemory tool on test solution with the optimized method and with the method that existed before. It could notice a small improvement when calling the "LoadProducts" method with the optimized method a memory consumption of 5.63 MB was obtained and with the previous one the memory consumption was 7.83 MB. It's not a significant optimization improvement, but maybe with a slightly longer time test we can see a bigger improvement. With the update of the method we do not avoid unnecessary queries that in the end it is memory consumption that we save.

ArielBlanco1990 commented 1 year ago

The 2 tests performed on 2 HCC solutions, one with the existing method and one with the updated method, were documented.

The solution was tested using the existing method and the new optimized method to verify that the status of the results was the same. I have separated this test into several sections.

  1. Same input parameters in both cases.

  2. Output Results

The results obtained were the same for both tests. For 9 input parameters 9 results, in both cases the same.

  1. Test products that are not in the cache.

In the currently existing method in HCC, we have a list of strings where the Bvins of products are stored that are not cached during the query. Clicking on page 2 of products will detect 9 products which will then be added to the cache.

The updated method detected these 9 products that were not in the cache and added them without problem as expected.

  1. Results Obtained on Page 2 of Products.

The results obtained during the test of page 2 of Products are the same in the 2 methods, that the 9 products are already added to the cache storage and are being sent in the final result correctly.

With these tests it was possible to observe that the results obtained in the updated method correspond 100% to those of the method that HCC currently has. There are no data losses. This test was documented in just 2 pages, but many others were also performed with satisfactory results.