In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to build a non-blocking cache in Guava cache". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to build a non-blocking cache in Guava cache.
Guava cache builds a non-blocking cache for scenarios:
Guava cache is a local cache and is implemented based on JDK ConcurrentHashMap. It is not suitable for use in a distributed environment (unless its node cache consistency can be guaranteed, such as: open source J2cache in the open source Chinese community). If the scenario is simple and there is no need to use the cache complexity feature, you can use ConcurrentHashMap, which is more efficient and direct than Guava cache.
Construction method:
There are three ways to build: CacheLoader,Callable,Inserted Directly.
CacheLoader:
Suitable for having some reasonable default functions to load or calculate key-related values
Callable:
If you need to override the default value, but still need atomic "get-if-absent-compute" semantics, you should use the get method to pass the Callable interface. Implemented: if cached, return; otherwise create, cache and return.
Inserted Directly:
Insert values directly using put.
CacheLoader builds a non-blocking cache:
/ * * Custom refresh cache thread pool * /
Private static ListeningExecutorService backgroundRefreshPools =
MoreExecutors.listeningDecorator (executorService)
/ * * create cache * /
Public static final LoadingCache cache = CacheBuilder.newBuilder ()
.refreshAfterWrite (100no TimeUnit.MILLISECONDS)
.build (new CacheLoader () {)
@ Override
Public String load (String key) {
Return getNewValue ()
}
@ Override
Public ListenableFuture reload (String key,String oldValue) {
Return backgroundRefreshPools.submit (ReloadCache::getNewValue)
}
});
Source code:
Public ListenableFuture loadFuture (K key, CacheLoader)
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.