hystrix supports cached one request result, and the next request with the same key will directly fetch the result from the cache, reducing request overhead. To use this function, you must manage the HystrixRequestContext. If Request B wants to use the result cache of Request A, A and B must be in the same context. A context can be constructed through HystrixRequestContext.initializeContext() and context.shutdown(). All requests between these two statements are in the same context. Of course, this management process can be implemented through a custom filter. Refer to the previous article //www.VeVB.COM/article/140527.htm
Hystrix request cache annotation
@CacheResult The method added to this annotation will enable request caching. By default, all parameters of the method are used as cache keys, which means that the cache will only be cached if all parameters of the method are consistent.
@Servicepublic class UserCacheService { @Autowired private UserFeignClient userFeignClient; /** * @HystrixCommand's requestCache.enabled can control whether caching is supported* Only when @CacheResult is added can cache, even if requestCache.enabled=true * @param id User id * @return specified user*/ @CacheResult @HystrixCommand(commandProperties = { @HystrixProperty(name="requestCache.enabled",value = "true") }) public User findUserById(Integer id){ return userFeignClient.findUserById(id); }}If requestCache.enabled is set to false, the cache will not work even if @CacheResult is added.
@CacheKey This annotation allows you to specify the cached key
@CacheResult @HystrixCommand(commandProperties = { @HystrixProperty(name="requestCache.enabled",value = "true") }) public User findUserByIdAndName(@CacheKey Integer id,String name){ return userFeignClient.findUserById(id); }In the above code, we modified the id field with @CacheKey, indicating that as long as the id is the same, the cache will be cached by default, and it has nothing to do with the name field. If we specify the cacheKeyMethod property of @CacheResult, the @CacheKey annotation is invalid.
@CacheRemove The function of this annotation is to invalidate the cache
/** * Specify the cache of this method when the findUserById is called through the @CacheRemove annotation * @param id User id * @param name User name * @return Specified user*/ @CacheResult @CacheRemove(commandKey = "findUserById") @HystrixCommand(commandProperties = { @HystrixProperty(name="requestCache.enabled",value = "true") }) public User findUserByIdAndName2(@CacheKey Integer id,String name){ return userFeignClient.findUserById(id); }The above code specifies that the value of the @CacheRemove property commandKey is foundUserById, which means that when findUserById is called, the cache of this method will be deleted.
Please refer to the full version of the code: https://github.com/jingangwang/micro-service
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.