IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    Google Guava V11 中的Cache操作

    longhao (longtask@gmail.com)发表于 2012-04-05 01:46:03
    love 0

    Google Guava cache的主要功能点:

        * 你需要给现有系统加速;
        * 有些key需要不知一次的查询获取;
        * 从策略上讲,你的应用需要从策略上把所有的value从cache中清理出来 -- 你试图减少重复的工作;[注:weakKey , weakValue]
        * cache仅仅存储在内存中,没有在文件中或者其他的server上面,如果不满足你的需求,可以考虑Memcached

    API的两种调用方式

    1:普通的调用方式,通过key得到value的时间较短

     /**
           * 不需要延迟处理(泛型的方式封装)
           * @return
           */
          public static <K , V> LoadingCache<K , V> cached(CacheLoader<K , V> cacheLoader) {
                LoadingCache<K , V> cache = CacheBuilder.newBuilder()
                            .maximumSize(10000)
                            .weakKeys()
                            .softValues()
                            .refreshAfterWrite(120, TimeUnit.SECONDS)
                            .expireAfterWrite(10, TimeUnit.MINUTES)
                            // .removalListener(RemovalListeners.asynchronous(listener,executor))
                            // .removalListener(MY_LISTENER)
                            .build(cacheLoader);
                return cache;
          }
          /**
           * 通过key获取value
           * 调用方式 commonCache.get(key) ; return String
           * @param key
           * @return
           * @throws Exception
           */
          public static LoadingCache<String , String> commonCache(final String key) throws Exception{
                return cached(new CacheLoader<String , String>(){
                      @Override
                      public String load(String key) throws Exception {
                            return null;
                      }
                });
          }

    2:延迟加载的处理方式,通过key得到value的时间较长

    /**
           * 对需要延迟处理的可以采用这个机制;(泛型的方式封装)
           * @param <K>
           * @param <V>
           * @param key
           * @param callable
           * @return V
           * @throws Exception
           */
          public static <K,V> Cache<K , V> callableCached(Callable<V> callable) throws Exception {
                Cache<K, V> cache = CacheBuilder.newBuilder()
                            .maximumSize(10000)
                            .expireAfterWrite(10, TimeUnit.MINUTES)
                            // .removalListener(RemovalListeners.asynchronous(listener,executor))
                            // .removalListener(MY_LISTENER)
                            // .weakKeys()
                            // .weakValues()
                            .build();
                return cache;
          }
          /**
           * 通过callable的方式获取value;
           * 调用方式 callableCache.get(key) ; return String
           * @param key
           * @return
           * @throws Exception
           */
          public static Cache<String , String> callableCache(final String key) throws Exception{
                return callableCached(new Callable<String>(){
                      @Override
                      public String call() throws Exception {
                            return null;
                      }
                });
          }

         回收的参数设置

        1. 大小的设置:CacheBuilder.maximumSize(long)  CacheBuilder.weigher(Weigher)  CacheBuilder.maxumumWeigher(long)
        2. 时间:expireAfterAccess(long, TimeUnit) expireAfterWrite(long, TimeUnit)
        3. 引用:CacheBuilder.weakKeys() CacheBuilder.weakValues()  CacheBuilder.softValues()
        4. 明确的删除:invalidate(key)  invalidateAll(keys)  invalidateAll()
        5. 删除监听器:CacheBuilder.removalListener(RemovalListener)

         refresh机制

        1. LoadingCache.refresh(K)  在生成新的value的时候,旧的value依然会被使用。
        2. CacheLoader.reload(K, V) 生成新的value过程中允许使用旧的value
        3. CacheBuilder.refreshAfterWrite(long, TimeUnit) 自动刷新cache

         未来要实现的功能

        1. 更多统计信息,通过Cache.stats()来获取统计类CacheStats,例如缓存命中率,缓存获取实践统计等
        2. asMap,把缓存动作一个ConcurrentMap


    参考资料 :http://code.google.com/p/guava-libraries/wiki/CachesExplained#Inserted_Directly  



沪ICP备19023445号-2号
友情链接