Skip to content

Commit d22038a

Browse files
author
chenhuawei
committed
增加LoadingCache示例
1 parent 7e9c70a commit d22038a

3 files changed

Lines changed: 109 additions & 0 deletions

File tree

guava/pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>tutorial-java</artifactId>
7+
<groupId>biz.chenxu</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>guava</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>com.google.guava</groupId>
17+
<artifactId>guava</artifactId>
18+
<version>18.0</version>
19+
</dependency>
20+
</dependencies>
21+
22+
</project>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package biz.chenxu.tutorial;
2+
3+
import com.google.common.cache.CacheBuilder;
4+
import com.google.common.cache.CacheLoader;
5+
import com.google.common.cache.LoadingCache;
6+
7+
import java.util.Map;
8+
import java.util.concurrent.ExecutionException;
9+
10+
public class LoadingCacheTutorial {
11+
12+
private LoadingCache<String, String> loadingCache;
13+
14+
private int counter;
15+
16+
private static LoadingCacheTutorial instance = new LoadingCacheTutorial();
17+
18+
public static LoadingCacheTutorial getInstance() {
19+
return instance;
20+
}
21+
22+
private LoadingCacheTutorial() {
23+
loadingCache = CacheBuilder.newBuilder()
24+
.maximumSize(3)
25+
.build(new CacheLoader<String, String>() {
26+
27+
@Override
28+
public String load(String key) throws Exception {
29+
System.out.println(String.format("loading not exist key [%s]", key));
30+
return String.format("cache value %d", counter++);
31+
}
32+
});
33+
}
34+
35+
public String getCacheValueIfExist(String key) {
36+
return loadingCache.getIfPresent(key);
37+
}
38+
39+
public String getCacheValue(String key) {
40+
try {
41+
return loadingCache.get(key);
42+
} catch (ExecutionException e) {
43+
e.printStackTrace();
44+
return null;
45+
}
46+
}
47+
48+
public void dump() {
49+
System.out.println("***begin dump cache content***");
50+
for (Map.Entry<String, String> entry : loadingCache.asMap().entrySet()) {
51+
System.out.println(String.format("%s = %s", entry.getKey(), entry.getValue()));
52+
}
53+
System.out.println("***end dump cache content***");
54+
}
55+
56+
public static void main(String[] args) {
57+
58+
// load notExistKey1
59+
String notExistKey1 = "notExistKey1";
60+
String notExistValue1 = LoadingCacheTutorial.getInstance().getCacheValue(notExistKey1);
61+
System.out.println(String.format("1.%s = %s", notExistKey1, notExistValue1));
62+
63+
// don't load notExistKey2
64+
String notExistKey2 = "notExistKey2";
65+
String notExistValue2 = LoadingCacheTutorial.getInstance().getCacheValueIfExist(notExistKey2);
66+
System.out.println(String.format("2.%s = %s", notExistKey2, notExistValue2));
67+
68+
// LRU
69+
// load notExistKey2
70+
LoadingCacheTutorial.getInstance().getCacheValue(notExistKey2);
71+
LoadingCacheTutorial.getInstance().dump();
72+
73+
System.err.println("[Warn] the key2 will be removed by LRU strategy ......");
74+
// use key1
75+
LoadingCacheTutorial.getInstance().getCacheValue(notExistKey1);
76+
LoadingCacheTutorial.getInstance().getCacheValue(notExistKey1);
77+
78+
// load notExistKey3
79+
LoadingCacheTutorial.getInstance().getCacheValue("notExistKey3");
80+
81+
// load notExistKey4
82+
LoadingCacheTutorial.getInstance().getCacheValue("notExistKey4");
83+
84+
LoadingCacheTutorial.getInstance().dump();
85+
}
86+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<modules>
1313
<module>http-async-client</module>
1414
<module>fast-json</module>
15+
<module>guava</module>
1516
</modules>
1617

1718
<properties>

0 commit comments

Comments
 (0)