Guava通過介面LoadingCache提供了一個非常強大的基於記憶體的LoadingCache<K,V>。在快取中自動載入值,它提供了許多實用的方法,在有快取需求時非常有用。
以下是forcom.google.common.cache.LoadingCache<K,V>介面的宣告:
@Beta @GwtCompatible public interface LoadingCache<K,V> extends Cache<K,V>, Function<K,V>
S.N. | 方法及說明 |
---|---|
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) 載入鍵key,可能是非同步的一個新值。 |
使用所選擇的編輯器建立下面的java程式 C:/> Guava
GuavaTester.javaimport 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 LoadingCacheemployeeCache = 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 (){ // 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 database = new HashMap (); 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}
private static Employee getFromDataBaseString empId { Employee e1 = new Employee"Mahesh" "Finance" "100" Employee e2 = new Employee"Rohan" "IT" "103" Employee e3 = new Employee"Sohan" "Admin" "110" //指定型別 Mapdatabase = new HashMap database.put"100" e1 database.put"103" e2 database.put"110" e3 System.out.println"Database hit for " empId return database.getempId } public static void mainString[] args { //create a cache for employees based on their employee id LoadingCache employeeCache = CacheBuilder.newBuilder //maximum 100 records can be cached .maximumSize100 //cache will expire after 30 minutes of access .expireAfterAccess30 TimeUnit.MINUTES //build the cacheloader //指定型別 .buildnew CacheLoader { Override public Employee loadString empId throws Exception { //make the expensive call return getFromDataBaseempId } } try { //on first invocation cache will be populated with corresponding //employee record System.out.println"Invocation #1" System.out.printlnemployeeCache.get"100" System.out.printlnemployeeCache.get"103" System.out.printlnemployeeCache.get"110" //second invocation data will be returned from cache System.out.printlnemployeeCache.get"100" System.out.printlnemployeeCache.get"103" System.out.printlnemployeeCache.get"110" } catch Exception e { e.printStackTrace } } 提交時間:2019-08-26