Guava Cache是在内存中缓存数据,相比较于数据库或redis存储,访问内存中的数据会更加高效。Guava官网介绍,下面的这几种状况能够考虑使用Guava Cache:redis
愿意消耗一些内存空间来提高速度。数据库
预料到某些键会被屡次查询。设计模式
缓存中存放的数据总量不会超出内存容量。缓存
因此,能够将程序频繁用到的少许数据存储到Guava Cache中,以改善程序性能。下面对Guava Cache的用法进行详细的介绍。多线程
接口Cache表明一块缓存,它有以下方法:app
public interface Cache<K, V> { V get(K key, Callable<? extends V> valueLoader) throws ExecutionException; ImmutableMap<K, V> getAllPresent(Iterable<?> keys); void put(K key, V value); void putAll(Map<? extends K, ? extends V> m); void invalidate(Object key); void invalidateAll(Iterable<?> keys); void invalidateAll(); long size(); CacheStats stats(); ConcurrentMap<K, V> asMap(); void cleanUp(); }
能够经过CacheBuilder类构建一个缓存对象,CacheBuilder类采用builder设计模式,它的每一个方法都返回CacheBuilder自己,直到build方法被调用。构建一个缓存对象代码以下。ide
public class StudyGuavaCache { public static void main(String[] args) { Cache<String,String> cache = CacheBuilder.newBuilder().build(); cache.put("word","Hello Guava Cache"); System.out.println(cache.getIfPresent("word")); } }
上面的代码经过CacheBuilder.newBuilder().build()这句代码建立了一个Cache缓存对象,并在缓存对象中存储了key为word,value为Hello Guava Cache的一条记录。能够看到Cache很是相似于JDK中的Map,可是相比于Map,Guava Cache提供了不少更强大的功能。性能
Guava Cache能够在构建缓存对象时指定缓存所可以存储的最大记录数量。当Cache中的记录数量达到最大值后再调用put方法向其中添加对象,Guava会先从当前缓存的对象记录中选择一条删除掉,腾出空间后再将新的对象存储到Cache中。ui
public class StudyGuavaCache { public static void main(String[] args) { Cache<String,String> cache = CacheBuilder.newBuilder() .maximumSize(2) .build(); cache.put("key1","value1"); cache.put("key2","value2"); cache.put("key3","value3"); System.out.println("第一个值:" + cache.getIfPresent("key1")); System.out.println("第二个值:" + cache.getIfPresent("key2")); System.out.println("第三个值:" + cache.getIfPresent("key3")); } }
上面代码在构造缓存对象时,经过CacheBuilder类的maximumSize方法指定Cache最多能够存储两个对象,而后调用Cache的put方法向其中添加了三个对象。程序执行结果以下图所示,能够看到第三条对象记录的插入,致使了第一条对象记录被删除。spa
在构建Cache对象时,能够经过CacheBuilder类的expireAfterAccess和expireAfterWrite两个方法为缓存中的对象指定过时时间,过时的对象将会被缓存自动删除。其中,expireAfterWrite方法指定对象被写入到缓存后多久过时,expireAfterAccess指定对象多久没有被访问后过时。
public class StudyGuavaCache { public static void main(String[] args) throws InterruptedException { Cache<String,String> cache = CacheBuilder.newBuilder() .maximumSize(2) .expireAfterWrite(3,TimeUnit.SECONDS) .build(); cache.put("key1","value1"); int time = 1; while(true) { System.out.println("第" + time++ + "次取到key1的值为:" + cache.getIfPresent("key1")); Thread.sleep(1000); } } }
上面的代码在构造Cache对象时,经过CacheBuilder的expireAfterWrite方法指定put到Cache中的对象在3秒后会过时。在Cache对象中存储一条对象记录后,每隔1秒读取一次这条记录。程序运行结果以下图所示,能够看到,前三秒能够从Cache中获取到对象,超过三秒后,对象从Cache中被自动删除。
下面代码是expireAfterAccess的例子。
public class StudyGuavaCache { public static void main(String[] args) throws InterruptedException { Cache<String,String> cache = CacheBuilder.newBuilder() .maximumSize(2) .expireAfterAccess(3,TimeUnit.SECONDS) .build(); cache.put("key1","value1"); int time = 1; while(true) { Thread.sleep(time*1000); System.out.println("睡眠" + time++ + "秒后取到key1的值为:" + cache.getIfPresent("key1")); } } }
经过CacheBuilder的expireAfterAccess方法指定Cache中存储的对象若是超过3秒没有被访问就会过时。while中的代码每sleep一段时间就会访问一次Cache中存储的对象key1,每次访问key1以后下次sleep的时间会加长一秒。程序运行结果以下图所示,从结果中能够看出,当超过3秒没有读取key1对象以后,该对象会自动被Cache删除。
也能够同时用expireAfterAccess和expireAfterWrite方法指定过时时间,这时只要对象知足二者中的一个条件就会被自动过时删除。
能够经过weakKeys和weakValues方法指定Cache只保存对缓存记录key和value的弱引用。这样当没有其余强引用指向key和value时,key和value对象就会被垃圾回收器回收。
public class StudyGuavaCache { public static void main(String[] args) throws InterruptedException { Cache<String,Object> cache = CacheBuilder.newBuilder() .maximumSize(2) .weakValues() .build(); Object value = new Object(); cache.put("key1",value); value = new Object();//原对象再也不有强引用 System.gc(); System.out.println(cache.getIfPresent("key1")); } }
上面代码的打印结果是null。构建Cache时经过weakValues方法指定Cache只保存记录值的一个弱引用。当给value引用赋值一个新的对象以后,就再也不有任何一个强引用指向原对象。System.gc()触发垃圾回收后,原对象就被清除了。
能够调用Cache的invalidateAll或invalidate方法显示删除Cache中的记录。invalidate方法一次只能删除Cache中一个记录,接收的参数是要删除记录的key。invalidateAll方法能够批量删除Cache中的记录,当没有传任何参数时,invalidateAll方法将清除Cache中的所有记录。invalidateAll也能够接收一个Iterable类型的参数,参数中包含要删除记录的全部key值。下面代码对此作了示例。
public class StudyGuavaCache { public static void main(String[] args) throws InterruptedException { Cache<String,String> cache = CacheBuilder.newBuilder().build(); Object value = new Object(); cache.put("key1","value1"); cache.put("key2","value2"); cache.put("key3","value3"); List<String> list = new ArrayList<String>(); list.add("key1"); list.add("key2"); cache.invalidateAll(list);//批量清除list中所有key对应的记录 System.out.println(cache.getIfPresent("key1")); System.out.println(cache.getIfPresent("key2")); System.out.println(cache.getIfPresent("key3")); } }
代码中构造了一个集合list用于保存要删除记录的key值,而后调用invalidateAll方法批量删除key1和key2对应的记录,只剩下key3对应的记录没有被删除。
能够为Cache对象添加一个移除监听器,这样当有记录被删除时能够感知到这个事件。
public class StudyGuavaCache { public static void main(String[] args) throws InterruptedException { RemovalListener<String, String> listener = new RemovalListener<String, String>() { public void onRemoval(RemovalNotification<String, String> notification) { System.out.println("[" + notification.getKey() + ":" + notification.getValue() + "] is removed!"); } }; Cache<String,String> cache = CacheBuilder.newBuilder() .maximumSize(3) .removalListener(listener) .build(); Object value = new Object(); cache.put("key1","value1"); cache.put("key2","value2"); cache.put("key3","value3"); cache.put("key4","value3"); cache.put("key5","value3"); cache.put("key6","value3"); cache.put("key7","value3"); cache.put("key8","value3"); } }
removalListener方法为Cache指定了一个移除监听器,这样当有记录从Cache中被删除时,监听器listener就会感知到这个事件。程序运行结果以下图所示。
Cache的get方法有两个参数,第一个参数是要从Cache中获取记录的key,第二个记录是一个Callable对象。当缓存中已经存在key对应的记录时,get方法直接返回key对应的记录。若是缓存中不包含key对应的记录,Guava会启动一个线程执行Callable对象中的call方法,call方法的返回值会做为key对应的值被存储到缓存中,而且被get方法返回。下面是一个多线程的例子:
public class StudyGuavaCache { private static Cache<String,String> cache = CacheBuilder.newBuilder() .maximumSize(3) .build(); public static void main(String[] args) throws InterruptedException { new Thread(new Runnable() { public void run() { System.out.println("thread1"); try { String value = cache.get("key", new Callable<String>() { public String call() throws Exception { System.out.println("load1"); //加载数据线程执行标志 Thread.sleep(1000); //模拟加载时间 return "auto load by Callable"; } }); System.out.println("thread1 " + value); } catch (ExecutionException e) { e.printStackTrace(); } } }).start(); new Thread(new Runnable() { public void run() { System.out.println("thread2"); try { String value = cache.get("key", new Callable<String>() { public String call() throws Exception { System.out.println("load2"); //加载数据线程执行标志 Thread.sleep(1000); //模拟加载时间 return "auto load by Callable"; } }); System.out.println("thread2 " + value); } catch (ExecutionException e) { e.printStackTrace(); } } }).start(); } }
这段代码中有两个线程共享同一个Cache对象,两个线程同时调用get方法获取同一个key对应的记录。因为key对应的记录不存在,因此两个线程都在get方法处阻塞。此处在call方法中调用Thread.sleep(1000)模拟程序从外存加载数据的时间消耗。代码的执行结果以下图:
从结果中能够看出,虽然是两个线程同时调用get方法,但只有一个get方法中的Callable会被执行(没有打印出load2)。Guava能够保证当有多个线程同时访问Cache中的一个key时,若是key对应的记录不存在,Guava只会启动一个线程执行get方法中Callable参数对应的任务加载数据存到缓存。当加载完数据后,任何线程中的get方法都会获取到key对应的值。
能够对Cache的命中率、加载数据时间等信息进行统计。在构建Cache对象时,能够经过CacheBuilder的recordStats方法开启统计信息的开关。开关开启后Cache会自动对缓存的各类操做进行统计,调用Cache的stats方法能够查看统计后的信息。
public class StudyGuavaCache { public static void main(String[] args) throws InterruptedException { Cache<String,String> cache = CacheBuilder.newBuilder() .maximumSize(3) .recordStats() //开启统计信息开关 .build(); cache.put("key1","value1"); cache.put("key2","value2"); cache.put("key3","value3"); cache.put("key4","value4"); cache.getIfPresent("key1"); cache.getIfPresent("key2"); cache.getIfPresent("key3"); cache.getIfPresent("key4"); cache.getIfPresent("key5"); cache.getIfPresent("key6"); System.out.println(cache.stats()); //获取统计信息 } }
程序执行结果以下图所示:
这些统计信息对于调整缓存设置是相当重要的,在性能要求高的应用中应该密切关注这些数据
LoadingCache是Cache的子接口,相比较于Cache,当从LoadingCache中读取一个指定key的记录时,若是该记录不存在,则LoadingCache能够自动执行加载数据到缓存的操做。LoadingCache接口的定义以下:
public interface LoadingCache<K, V> extends Cache<K, V>, Function<K, V> { V get(K key) throws ExecutionException; V getUnchecked(K key); ImmutableMap<K, V> getAll(Iterable<? extends K> keys) throws ExecutionException; V apply(K key); void refresh(K key); @Override ConcurrentMap<K, V> asMap(); }
与构建Cache类型的对象相似,LoadingCache类型的对象也是经过CacheBuilder进行构建,不一样的是,在调用CacheBuilder的build方法时,必须传递一个CacheLoader类型的参数,CacheLoader的load方法须要咱们提供实现。当调用LoadingCache的get方法时,若是缓存不存在对应key的记录,则CacheLoader中的load方法会被自动调用从外存加载数据,load方法的返回值会做为key对应的value存储到LoadingCache中,并从get方法返回。
public class StudyGuavaCache { public static void main(String[] args) throws ExecutionException { CacheLoader<String, String> loader = new CacheLoader<String, String> () { public String load(String key) throws Exception { Thread.sleep(1000); //休眠1s,模拟加载数据 System.out.println(key + " is loaded from a cacheLoader!"); return key + "'s value"; } }; LoadingCache<String,String> loadingCache = CacheBuilder.newBuilder() .maximumSize(3) .build(loader);//在构建时指定自动加载器 loadingCache.get("key1"); loadingCache.get("key2"); loadingCache.get("key3"); } }
程序执行结果以下图所示: