In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use the Java local cache tool LoadingCache". In the daily operation, I believe many people have doubts about how to use the Java local cache tool LoadingCache. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the Java local cache tool LoadingCache". Next, please follow the editor to study!
Environmental dependence
Add maven dependencies first
Com.google.guava guava 30.1.1-jre cn.hutool hutool-all 5.5.2 org.projectlombok lombok true Code
No nonsense, it's on the code.
Package ai.guiji.csdn.tools; import cn.hutool.core.thread.ThreadUtil;import com.google.common.cache.*;import lombok.extern.slf4j.Slf4j; import java.text.MessageFormat;import java.util.Map;import java.util.concurrent.TimeUnit;import java.util.function.Consumer;import java.util.function.Function;import java.util.stream.LongStream / * * @ Author swordsman A Liang _ ALiang @ Date 17:57 2021-12-30 @ Description: caching tool * / @ Slf4jpublic class CacheUtils {private static LoadingCache cache / * initialization cache method * * @ param totleCount cache pool upper limit * @ param overtime timeout * @ param unit time unit * @ param handleNotExist processing does not exist key method * @ param handleRemove remove primary key consumption * / private static void initCache (Integer totleCount, Integer overtime, TimeUnit unit, Function handleNotExist) Consumer handleRemove) {cache = CacheBuilder.newBuilder () / / cache pool size .maximumSize (totleCount) / / if the time object is not read / write accessed, the object is deleted from memory. InherreAfterWrite (overtime) Unit) / / remove listeners .removalListener (new RemovalListener () {@ Override public void onRemoval (RemovalNotification rn) {handleRemove.accept (rn.getKey () }}) .recordStats () .build (new CacheLoader () {@ Override public String load (Long aLong) throws Exception {return handleNotExist.apply (aLong);}}) Log.info ("initialize cache"); * * @ param key key * @ param value value * / public static void put (Long key, String value) {try {log.info ("cache deposit: [{}]-[{}]", key, value); cache.put (key, value) } catch (Exception exception) {log.error ("save cache exception", exception);}} / * * bulk cache * * @ param map mapping * / public static void putMap (Map map) {try {log.info ("bulk cache save: [{}]", map); cache.putAll (map) } catch (Exception exception) {log.error ("bulk cache exception", exception);}} / * get cache * * @ param key key * / public static String get (Long key) {try {return cache.get (key);} catch (Exception exception) {log.error ("get cache exception", exception); return null }} / * delete cache * * @ param key key * / public static void removeKey (Long key) {try {cache.invalidate (key);} catch (Exception exception) {log.error ("remove cache exception", exception) }} / * bulk delete cache * * @ param keys key * / public static void removeAll (Iterable keys) {try {cache.invalidateAll (keys);} catch (Exception exception) {log.error ("bulk delete cache exception", exception);}} / * * Clean cache * / public static void clear () {try {cache.invalidateAll () } catch (Exception exception) {log.error ("Clean cache exception", exception);}} / * get cache size * * @ return length * / public static long size () {return cache.size () } public static void main (String [] args) {initCache (Integer.MAX_VALUE, 10, TimeUnit.SECONDS, k-> {log.info ("cache: [{}], does not exist", k); return ";}, x-> log.info (" cache: [{}], removed ", x) System.out.println (size ()); LongStream.range (0,10) .forEach (a-> put (a, MessageFormat.format ("tt- {0}", a)); System.out.println (cache.asMap ()); ThreadUtil.sleep (5000); LongStream.range (0,10) .forEach (a-> {System.out.println (get (a)) ThreadUtil.sleep (1000);}); System.out.println (cache.asMap ()); ThreadUtil.sleep (10000); System.out.println (cache.asMap ());}}
Code description
1. When initializing loadingCache, you can add the maximum number of caches, elapsed time, elapsed or removed listening events, no key handling, and so on. In the above code, I initialize the cache size to the maximum value of Integer and disappear after 10 seconds of writing, such as there is no key return empty string, and so on.
2. This class also provides put, putAll, get, remove, removeAll, clear, size methods, which can save, retrieve, delete, clean and size the cache.
3. In the main demonstration method, 10 pieces of data are first stored in the cache, then one data per second is fetched after 5 seconds, and all the contents of the cache are printed.
4. Add that LoadingCache is thread safe.
Show me.
53.495 [main] INFO ai.guiji.csdn.tools.CacheUtils-initialize cache
0
53.502 [main] INFO ai.guiji.csdn.tools.CacheUtils-cache deposit: [0]-[tt-0]
53.508 [main] INFO ai.guiji.csdn.tools.CacheUtils-cache deposit: [1]-[tt-1]
53.508 [main] INFO ai.guiji.csdn.tools.CacheUtils-cache deposit: [2]-[tt-2]
53.508 [main] INFO ai.guiji.csdn.tools.CacheUtils-cache deposit: [3]-[tt-3]
53.508 [main] INFO ai.guiji.csdn.tools.CacheUtils-cache deposit: [4]-[tt-4]
53.508 [main] INFO ai.guiji.csdn.tools.CacheUtils-cache deposit: [5]-[tt-5]
53.508 [main] INFO ai.guiji.csdn.tools.CacheUtils-cache deposit: [6]-[tt-6]
53.508 [main] INFO ai.guiji.csdn.tools.CacheUtils-cache deposit: [7]-[tt-7]
53.509 [main] INFO ai.guiji.csdn.tools.CacheUtils-cache deposit: [8]-[tt-8]
53.509 [main] INFO ai.guiji.csdn.tools.CacheUtils-cache deposit: [9]-[tt-9]
{6=tt-6, 5=tt-5, 0=tt-0, 8=tt-8, 7=tt-7, 2=tt-2, 1=tt-1, 9=tt-9, 3=tt-3, 4=tt-4}
Tt-0
Tt-1
Tt-2
Tt-3
Tt-4
15Plus 32Drex 03.572 [main] INFO ai.guiji.csdn.tools.CacheUtils-cache: [5], removed
15Plus 32Drex 03.573 [main] INFO ai.guiji.csdn.tools.CacheUtils-cache: [6], removed
15Plus 32Drex 03.573 [main] INFO ai.guiji.csdn.tools.CacheUtils-cache: [5], does not exist
15Plus 32Suzhou 04.581 [main] INFO ai.guiji.csdn.tools.CacheUtils-Cache: [6], does not exist
15Plus 32 main 05.589 [main] cache: [0], has been removed
15Plus 32 main 05.589 [main] cache: [7], has been removed
15Plus 32 main 05.589 [main] cache: [8], has been removed
15Plus 32VR 05.589 [main] INFO ai.guiji.csdn.tools.CacheUtils-Cache: [7], does not exist
15Plus 32VR 06.589 [main] INFO ai.guiji.csdn.tools.CacheUtils-Cache: [8], does not exist
15Plus 32VR 07.591 [main] INFO ai.guiji.csdn.tools.CacheUtils-cache: [1], has been removed
15Plus 32VR 07.591 [main] INFO ai.guiji.csdn.tools.CacheUtils-cache: [2], has been removed
15Plus 32VR 07.591 [main] INFO ai.guiji.csdn.tools.CacheUtils-cache: [9], has been removed
15Plus 32VR 07.591 [main] INFO ai.guiji.csdn.tools.CacheUtils-Cache: [9], does not exist
{6, 5, 8, 7, 9 =}
{}
Process finished with exit code 0
As you can see, the following 5-9 no longer has a corresponding value in memory.
At this point, the study on "how to use the Java local caching tool LoadingCache" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.