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
>Solution :
In Hybris, the @Cacheable annotation is used to cache the results of methods, and it might affect the response time of your controller. If you want to disable caching for a specific controller or method, there are a few approaches you can consider:
Approach 1: Override the Controller
You can create a custom controller by extending the existing ProductsController and override the specific method where @Cacheable is used. In your custom controller, you can choose not to use @Cacheable.
@Controller
@RequestMapping(value = "/{baseSiteId}/products")
public class CustomProductsController extends ProductsController {
@Override
@CacheControl(directive = CacheControlDirective.PRIVATE, maxAge = 120)
public ProductData getProductForCodeAndOptions(@PathVariable final String baseSiteId,
@PathVariable final String productCode,
@RequestParam(required = false) final Set<Options> options,
@RequestParam(defaultValue = DEFAULT_FIELD_SET) final String fields) {
// Your custom logic without @Cacheable
}
}
Approach 2: Use SpEL Expression in @Cacheable
If you want more fine-grained control, you can use the SpEL (Spring Expression Language) in the @Cacheable annotation to conditionally enable or disable caching.
@CacheControl(directive = CacheControlDirective.PRIVATE, maxAge = 120)
@Cacheable(value = "productCache", key = "T(de.hybris.platform.commercewebservicescommons.cache.CommerceCacheKeyGenerator).generateKey(true,true,#productCode,#fields)", condition = "#useCache")
public ProductData getProductForCodeAndOptions(@PathVariable final String baseSiteId,
@PathVariable final String productCode,
@RequestParam(required = false) final Set<Options> options,
@RequestParam(defaultValue = DEFAULT_FIELD_SET) final String fields,
@RequestParam(defaultValue = "true") boolean useCache) {
// Your method logic
}
In this approach, you can pass a parameter (useCache) to the method and control caching based on its value.