-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp3.java
More file actions
46 lines (39 loc) · 1.12 KB
/
Copy pathApp3.java
File metadata and controls
46 lines (39 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.Weigher;
public class App3 {
public static void main(String[] args) {
//
CacheLoader<String,String> loader;
loader=new CacheLoader<String,String>(){
@Override
public String load(String key) throws Exception {
//
return key.toUpperCase();
}
};
//
Weigher<String,String> weightByLength;
weightByLength =new Weigher<String, String>() {
public int weigh(String key, String value) {
return value.length();
}
};
//装载cache
LoadingCache<String,String> cache;
cache= CacheBuilder.newBuilder()
.maximumSize(16)
.build(loader);
//
cache.getUnchecked("first");
cache.getUnchecked("second");
cache.getUnchecked("third");
cache.getUnchecked("last");
//
System.out.println(" cache缓存的大小是: "+cache.size());
System.out.println("first的value是 "+cache.getIfPresent("first"));
System.out.println("last的value是 "+cache.getIfPresent("last"));
}
}