当前位置:网站首页>Local cache
Local cache
2022-07-26 09:16:00 【Or turn around】
In distributed systems , Distributed caching is like redis,memcache Use more . actually , Local caching is also necessary in many scenarios .
This paper mainly introduces google The toolkit guava The use of caching tools in .
Local cache application scenarios :
(1) Very high performance requirements
(2) Not often change or there are hot words
(3) It can accept the non real-time nature of data
Local caching can be implemented in several ways :ConcurrentHashMap,Guava cache or Ehcached. Here to talk about Guava cache Use .
Its characteristics are as follows :
- Use LRU Cache expiration mechanism
- Concurrent processing power : Be similar to ConcurrentHashMap, It's thread safe , It adopts the segmented lock mechanism .
- Update lock : stay CacheLoader Of load Control in the method , To the same key, Let only one request read the source and set it in the cache , Other requests block waiting ( Prevent cache breakdown ).
- Integrated data sources :get Method can read data from the data source and backfill it into the cache when it cannot be read from the cache .
- Monitor cache load / Hit situation
Let's take a look at the code :
public class LocalCache {
public static void main(String[] args) throws Exception {
LoadingCache<ReqArg, String> loadingCache = CacheBuilder.newBuilder()
.recordStats().maximumSize(1000).expireAfterWrite(1, TimeUnit.MINUTES)
.build(new CacheLoader<ReqArg, String>() {
@Override
public String load(ReqArg key) throws Exception {
return key.getKey();
}
});
Thread checkupThread = new Thread(() -> {
while (true) {
try {
Thread.sleep(1000);
System.out.println(loadingCache.stats());
} catch (InterruptedException e) {
break;
}
}
});
checkupThread.setDaemon(true);
checkupThread.start();
for (int i = 0; i < 20; i++) {
String key = String.valueOf(i);
ReqArg reqArg = new ReqArg(key, key);
System.out.println(loadingCache.get(reqArg));
}
for (int i = 0; i < 10; i++) {
String key = String.valueOf(i);
ReqArg reqArg = new ReqArg(key, key);
System.out.println(loadingCache.get(reqArg));
}
Thread.sleep(10 * 60 * 3600);
}
static class ReqArg {
String key;
String id;
public ReqArg() {
}
public ReqArg(String key, String id) {
this.key = key;
this.id = id;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public int hashCode() {
return new HashCodeBuilder()
.append(key)
.append(id)
.toHashCode();
}
@Override
public boolean equals(Object obj) {
ReqArg args = (ReqArg) obj;
return new EqualsBuilder()
.append(this.key, args.key)
.append(this.id, args.id)
.isEquals();
}
// public boolean equals(Object o) {
// return EqualsBuilder.reflectionEquals(this, o);
//
// }
//
// public int hashCode() {
// return HashCodeBuilder.reflectionHashCode(this);
// }
}
}
Run the above code , The output is as follows :
0
1
...
CacheStats{hitCount=10, missCount=20, loadSuccessCount=20, loadExceptionCount=0, totalLoadTime=2012339, evictionCount=0}
CacheStats{hitCount=10, missCount=20, loadSuccessCount=20, loadExceptionCount=0, totalLoadTime=2012339, evictionCount=0}
...
The numbers above are two for The result of the loop output . The following cache hit statistics are threads checkupThread Output . Its meaning is as follows :
A cache hit 10 Time , Not hit 20 Time , Load data from data source 20 Time .
At the first for In circulation , At this point, the cache is empty , You need to read data from the data source every time (load Method ), And load it into the cache .20 All misses .
In the second for In circulation , All the data has been cached , So read directly from the cache .10 Hit the cache all times .
边栏推荐
猜你喜欢

布隆过滤器

优秀的 Verilog/FPGA开源项目介绍(三十零)- 暴力破解MD5

Redis principle and use - Basic Features

2022化工自动化控制仪表操作证考试题模拟考试平台操作

CSDN Top1 "how does a Virgo procedural ape" become a blogger with millions of fans through writing?

NTT (fast number theory transformation) polynomial inverse 1500 word analysis

NPM add source and switch source

Li Mu D2L (VI) -- model selection

Li Mu D2L (V) -- multilayer perceptron

ext4文件系统打开了DIR_NLINK特性后,link_count超过65000的后使用link_count=1来表示数量不可知
随机推荐
QT | about how to use EventFilter
Qt | 关于如何使用事件过滤器 eventFilter
NFT与数字藏品到底有何区别?
Datawhale panda book has been published!
ONTAP 9文件系统的限制
STM32+MFRC522完成IC卡号读取、密码修改、数据读写
220. Presence of repeating element III
Probability model in machine learning
CF1481C Fence Painting
基于序的评价指标 (特别针对推荐系统和多标签学习)
tornado之多进程服务
【无标题】
2022化工自动化控制仪表操作证考试题模拟考试平台操作
PAT 甲级 A1076 Forwards on Weibo
Hbuilderx runs the wechat developer tool "fail to open ide" to solve the error
李沐d2l(五)---多层感知机
Study notes of dataX
redis原理和使用-基本特性
【Mysql】Mysql锁详解(三)
ext4文件系统打开了DIR_NLINK特性后,link_count超过65000的后使用link_count=1来表示数量不可知