IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

Google Guava V11 中的Cache操作

龙浩的blog 2012-04-07 14:43:40 累计浏览 2,348 次
本机暂存

Google Guava cache的主要功能点:

     * 你需要给现有系统加速;

         * 有些key需要不知一次的查询获取;

         * 从策略上讲,你的应用需要从策略上把所有的value从cache中清理出来 — 你试图减少重复的工作;[注:weakKey , weakValue]

         * cache仅仅存储在内存中,没有在文件中或者其他的server上面,如果不满足你的需求,可以考虑Memcached

API的两种调用方式

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

 /**
       * 不需要延迟处理(泛型的方式封装)
       * @return
       */
      public static  LoadingCache cached(CacheLoader cacheLoader) {
            LoadingCache 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 commonCache(final String key) throws Exception{
            return cached(new CacheLoader(){
                  @Override
                  public String load(String key) throws Exception {
                        return null;
                  }
            });
      }

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

/**
       * 对需要延迟处理的可以采用这个机制;(泛型的方式封装)
       * @param 
       * @param 
       * @param key
       * @param callable
       * @return V
       * @throws Exception
       */
      public static  Cache callableCached(Callable callable) throws Exception {
            Cache 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 callableCache(final String key) throws Exception{
            return callableCached(new Callable(){
                  @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  

同分类推荐文章

  1. 等了十年的 Go 链式管道,终于来了:seq 让你像写 Scala 一样写 Go (2026-06-25 18:38:18)
  2. Go 实验特性详解 (2026-06-21 10:05:27)
  3. amd64 微架构级别对 Go 程序性能提升多少? (2026-06-21 09:38:49)

查看更多 后端 文章 →

建议继续学习

  1. SmartSprites - 命令行形式的CSS Sprites生成器 (累计阅读 123,898)
  2. Java开发岗位面试题归类汇总 (累计阅读 22,159)
  3. android 开发入门 (累计阅读 19,531)
  4. 浅析http协议、cookies和session机制、浏览器缓存 (累计阅读 17,448)
  5. 分布式缓存系统 Memcached 入门 (累计阅读 16,246)
  6. 30分钟3300%性能提升――python+memcached网页优化小记 (累计阅读 13,744)
  7. 我的PHP,Python和Ruby之路 (累计阅读 13,150)
  8. HashMap解决hash冲突的方法 (累计阅读 12,657)
  9. 关于memcache分布式一致性hash (累计阅读 11,821)
  10. 淘宝图片存储架构 (累计阅读 10,963)