In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to use Caffeine Cache correctly". In daily operation, I believe many people have doubts about how to use Caffeine Cache correctly. 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 Caffeine Cache correctly". Next, please follow the editor to study!
1. About Caffeine Cache
In the recommendation service, although a small number of requests are allowed to return to the default list due to reasons such as calculation timeout. However, in terms of operational indicators, the higher the "completion rate" means the more complete algorithm results are presented, and it also means higher business returns. (the completion rate is analogous to the completion rate of the video, and the number of requests / total requests calculated by the entire recommended online process are successfully completed.)
In order to complete the calculation as quickly as possible, the multi-level cache scheme has become the standard configuration of recommended online services. Among them, local cache is particularly important, and Caffeine Cache is a high-performance local cache that stands out in recent years. Caffeine Cache has replaced Google Guava as the default caching framework in Spring Boot 2.0, which shows that it is mature and reliable.
There are many introduction articles about Caffeine, which will not be repeated any more. You can read the references at the end of the article to learn about the brief introduction of Caffeine, performance benchmark results, basic API usage, and the principle of Window-TinyLFU caching algorithm. Although I haven't been exposed to Caffeine for a long time, its concise API and silky asynchronous loading ability are simply not very easy to use. And this rookie also stepped on some holes in the process of use, and even the cache can jam as slowly as the disk IO if it is not used properly.
After some study and attempt, I finally understand the secret of the silky smoothness of Caffeine Cache, so I can summarize it and share it.
II. Caffeine Cache configuration routine
Using Caffeine Cache, in addition to the common annotations such as @ EnableCache and @ Cacheable in Spring, it is also a common way for recommendation services to create LoadingCache directly using the Caffeine.newBuilder () .build () method.
Let's first take a look at the configuration routines of Caffeine#builder:
2.1 follow-up questions for three companies 2.1.1 ObjectPool
Of course, barefoot is not afraid to wear shoes, don't go online.
2.1.2 both expireAfterWrite and expireAfterAccess are configured?
Although both expireAfterWrite and expireAfterAccess are configured without error, access includes write, so just pick one.
2.1.3 what are the characteristics of reference-based expulsion?
As long as the configuration uses = = to compare object equality, there is also a very important configuration for equals;, which is also the secret to determining the silky smoothness of the cache: refresh policy refreshAfterWrite. This configuration allows Caffeine to refresh data after it has been loaded for more than a given time. It will be explained in detail below.
Witty as I can tread on Builder.
Unlike lombok's builder, Caffeine#builder 's policy calls twice will result in run-time exceptions! This is because each policy saves the flag bits that have been set when Caffeine is built, so repeating the setting does not override but directly throws an exception:
Public Caffeine maximumWeight (@ NonNegative long maximumWeight) {requireState (this.maximumWeight = = UNSET_INT, "maximum weight was already set to% s", this.maximumWeight); requireState (this.maximumSize = = UNSET_INT, "maximum size was already set to% s", this.maximumSize); this.maximumWeight = maximumWeight; requireArgument (maximumWeight > = 0, "maximum weight must not be negative"); return this;}
For example, in the above code, if maximumWeight () is called twice, it throws an exception and prompts maximum weight was already set to xxx.
What does the get method do with the essence of Caffeine Cache?
First of all, you can see in the implementation class LocalLoadingCache
Default @ Nullable V get (K key) {return cache () .computeIfAbsent (key, mappingFunction ());}
But suddenly found that this get method does not implement the class! Why? We can see the clue by tracking the cache () method:
Public BoundedLocalCache cache () {return cache;} public UnboundedLocalCache cache () {return cache;}
According to the process of calling Caffeine.newBuilder (). Build (), it determines whether BoundedLocalCache or UnboundedLocalCache is generated.
The conditions for determining BoundedLocalCache are as follows:
Public LoadingCache build (@ NonNull 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.
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.