当前位置:网站首页>Guava cache usage summary
Guava cache usage summary
2022-06-23 07:44:00 【Xujingfeng】
gossip
The original article has been broken 2 It's been months , It's not because I'm busy , Mainly lazy . But I also feel that there are fewer and fewer technical points to share with you , On the one hand, I have been engaged in some “ Internal projects ” The development of , Even if I want to share , I can't move to the official account & Blog up ; On the one hand, I am not very good at some technical points , When I was a beginner , I dare to write , And after a certain number of years of work , On the contrary, there are some burdens , Would my readers mind ? reasoning , I recalled the original intention of writing , Isn't it just to record your learning process ? So , I still recorded this article according to my previous style of writing , In order to avoid becoming a blogger who breaks the blog .
Here is the text .
Preface
“ cache ” It has always been the kind of technical point that our programmers talk about most , Such as Redis、Encache、Guava Cache, You've heard of at least one . What needs to be acknowledged is that , Whether it's the eight part essay style of interview , Or the frequency of actual use ,Redis Distributed caching is indeed the most popular caching technology at present , But at the same time , From my personal project experience , Local caching is also a common technique .
analysis Redis There are many cached articles , for example Redis An avalanche 、Redis Expiration mechanism, etc , Such titles of official account are not uncommon in my circle of friends timeline in , However, there are few articles analyzing the local cache in my image .
In a recent project , A new colleague used Guava Cache To a person RPC Interface , I am here review Its code just found an unreasonable way to write , So there is this article .
This article will introduce Guava Cache Some common operations of : Basics API Use , Expiration strategy , Refresh strategy . And according to my writing habits , Some summaries of actual development will be attached . What needs to be stated in advance is , I haven't read Guava Cache Source code , The introduction is just some experience or best practices , There won't be too much in-depth parsing .
Just a quick introduction Guava Cache, It is Google Packaged base kit guava A memory cache module in , It mainly provides the following capabilities :
- Encapsulates the process of caching and data source interaction , Make development more focused on business operations
- Provide thread safe access operations ( By analogy ConcurrentHashMap)
- Provide common cache expiration policies , Cache refresh policy
- Provide cache hit rate monitoring
Based on using
Use an example to introduce Guava Cache The basic method of use – Cache the return value of case conversion .
1 | private String fetchValueFromServer(String key) { |
Use Guava Cache The benefits of have jumped onto the paper , It decouples cache access from business operations .CacheLoader Of load Method can be understood as an entry for loading raw data from a data source , When calling LoadingCache Of getUnchecked perhaps get When the method is used ,Guava Cache The behavior is as follows :
- When cache misses , A synchronous invocation load Interface , Load into cache , Return cache value
- A cache hit , Directly return the cached value
- When multithreaded cache misses ,A Threads load when , It will block B Thread request , Until the cache is loaded
be aware ,Guava Two are provided getUnchecked perhaps get Loading method , No big difference , No matter which one you use , All need to pay attention to , Data sources, whether they are RPC The return value of the interface is still the database , Consider access timeout or failure , Do exception handling .
Preload cache
Common usage scenarios for preloading cache :
- The commonplace seckill scene , Pre cache warm up , Add hot items to cache ;
- After system restart , Load the cache in advance , Avoid real requests from crashing the cache
Guava Cache Provides put and putAll Method
1 |
|
Operation and HashMap As like as two peas .
There is a mistake here , And the new colleague just stepped on , It's also my original intention to write this article , Be sure to use only in the scenario of preloading cache put, Any other scenario should use load To trigger the load cache . Look at this Opposite the sample :
1 | // Note that this is a negative example |
This way of writing , stay load Method has a null value set , Subsequently, manually put + get Using the cache , This habit is more like operating a HashMap, But it is not recommended to Cache Use in . I introduced get coordination load By Guava Cache To ensure thread safety , When multiple threads are guaranteed to access the cache , The first request loads the cache at the same time , Blocking subsequent requests , In this way HashMap The usage is neither elegant , In extreme cases, it can also cause cache breakdown 、 Thread safety and so on .
Please make sure that only put Method is used as a preload cache scene .
Cache expiration
The previous introduction is still in use ConcurrentHashMap The category of ,Cache Its first difference is “ Cache expiration ” This scene can be reflected . This section describes Guava Some common cache expiration behaviors and Strategies .
Cache a fixed number of values
1 |
|
Use ConcurrentHashMap One of the biggest problems in caching , That is, we have no simple and effective means to stop its unlimited growth , and Guava Cache Can be initialized by LoadingCache The process of , To configure maximumSize , To ensure that cached content does not cause your system to appear OOM.
It is worth noting that , The test cases I use here are in addition to get 、getUnchecked The third way to get the cache , As the literal meaning describes ,getIfPresent When the cache does not exist , It doesn't trigger load Method to load the data source .
LRU Expiration strategy
Still use the above example , We are setting the capacity to 3 when , Learn only LoadingCache Can be stored 3 It's worth , But I didn't know that 4 After saving values , Which old value needs to be eliminated , Make room for new values . actually ,Guava Cache By default LRU Cache retirement strategy .Least Recently Used Least recently used , You may not have implemented this algorithm , But you must have heard of , stay Guava Cache in Used The semantics of represents any access , for example put、get. Continue with the following example .
1 |
|
Notice the difference between this example and the example in the previous section : The fourth time get visit one after ,two Becomes the longest unused value , When the fourth value four After the deposit , The object of elimination has become two, Instead of one 了 .
Cache fixed time
Set the expiration time for the cache , Also distinguish HashMap and Cache An important feature of .Guava Cache Provides expireAfterAccess、 expireAfterWrite The plan , by LoadingCache Set the expiration time for the cache value in .
1 |
|
Cache invalidation
1 |
|
Use void invalidate(Object key) Remove a single cache , Use void invalidateAll() Remove all caches .
Cache refresh
Cache refresh is often used to overwrite the old cache value with the new value of the data source ,Guava Cache Two types of refresh mechanisms are provided : Manual refresh and scheduled refresh .
Manually refresh
1 | cache.refresh("kirito"); |
refresh Method will trigger load Logic , Try loading the cache from the data source .
It should be noted that ,refresh Method does not block get Method , So in refresh period , The old cache values will still be accessed , until load complete , See the following example .
1 |
|
In any case , The cache value may be inconsistent with the data source , At the business level, fault-tolerant logic for accessing old values should be well prepared .
Automatically refresh
1 |
|
And the previous section refresh The mechanism is the same ,refreshAfterWrite It won't block either get Threads , There is still the possibility of accessing old values .
Cache hit Statistics
Guava Cache By default, no statistics will be made on hits , Need to build CacheBuilder Time explicit configuration recordStats.
1 |
|
Notification mechanism for cache removal
In some business scenarios , We want to do some monitoring of cache invalidation , Or do some callback processing for the expired cache , You can use RemovalNotification Mechanism .
1 |
|
removalListener You can give LoadingCache Add a callback handler ,RemovalNotification The instance contains the cached key value pairs and the reason for removal .
Weak Keys & Soft Values
Java The concepts of weak reference and soft reference in the foundation are believed to have been learned by everyone , Here is a review for you
- Soft citation : If an object has only Soft citation , be Enough memory when , Garbage collector Just Can't Recycle it ; If Out of memory , will Recycling These objects . As long as the garbage collector doesn't recycle it , This object can be used by the program
- Weak reference : Only have Weak reference Owned by Shorter Of Life cycle . In the process of the garbage collector thread scanning the memory area it governs , Once found, only Weak reference The object of , Whether or not the current Is there enough memory space , Metropolis Recycling Its memory .
stay Guava Cache in ,CacheBuilder Provides weakKeys、weakValues、softValues Three methods , Match the cached key value pairs with JVM The garbage collection mechanism is associated .
This operation may have its applicable scenarios , For example, maximum use JVM Memory cache , But rely on GC clear , The performance is expected to be low . In short, I will not rely on JVM Mechanism to clean up the cache , So I dare not use this feature , Online stability comes first .
If you need to set a cleanup policy , You can refer to the two schemes of fixed quantity and fixed time in the cache expiration summary , Combined use ensures high performance with caching , Don't hang up the memory .
summary
This paper introduces Guava Cache Some commonly used API 、 Usage examples , And some misuses that need to be vigilant .
In choosing to use Guava when , I usually use it in combination with actual scenarios , Make the following considerations :
Why not Redis?
If the local cache can solve , I don't want to introduce an additional middleware .
If you guarantee the consistency between the cache and the data source ?
A situation , I will use caching in scenarios with low data sensitivity , So a brief disagreement can be tolerated ; Other cases , I will set the mechanism of periodically refreshing the cache and manually refreshing the cache . for instance , There is a display application on the page developer List function , Only the application name is stored locally ,developer The list is through a RPC Interface query , And because of the other side's restrictions , The interface qps Very low tolerance , You can consider caching developer list , And configuration maximumSize as well as expireAfterAccess. If a user is developer New data is added to the data source , It leads to data inconsistency , The page can also be set with a sync button , Let users take the initiative refresh; perhaps , If it is judged that the current user is not developer list , You can also program refresh once . All in all, it's very flexible , Use Guava Cache Of API It can meet the caching requirements of most business scenarios .
Why Guava Cache, How about its performance ?
I'm mainly thinking about stability , The project has been using Guava Cache. It is said that there are more than Guava Cache Fast local cache , But my system doesn't care about that performance .
边栏推荐
- U-Net: Convolutional Networks for Biomedical Image Segmentation
- 聊聊服务治理中的路由设计
- C WPF additional attribute implementation interface defines decorator
- 小爱音箱连接网络异常解决办法
- HCIP之路第八次实验
- C WPF realizes dynamic loading of controls through binding
- MIT CMS.300 Session 12 – IDENTITY CONSTRUCTION 虚拟世界中身份认同的建立 part 2
- How to solve CSRF attack in laravel
- Test APK exception control nettraffic attacker development
- Friends of the week
猜你喜欢

在kubernetes中部署kubersphere

Yan's DP analysis

跳跃表原理

The eighth experiment of hcip Road

U-Net: Convolutional Networks for Biomedical Image Segmentation

HCIP之路MPLS

【Veusz】导入CSV中的二维数据

【云计算赛项】职业技能竞赛--容器开发部分例题Pig快速开发框架

How to quickly and gracefully download large files from Google cloud disk (II)

Qt工程报错:-1: error: Cannot run compiler ‘clang++‘. Output:mingw32-make.exe
随机推荐
Unity图片加载和保存
Vs problems when connecting to SQL myconn OPen(); cannot execute
YGG Spain subdao Ola GG officially established
《一周的朋友》
Operation on a bit of binary
3dmax插件开发环境配置及FileExport和Utilities模板测试
【Veusz】导入CSV中的二维数据
[deep learning] [original] how to detect targets and draw map and other parameter maps without yolov5 weights or models
The eighth experiment of hcip Road
利用for循环输出一个字母三角形
Redis setting password
Ffplay realizes user-defined input stream playback
Yan's DP analysis
[2022 graduation season] from graduation to transition to the workplace
[AI practice] data normalization and standardization of machine learning data processing
基于51单片机的温度检测监测报警系统设计
Qt 使用QDomDocument读取xml文件
1. probability theory - combination analysis
To conquer salt fields and vegetable fields with AI, scientific and technological innovation should also step on the "field"
Deploy kubersphere in kubernetes