黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

Guava教程

Guava緩存工具

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>

接口方法

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 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(){ // 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();
   }	
}原文出自【易百教程】,商業轉載請聯系作者獲得授權,非商業請保留原文鏈接:https://www.yiibai.com/guava/guava_caching_utilities.html

驗證結果

使用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}

 

 

 

 

轉載自并發編程網-ifeve.com

全部教程
主站蜘蛛池模板: 俄罗斯一级毛片aaaa | 久久久精品麻豆 | 日本黄色的视频 | 日本中文字幕一区二区 | 久久精品国产亚洲精品2020 | 男人香蕉好大好爽视频 | 日本免费黄色网 | 成年看片永远免费 | 色老头永久免费网站 | 最近免费的中文字幕一 | 一级做a爱过程免费视频高清 | 吃奶呻吟打开双腿做受动态图 | 亚洲精品国产成人中文 | 色黄网站成年女人色毛片 | 在线三区| 欧美一级黄色片在线观看 | 欧美成人免费看片一区 | 91视频最新网址 | 大香网伊人久久综合网2021 | 欧美高清另类video | 国产高清一区二区三区 | 国产成人a一在线观看 | 黄色男女视频 | 一级特黄a大片免费 | 欧美人与牲动交xxxxbbbb | 无遮挡动漫肉在线观看高清 | 中国特级黄一级真人毛片 | 免费黄色小视频在线观看 | 在线播放精品视频 | 无收费看污网站 | 免费一级欧美片在线观免看 | 极品色在线精品视频 | 福利在线视频观看 | 成人免费观看www在线 | 永久免费视频v片www | 国产一级一级一级成人毛片 | 国产精品午夜久久 | 黄站免费| 免费一级毛片在线视频观看 | 国产乡下三片在线观看64 | 久久99精品久久久久久秒播放器 |