当前位置:网站首页>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 .
边栏推荐
猜你喜欢
随机推荐
Introduction to excellent verilog/fpga open source project (30) - brute force MD5
Qtcreator reports an error: you need to set an executable in the custom run configuration
堆外内存的使用
JVM触发minor gc的条件
[eslint] Failed to load parser ‘@typescript-eslint/parser‘ declared in ‘package. json » eslint-confi
网络安全漫山遍野的高大上名词之后的攻防策略本质
760. String length
Windows通过命令备份数据库到本地
ES6 modular import and export) (realize page nesting)
Sending and receiving of C serialport
Thread Join 和Object wait 的区别
JS - DataTables control on the number of displays per page
PHP page value transfer
redis原理和使用-基本特性
李沐d2l(五)---多层感知机
2022茶艺师(中级)特种作业证考试题库模拟考试平台操作
QtCreator报错:You need to set an executable in the custom run configuration.
2B和2C
CSDN Top1 "how does a Virgo procedural ape" become a blogger with millions of fans through writing?
Elastic APM安装和使用