在Hybra中,@Cacheable
注释用于缓存方法的结果,它可能会影响控制器的响应时间.如果要禁用特定控制器或方法的缓存,可以考虑以下几种方法:
方法1:覆盖控制器
您可以通过扩展现有的ProductsController
来创建自定义控制器,并覆盖使用@Cacheable
的特定方法.在您的自定义控制器中,您可以 Select 不使用@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
如果您想要更细粒度的控制,可以使用@Cacheable
批注中的Spel(Spring表达式语言)来有条件地启用或禁用缓存.
@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
}
在这种方法中,您可以将一个参数(useCache
)传递给该方法,并根据它的值控制缓存.