- Guava 教程
- Guava - 首頁
- Guava - 概述
- Guava - 環境設定
- Guava - Optional 類
- Guava - Preconditions 類
- Guava - Ordering 類
- Guava - Objects 類
- Guava - Range 類
- Guava - Throwables 類
- Guava - 集合實用工具
- Guava - 快取實用工具
- Guava - 字串實用工具
- Guava - 原始實用工具
- Guava - 數學實用工具
- Guava 有用資源
- Guava - 快速指南
- Guava - 有用資源
- Guava - 討論
Guava - 快取實用工具
Guava 透過介面 LoadingCache<K,V> 提供了一種非常強大的基於記憶體的快取機制。值會自動載入到快取中,它提供了很多對快取需求有用的實用方法。
介面宣告
以下是 com.google.common.cache.LoadingCache<K,V> 的宣告 -
@Beta @GwtCompatible public interface LoadingCache<K,V> extends Cache<K,V>, Function<K,V>
介面方法
| 序號 | 方法 & 說明 |
|---|---|
| 1 |
V apply(K key) 已棄用。提供以滿足函式介面;改用 get(K) 或 getUnchecked(K)。 |
| 2 |
ConcurrentMap<K,V> asMap() 以執行緒安全對映的形式返回此快取中儲存的條目的檢視。 |
| 3 |
V get(K key) 返回與此快取中鍵關聯的值,必要時先載入該值。 |
| 4 |
ImmutableMap<K,V> getAll(Iterable<? extends K> keys) 返回與鍵關聯的值的對映,必要時建立或檢索這些值。 |
| 5 |
V getUnchecked(K key) 返回與此快取中鍵關聯的值,必要時先載入該值。 |
| 6 |
void refresh(K key) 載入鍵的新值,可能非同步進行。 |
LoadingCache 示例
使用任意你選擇的編輯器建立以下 Java 程式,放在 C:/> Guava 中。
GuavaTester.java
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import com.google.common.base.MoreObjects;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
public class GuavaTester {
public static void main(String args[]) {
//create a cache for employees based on their employee id
LoadingCache<String, Employee> employeeCache =
CacheBuilder.newBuilder()
.maximumSize(100) // maximum 100 records can be cached
.expireAfterAccess(30, TimeUnit.MINUTES) // cache will expire after 30 minutes of access
.build(new CacheLoader<String, Employee>() { // build the cacheloader
@Override
public Employee load(String empId) throws Exception {
//make the expensive call
return getFromDatabase(empId);
}
});
try {
//on first invocation, cache will be populated with corresponding
//employee record
System.out.println("Invocation #1");
System.out.println(employeeCache.get("100"));
System.out.println(employeeCache.get("103"));
System.out.println(employeeCache.get("110"));
//second invocation, data will be returned from cache
System.out.println("Invocation #2");
System.out.println(employeeCache.get("100"));
System.out.println(employeeCache.get("103"));
System.out.println(employeeCache.get("110"));
} catch (ExecutionException e) {
e.printStackTrace();
}
}
private static Employee getFromDatabase(String empId) {
Employee e1 = new Employee("Mahesh", "Finance", "100");
Employee e2 = new Employee("Rohan", "IT", "103");
Employee e3 = new Employee("Sohan", "Admin", "110");
Map<String, Employee> database = new HashMap<String, Employee>();
database.put("100", e1);
database.put("103", e2);
database.put("110", e3);
System.out.println("Database hit for" + empId);
return database.get(empId);
}
}
class Employee {
String name;
String dept;
String emplD;
public Employee(String name, String dept, String empID) {
this.name = name;
this.dept = dept;
this.emplD = empID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public String getEmplD() {
return emplD;
}
public void setEmplD(String emplD) {
this.emplD = emplD;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(Employee.class)
.add("Name", name)
.add("Department", dept)
.add("Emp Id", emplD).toString();
}
}
驗證結果
使用 javac 編譯器編譯該類,如下所示 -
C:\Guava>javac GuavaTester.java
現在執行 GuavaTester 檢視結果。
C:\Guava>java GuavaTester
檢視結果。
Invocation #1
Database hit for100
Employee{Name=Mahesh, Department=Finance, Emp Id=100}
Database hit for103
Employee{Name=Rohan, Department=IT, Emp Id=103}
Database hit for110
Employee{Name=Sohan, Department=Admin, Emp Id=110}
Invocation #2
Employee{Name=Mahesh, Department=Finance, Emp Id=100}
Employee{Name=Rohan, Department=IT, Emp Id=103}
Employee{Name=Sohan, Department=Admin, Emp Id=110}
廣告