import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;
public class GuavaCache {
public static void main(String[] args) throws InterruptedException, ExecutionException {
loadingCacheTest();
}
//LoadingCache
private static void loadingCacheTest() throws ExecutionException {
CacheLoader<String, String> loader = new CacheLoader<String, String>(){
@Override
public String load(String key) throws Exception {
Thread.sleep(1000);
System.out.println(key + " is loaded from a cacheLoader!");
return key+"'s value";
}
};
LoadingCache<String, String> loadingCache = CacheBuilder.newBuilder().maximumSize(3).build(loader);
loadingCache.get("k1");
loadingCache.get("k2");
loadingCache.get("k3");
loadingCache.get("k1");
loadingCache.get("k4");
}
//統(tǒng)計(jì)信息
private static void recordStatsTest() {
Cache<String, String> cache = CacheBuilder.newBuilder().maximumSize(3).recordStats().build();//開啟統(tǒng)計(jì)信息開關(guān)
cache.put("k1", "value");
cache.put("k2", "value");
cache.put("k3", "value");
cache.put("k4", "value");
cache.getIfPresent("k1");
cache.getIfPresent("k2");
cache.getIfPresent("k3");
cache.getIfPresent("k4");
cache.getIfPresent("k5");
cache.getIfPresent("k6");
System.out.println(cache.stats());
}
//自動加載锄开,如果值沒有緩存脾拆,則根據(jù)callable進(jìn)行加載,并將返回值加入緩存癌蚁;
//如果同時(shí)有多個(gè)請求相同的key唱遭,則guava會保證只有一個(gè)callable進(jìn)行加載
private static void autoCacheTest() {
Cache<String, String> cache = CacheBuilder.newBuilder().build();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("thread1");
try {
String value = cache.get("key", new Callable<String>() {
@Override
public String call() throws Exception {
System.out.println("load by thread1...");
Thread.sleep(1000);//模擬加載
return "auto load by callable 1";
}
});
System.out.println("thread1 "+value);
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("thread2");
try {
String value = cache.get("key", new Callable<String>() {
@Override
public String call() throws Exception {
System.out.println("load by thread2...");
Thread.sleep(1000);//模擬加載
return "auto load by callable 2";
}
});
System.out.println("thread2 "+value);
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}).start();
}
// 移除監(jiān)聽器
private static void removalListenerTest() {
RemovalListener<String, String> listener = new RemovalListener<String, String>(){
@Override
public void onRemoval(RemovalNotification<String, String> notification) {
System.out.println("[" + notification.getKey() +":" + notification.getValue() + "] is removed!");
}
};
Cache<String, String> cache = CacheBuilder.newBuilder().maximumSize(3).removalListener(listener).build();
cache.put("k1", "a");
cache.put("k2", "a");
cache.put("k3", "a");
cache.put("k4", "a");
cache.put("k5", "a");
}
// 手動清除
private static void invalidateTest() {
Cache<String, String> cache = CacheBuilder.newBuilder().build();
Object value = new Object();
cache.put("k1", "value1");
cache.put("k2", "value2");
cache.put("k3", "value3");
cache.put("k4", "value4");
List<String> list = new ArrayList<String>();
list.add("k1");
list.add("k2");
cache.invalidateAll(list);
System.out.println(cache.getIfPresent("k1"));
System.out.println(cache.getIfPresent("k2"));
System.out.println(cache.getIfPresent("k3"));
System.out.println(cache.getIfPresent("k4"));
cache.invalidate("k3");
System.out.println(cache.getIfPresent("k3"));
cache.invalidateAll();
System.out.println(cache.getIfPresent("k4"));
}
private static void basicUsage() {
Cache<String, String> cache = CacheBuilder.newBuilder().build();
cache.put("word", "Hello Guava Cache");
System.out.println(cache.getIfPresent("word"));
}
//設(shè)置最大存儲
private static void maximumSizeTest() {
Cache<String, String> cache = CacheBuilder.newBuilder().maximumSize(2).build();
cache.put("k1", "a");
cache.put("k2", "b");
cache.put("k3", "c");
System.out.println("第一個(gè)值:"+cache.getIfPresent("k1"));
System.out.println("第二個(gè)值:"+cache.getIfPresent("k2"));
System.out.println("第三個(gè)值:"+cache.getIfPresent("k3"));
}
//設(shè)置過期時(shí)間戳寸,按最后寫時(shí)間開始計(jì)時(shí)
private static void expireAfterWriteTest() throws InterruptedException {
Cache<String, String> cache = CacheBuilder.newBuilder().expireAfterWrite(3, TimeUnit.SECONDS).build();
cache.put("k1", "a");
int times=1;
while(true) {
System.out.println("第"+ times++ + "次獲取到的值為:"+cache.getIfPresent("k1"));
Thread.sleep(1000);
}
}
//設(shè)置過期時(shí)間, 按最后一次讀開始計(jì)時(shí)
private static void expireAfterAccessTest() throws InterruptedException {
Cache<String, String> cache = CacheBuilder.newBuilder().expireAfterAccess(3, TimeUnit.SECONDS).build();
cache.put("k1", "a");
int times=1;
while(true) {
Thread.sleep(times*1000);
System.out.println("等待"+times+++"s后取到k1的值為:"+cache.getIfPresent("k1"));
}
}
//弱引用
private static void weakValueTest() {
Cache<String, Object> cache = CacheBuilder.newBuilder().weakValues().build();
Object value = new Object();
cache.put("k1", value);
value = new Object();
System.gc();
System.out.println(cache.getIfPresent("k1"));
}
}
參考資料