教程 > guava 教程 > 阅读:38

guava caching 实用程序——迹忆客-ag捕鱼王app官网

guava 通过接口 loadingcache 提供了非常强大的基于内存的缓存机制。 值会自动加载到缓存中,它提供了许多可用于缓存需求的实用方法。


接口声明

以下是 com.google.common.cache.loadingcache 接口的声明

@beta
@gwtcompatible
public interface loadingcache
   extends cache, function

接口方法

序号 方法 说明
1 v apply(k key) 已弃用。 提供满足function接口; 使用 get(k) 或 getunchecked(k) 代替。
2 concurrentmap asmap() 返回存储在此缓存中的条目的视图作为线程安全映射。
3 v get(k key) 返回与此缓存中的键关联的值,如有必要,首先加载该值。
4 immutablemap getall(iterable 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[]) {
   
      //根据员工 id 为员工创建缓存
      loadingcache employeecache = 
         cachebuilder.newbuilder()
         .maximumsize(100)                             // 最多可缓存 100 条记录
         .expireafteraccess(30, timeunit.minutes)      // 缓存将在访问 30 分钟后过期
         .build(new cacheloader() {  // 构建缓存加载器
            
            @override
            public employee load(string empid) throws exception {
               return getfromdatabase(empid);
            } 
         });
      try {            
         //第一次调用时,缓存将填充相应的员工记录
         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}

查看笔记

扫码一下
查看教程更方便
网站地图