In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
An introduction
In the scenario of high concurrency, Hystrix provides the function of request cache, which can easily open and use request cache to optimize the system, so as to reduce the consumption of request thread and reduce the request response time. Friends who are willing to understand the source code directly ask for communication and sharing technology: 2147775633
Second, enable the request cache function
When implementing HystrixCommand or HystrixObservableCommand, turn on request caching by overloading the getCacheKey () method.
For example:
Public class CommandUsingRequestCache extends HystrixCommand {private final int value; protected CommandUsingRequestCache (int value) {super (HystrixCommandGroupKey.Factory.asKey ("ExampleGroup")); this.value = value;} @ Override protected Boolean run () {return value = = 0 | | value% 2 = = 0;} / / the request command can be cached through the request cache key value returned in the getCacheKey method. At this point, when different external request / processing logic calls the same dependent service, Hystrix will distinguish whether the request is repeated according to the value returned by the getCacheKey method. / / if their cachekey is the same, then the dependent service value will be called once when the first request arrives, and the other / / request will return the result directly from the request cache. Therefore, enabling caching has the following advantages: / / reduce the number of repeated requests and reduce the concurrency of dependent services / / in the context of the same user request, the returned data of the same dependent service is always consistent. / / the request cache takes effect before the execution of run () and construct (), so it can effectively reduce unnecessary thread overhead. @ Override protected String getCacheKey () {return String.valueOf (value);}}
Third, clean up the invalid cache function
When using the request cache, if it is only a read operation, then there is no need to consider whether the content of the cache is correct, but if there is still an operation to update the data in the request command, then the data in the cache needs to be processed in time during the write operation to prevent the request command of the read operation from getting invalid data.
In Hystrix, you can clean up the cache through the HystrixRequestCache.clear () method.
For example:
/ / after we have implemented the request cache for the GetterCommand command, it is necessary to clean the cache for the SetterCommand command implementation to ensure that after / / prefixStoredOnRemoteDataStore is updated, the result of the same cache in the Hystrix request cache is removed, so that the next time the prefixStoredOnRemoteDataStore is obtained according to id//, the data public class CommandUsingRequestCacheInvalidation {/ * represents a remote data store * / private static volatile String prefixStoredOnRemoteDataStore = "ValueBeforeSet_" will not be obtained from the cache. / / get data according to id public static class GetterCommand extends HystrixCommand {private static final HystrixCommandKey GETTER_KEY = HystrixCommandKey.Factory.asKey ("GetterCommand"); private final int id; public GetterCommand (int id) {super (Setter.withGroupKey (HystrixCommandGroupKey.Factory.asKey ("GetSetGet")) .andCommandKey (GETTER_KEY)); this.id = id } @ Override protected String run () {return prefixStoredOnRemoteDataStore + id;} @ Override protected String getCacheKey () {return String.valueOf (id) } / / this method obtains the instance of the command request cache object HystrixRequestCache from the default Hystrix concurrency strategy according to GETTER_KEY, and then calls the clear method of the request cache object to clean up the cache content whose Key is id. Public static void flushCache (int id) {HystrixRequestCache.getInstance (GETTER_KEY, HystrixConcurrencyStrategyDefault.getInstance ()) .clear (String.valueOf (id));}} / / the value public static class SetterCommand extends HystrixCommand {private final int id; private final String prefix used to update prefixStoredOnRemoteDataStore Public SetterCommand (int id, String prefix) {super (HystrixCommandGroupKey.Factory.asKey ("GetSetGet")); this.id = id; this.prefix = prefix;} @ Override protected Void run () {/ / persist the value against the datastore prefixStoredOnRemoteDataStore = prefix / / after the write prefixStoredOnRemoteDataStore is called, the call to the static method flushCache in GetterCommand / / is added to clean up the aging cache. GetterCommand.flushCache (id); / / no return value return null;}}
The overall code structure is as follows:
Sources of data and source
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.