Guava 缓存应用示例
精华
小牛编辑
132浏览
2023-03-14
1 什么是Guava LoadingCache接口
Guava 通过接口 LoadingCache<K,V> 提供了一个非常强大的基于内存的缓存机制。值会自动加载到缓存中,它提供了许多对缓存需求有用的实用方法。
2 Guava LoadingCache接口的语法
@Beta
@GwtCompatible
public interface LoadingCache<K,V>
extends Cache<K,V>, Function<K,V>
3 Guava LoadingCache接口的方法
方法 | 描述 |
---|---|
V apply(K key) | 已弃用。提供满足Function接口;请改用 get(K) 或 getUnchecked(K)。 |
ConcurrentMap<K,V> asMap() | 返回存储在此缓存中的条目的Map作为线程安全映射。 |
V get(K key) | 返回与此缓存中的key关联的值,如有必要,首先加载该值。 |
ImmutableMap<K,V> getAll(Iterable<? extends K> keys) | 返回与键关联的值的Map,必要时创建或检索这些值。 |
V getUnchecked(K key) | 返回与此缓存中的键关联的值,如有必要,首先加载该值。 |
void refresh(K key) | 为键加载一个新值,可能是异步的。 |
5 Guava LoadingCache接口的例子
让我们看一个简单的Guava LoadingCache接口示例。
package cn.xnip;
import com.google.common.base.MoreObjects;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
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();
}
}
输出结果为: