当前位置:网站首页>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 .
边栏推荐
- The idea shortcut key ALT realizes the whole column operation
- Zipkin安装和使用
- 【Mysql】认识Mysql重要架构(一)
- What is the difference between NFT and digital collections?
- Laravel框架日志文件存放在哪里?怎么用?
- Numpy Foundation
- Li Mu D2L (VI) -- model selection
- HBuilderX 运行微信开发者工具 “Fail to open IDE“报错解决
- NFT与数字藏品到底有何区别?
- The Child and Binary Tree-多项式开根求逆
猜你喜欢

Advanced mathematics | Takeshi's "classic series" daily question train of thought and summary of error prone points

Clean the label folder

Voice chat app source code - Nath live broadcast system source code

Elastic APM安装和使用

网络安全漫山遍野的高大上名词之后的攻防策略本质

Original root and NTT 5000 word explanation

redis原理和使用-安装和分布式配置

The essence of attack and defense strategy behind the noun of network security

分布式跟踪系统选型与实践

2022年上海市安全员C证考试试题及模拟考试
随机推荐
ext3文件系统的一个目录下,无法创建子文件夹,但可以创建文件
2B和2C
聪明的美食家 C语言
Introduction to excellent verilog/fpga open source project (30) - brute force MD5
NTT(快速数论变换)多项式求逆 一千五百字解析
服务器内存故障预测居然可以这样做!
滑动窗口、双指针、单调队列、单调栈
"No input file specified" problem handling
【线上死锁分析】由index_merge引发的死锁事件
SQL入门——组合表
Error: Cannot find module ‘umi‘ 问题处理
分布式跟踪系统选型与实践
Does volatile rely on the MESI protocol to solve the visibility problem? (next)
巴比特 | 元宇宙每日必读:元宇宙的未来是属于大型科技公司,还是属于分散的Web3世界?...
CF1481C Fence Painting
Li Mu D2L (VI) -- model selection
Vertical search
Datawhale panda book has been published!
[use of final keyword]
力扣链表题