-
Notifications
You must be signed in to change notification settings - Fork 1
/
GuavaCache.java
34 lines (29 loc) · 1000 Bytes
/
GuavaCache.java
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
package org.kur.practice.guava;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
/**
* This class demonstrate the cache mechanism provide by google guava.
* Created by Keyur on 18/8/17.
*/
public class GuavaCache {
public static void main(String[] args) throws ExecutionException {
CacheLoader<String, String> loader;
loader = new CacheLoader<String, String>() {
@Override
public String load(String key) {
return key.toUpperCase();
}
};
LoadingCache<String, String> cache;
cache = CacheBuilder.newBuilder()
.expireAfterWrite(10,TimeUnit.MINUTES)
.build(loader);
cache.put("1", "a");
cache.put("2", "b");
cache.put("3", "c");
System.out.println(cache.get("1"));
}
}