12
返回列表 发新帖
楼主: jieforest

Guava库的Cache详解

[复制链接]
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
11#
 楼主| 发表于 2014-10-18 13:23 | 只看该作者
8. Preload the Cache

We can insert multiple records in our cache using putAll() method. In the following example, we add multiple records into our cache using a Map:
  1. @Test
  2. public void whenPreloadCache_thenUsePutAll() {
  3.     CacheLoader<String, String> loader;
  4.     loader = new CacheLoader<String, String>() {
  5.         @Override
  6.         public String load(String key) {
  7.             return key.toUpperCase();
  8.         }
  9.     };

  10.     LoadingCache<String, String> cache;
  11.     cache = CacheBuilder.newBuilder().build(loader);

  12.     Map<String, String> map = new HashMap<String, String>();
  13.     map.put("first", "FIRST");
  14.     map.put("second", "SECOND");
  15.     cache.putAll(map);

  16.     assertEquals(2, cache.size());
  17. }
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
12#
 楼主| 发表于 2014-10-18 13:23 | 只看该作者
9. RemovalNotification

Sometimes, you need to take some actions when a record is removed from the cache; so, let’s discuss RemovalNotification.

We can register a RemovalListener to get notifications of a record being removed. We also have access to the cause of the removal – via the getCause() method.

In the following sample, a RemovalNotification is recieved when the forth element in the cache because of its size:
  1. @Test
  2. public void whenEntryRemovedFromCache_thenNotify() {
  3.     CacheLoader<String, String> loader;
  4.     loader = new CacheLoader<String, String>() {
  5.         @Override
  6.         public String load(final String key) {
  7.             return key.toUpperCase();
  8.         }
  9.     };

  10.     RemovalListener<String, String> listener;
  11.     listener = new RemovalListener<String, String>() {
  12.         @Override
  13.         public void onRemoval(RemovalNotification<String, String> n){
  14.             if (n.wasEvicted()) {
  15.                 String cause = n.getCause().name();
  16.                 assertEquals(RemovalCause.SIZE.toString(),cause);
  17.             }
  18.         }
  19.     };

  20.     LoadingCache<String, String> cache;
  21.     cache = CacheBuilder.newBuilder()
  22.       .maximumSize(3)
  23.       .removalListener(listener)
  24.       .build(loader);

  25.     cache.getUnchecked("first");
  26.     cache.getUnchecked("second");
  27.     cache.getUnchecked("third");
  28.     cache.getUnchecked("last");
  29.     assertEquals(3, cache.size());
  30. }
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
13#
 楼主| 发表于 2014-10-18 13:24 | 只看该作者
10. Notes

Finally, here are a few additional quick notes about the Guava cache implementation:

1) it is thread-safe

2) you can insert values manually into the cache using put(key,value)

3) you can measure your cache performance using CacheStats ( hitRate(), missRate(), ..)

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
14#
 楼主| 发表于 2014-10-18 13:24 | 只看该作者
11. Conclusion

We went through a lot of usecases of the Guava Cache in this tutorial – from simple usage to eviction of elements, refresh and preload of the cache and removal notifications.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
15#
 楼主| 发表于 2014-10-18 13:25 | 只看该作者
over.

使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表