Disable @Cacheable for a certain Hybris controller

65 views Asked by At

We have a scenario where we need to update userPriceGroup using an admin account. One product in our site can have multiple prices depending on user tier. The problem happens when we call this endpoint of ProductsController, /{baseSiteId}/products/{productCode}. It does not return the correct price immediately after changing the tier. We noticed there's a slight delay when returning the correct response. We then reviewed the controller again and noticed the @Cacheable annotation. Is there a way on our backend side to disable below code? Do we need to override this controller and in that child controller, remove the @Cacheable? Thanks in advance to anyone who can provide their knowledge in our predicament.

@CacheControl(directive = CacheControlDirective.PRIVATE, maxAge = 120)
    @Cacheable(value = "productCache", key = "T(de.hybris.platform.commercewebservicescommons.cache.CommerceCacheKeyGenerator).generateKey(true,true,#productCode,#fields)")

Scenario: Admin changes tier from Bronze to Gold

  • Bronze price: 582
  • Gold price: 552
  • Expected after tier update: 552
  • Actual after tier update: 582
1

There are 1 answers

0
Martin Ackermann On

In general I would suggest to not remove this annotation or the caching mechanism, as this may cost you some performance.

However, looking into the "CommerceCacheKeyGenerator" you can see that it loads the bean "commerceCacheKeyGenerator" from application context and invokes the method "generate".

I think you can create an own implementation of this bean, and ensure that your custom "user tier" is part of the cache key (e.g. override the #addUser method)

In this way the cache would only be invalidated when the tier changes, but you still benefit can benefit from cache usage in all other cases.