Guava-caching-utilities

提供:Dev Guides
移動先:案内検索

グアバ-キャッシングユーティリティ

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>

インターフェースメソッド

Sr.No Method & Description
1

V apply(K key)

廃止予定です。 Functionインターフェースを満たすために提供されます。代わりに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の例

たとえば、* C:/> Guava。*で選択したエディターを使用して、次のJavaプログラムを作成します。

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}