Guava Cache使用笔记

xiaoxiao2021-02-27  188

我刚开始使用时还以为Guava Cache跟HashMap一样,get(null)返回null。  实际上Guava整体设计思想就是拒绝null的,很多地方都会执行com.google.common.base.Preconditions.checkNotNull的检查。  2.Guava Cache的load方法不能返回null,否则抛异常   Guava Cache的get方法先在本地缓存中取,如果不存在,则会触发load方法。但load方法不能返回null。  设想这样一个场景:进行某些热点数据查询时,如果缓存中没有,则去数据库中查询,并把查询到的结果保存到缓存中。  但假如说数据库中也没有呢?  这个时候load方法就会抛异常,例如:  Java代码   public enum TestGuavaCache {          INSTANCE;          private LoadingCache<String, Person> infos;          TestGuavaCache() {           infos = CacheBuilder.newBuilder().expireAfterAccess(10, TimeUnit.MINUTES).build(new CacheLoader<String, Person>() {               public Person load(String key) throws Exception {                   //load from database                   Person p = loadFromDatabase();                   return p;               }           });       }          //假设数据库中不存在       private Person loadFromDatabase() {           return null;       }          public Person get(String key) {           try {               return infos.get(key);           } catch (Exception e) {               //log exception           }           return null;       }   }   这是因为Guava Cache认为cache null是无意义的,因此Guava Cache的javadoc里加粗说明: must not be null 现实世界没那么理想,肯定会有null的情况,那怎么处理呢?我的处理一般是对Guava Cache的get方法做try-catch。  有时候cache null也是有意义的,例如对于一个key,假如数据库中也没有对应的value,那就把这个情况记录下来,  避免频繁的查询数据库(例如一些攻击性行为),直接在缓存中就把这个key挡住了。  怎么做呢?举例:  Java代码   @Test   public void whenNullValue_thenOptional() {       CacheLoader<String, Optional<String>> loader;       loader = new CacheLoader<String, Optional<String>>() {           @Override           public Optional<String> load(String key) {               return Optional.fromNullable(getSuffix(key));           }       };           LoadingCache<String, Optional<String>> cache;       cache = CacheBuilder.newBuilder().build(loader);           assertEquals("txt", cache.getUnchecked("text.txt").get());       assertFalse(cache.getUnchecked("hello").isPresent());   }   private String getSuffix(final String str) {       int lastIndex = str.lastIndexOf('.');       if (lastIndex == -1) {           return null;       }       return str.substring(lastIndex + 1);   }   3.什么时候用get,什么时候用getUnchecked   官网文档说:  Java代码   . If you have defined a CacheLoader that does not declare any checked exceptions then you can perform cache lookups using getUnchecked(K);    however care must be taken not to call getUnchecked on caches whose CacheLoaders declare checked exceptions.   字面意思是,如果你的CacheLoader没有定义任何checked Exception,那你可以使用getUnchecked。  这一段话我也不是很理解。。官网上给了一个例子是,load方法没有声明throws Exception,那就可以使用getUnchecked:  Java代码   LoadingCache<Key, Graph> graphs = CacheBuilder.newBuilder()          .expireAfterAccess(10, TimeUnit.MINUTES)          .build(              new CacheLoader<Key, Graph>() {                public Graph load(Key key) { // no checked exception                  return createExpensiveGraph(key);                }              });      ...   return graphs.getUnchecked(key);   4.如何定义一个普通的Guava Cache,不需要用到load方法   假如只是简单的把Guava Cache当作HashMap或ConcurrentHashMap的替代品,不需要用到load方法,而是手动地插入,可以这样:  Java代码   com.google.common.cache.Cache<String, String> cache = CacheBuilder.newBuilder().expireAfterWrite(10, TimeUnit.MINUTES).build();   注意不能用LoadingCache了。  查找:  cache.getIfPresent("xx");  插入:  cache.put("xx", "xxx");  5.Guava Cache的超时机制不是精确的。   我曾经依赖Guava Cache的超时机制和RemovalListener,以实现类似定时任务的功能;后来发现Guava Cache的超时机制是不精确的,例如你设置cache的缓存时间是30秒,  那它存活31秒、32秒,都是有可能的。  官网说:  Java代码   Timed expiration is performed with periodic maintenance during writes and occasionally during reads, as discussed below.   Caches built with CacheBuilder do not perform cleanup and evict values "automatically," or instantly after a value expires, or anything of the sort.    Instead, it performs small amounts of maintenance during write operations, or during occasional read operations if writes are rare.  

另一篇

google guava中有cache包,此包提供内存缓存功能。内存缓存需要考虑很多问题,包括并发问题,缓存失效机制,内存不够用时缓存释放,缓存的命中率,缓存的移除等等。 当然这些东西guava都考虑到了。

guava中使用缓存需要先声明一个CacheBuilder对象,并设置缓存的相关参数,然后调用其build方法获得一个Cache接口的实例。请看下面的代码和注释,注意在注释中指定了Cache的各个参数。

    public static void main(String[] args) throws ExecutionException, InterruptedException{         //缓存接口这里是LoadingCache,LoadingCache在缓存项不存在时可以自动加载缓存         LoadingCache<Integer,Student> studentCache                 //CacheBuilder的构造函数是私有的,只能通过其静态方法newBuilder()来获得CacheBuilder的实例                 = CacheBuilder.newBuilder()                 //设置并发级别为8,并发级别是指可以同时写缓存的线程数                 .concurrencyLevel(8)                 //设置写缓存后8秒钟过期                 .expireAfterWrite(8, TimeUnit.SECONDS)                 //设置缓存容器的初始容量为10                 .initialCapacity(10)                 //设置缓存最大容量为100,超过100之后就会按照LRU最近虽少使用算法来移除缓存项                 .maximumSize(100)                 //设置要统计缓存的命中率                 .recordStats()                 //设置缓存的移除通知                 .removalListener(new RemovalListener<Object, Object>() {                     @Override                     public void onRemoval(RemovalNotification<Object, Object> notification) {                         System.out.println(notification.getKey() + " was removed, cause is " + notification.getCause());                     }                 })                 //build方法中可以指定CacheLoader,在缓存不存在时通过CacheLoader的实现自动加载缓存                 .build(                         new CacheLoader<Integer, Student>() {                             @Override                             public Student load(Integer key) throws Exception {                                 System.out.println("load student " + key);                                 Student student = new Student();                                 student.setId(key);                                 student.setName("name " + key);                                 return student;                             }                         }                 );         for (int i=0;i<20;i++) {             //从缓存中得到数据,由于我们没有设置过缓存,所以需要通过CacheLoader加载缓存数据             Student student = studentCache.get(1);             System.out.println(student);             //休眠1秒             TimeUnit.SECONDS.sleep(1);         }         System.out.println("cache stats:");         //最后打印缓存的命中率等 情况         System.out.println(studentCache.stats().toString());     }

以上程序的输出如下:

load student 1 Student{id=1, name=name 1} Student{id=1, name=name 1} Student{id=1, name=name 1} Student{id=1, name=name 1} Student{id=1, name=name 1} Student{id=1, name=name 1} Student{id=1, name=name 1} Student{id=1, name=name 1} 1 was removed, cause is EXPIRED load student 1 ...... Student{id=1, name=name 1} Student{id=1, name=name 1} Student{id=1, name=name 1} Student{id=1, name=name 1} cache stats: CacheStats{hitCount=17, missCount=3, loadSuccessCount=3, loadExceptionCount=0, totalLoadTime=1348802, evictionCount=2}

看看到在20此循环中命中次数是17次,未命中3次,这是因为我们设定缓存的过期时间是写入后的8秒,所以20秒内会失效两次,另外第一次获取时缓存中也是没有值的,所以才会未命中3次,其他则命中。

guava的内存缓存非常强大,可以设置各种选项,而且很轻量,使用方便。另外还提供了下面一些方法,来方便各种需要:

ImmutableMap<K, V> getAllPresent(Iterable<?> keys) 一次获得多个键的缓存值put和putAll方法向缓存中添加一个或者多个缓存项invalidate 和 invalidateAll方法从缓存中移除缓存项asMap()方法获得缓存数据的ConcurrentMap<K, V>快照cleanUp()清空缓存refresh(Key) 刷新缓存,即重新取缓存数据,更新缓存
转载请注明原文地址: https://www.6miu.com/read-6078.html

最新回复(0)