Catalog Performance Settings - Cache Product Prices

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
3 years ago
Hello,
I was hoping someone could clarify the "Cache product prices" option in the Catalog Performance Settings. There is a note that says "But you should not enable it if you use some complex discounts, discount requirement rules, or coupon codes." Can someone clarify what this is actually warning against? What level of complexity are we talking about here? If volume of discounts a concern?

Thanks!
Frank
3 years ago
The high level basics are that with caching enabled it should check for a cached price and return the first match found, and if no cached price is found only then will it go on to calculate based on formulas and/or check the database.  

If you intend to have different pricing based on customer roles, or categories, or tiers, coupons etc. then there is a chance it will return the incorrect price from a previously cached situation, contrary to your intent for the current customer.  

So if your pricing is the same across the board for everyone in every situation it is safe to cache.  Otherwise, disable that cache setting and let it determine the price per situation.  It may be more nuanced then that, but you'll need to dive in to the caching system to see how it works from the inside.

The first cache exclusion I found was in GetFinalPrice in the PriceCalculationService, but there's also various pricing methods for attributes, variants, discounts, as well as probably a host of other situations.  It may check for cached pricing several layers up or in any number of places, and you would need to step through the code to drill down to all the specifics.  


            var cacheKey = _cacheKeyService.PrepareKeyForDefaultCache(NopCatalogDefaults.ProductPriceCacheKey,
                product,
                overriddenProductPrice,
                additionalCharge,
                includeDiscounts,
                quantity,
                _customerService.GetCustomerRoleIds(customer),
                _storeContext.CurrentStore);

            //we do not cache price if this not allowed by settings or if the product is rental product
            //otherwise, it can cause memory leaks (to store all possible date period combinations)
            if (!_catalogSettings.CacheProductPrices || product.IsRental)
                cacheKey.CacheTime = 0;
3 years ago
Thanks for the reply.

Our findings were similar. We actually have no discounts based on role, tier, etc. All of our discounts are via coupon codes. That being said, I don't think this calculation is useful at all (and actually leads to major performance issues).

Do you (or anyone) know what may invalidate the cache? I don't see anywhere obvious in the codebase where this happens (ex: a product price changes).

Thanks,
Frank
3 years ago
I have yet to dive into the cache inner workings, but I'd start in the ProductCacheEventConsumer and work my way up to everywhere it's called.

CouponCodes get applied in the cart, right?  The cart/order total calculation happens in a different service, so conceivably if your Product prices (almost) never change you could cache them, then coupons get applied to the total later.  

Keep in mind if you update prices from an external service then you'll want to disable and clear the cache beforehand, and I'd have to go see what happens when you edit a Product price manually in the Admin.
3 years ago
Thanks af1racing. All very helpful.

af1racing wrote:
I have yet to dive into the cache inner workings, but I'd start in the ProductCacheEventConsumer and work my way up to everywhere it's called.

CouponCodes get applied in the cart, right?  The cart/order total calculation happens in a different service, so conceivably if your Product prices (almost) never change you could cache them, then coupons get applied to the total later.  

Keep in mind if you update prices from an external service then you'll want to disable and clear the cache beforehand, and I'd have to go see what happens when you edit a Product price manually in the Admin.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.