使用Google收藏品MapMaker? [英] Use of Google-collections MapMaker?

查看:165
本文介绍了使用Google收藏品MapMaker?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现了在SO中的答案,它是提到Google收藏集 MapMaker 是真棒。我通过文档,但不能真正弄清楚我可以在哪里使用它。任何一个点可以使用一个MapMaker适合的场景。

I just came across this answer in SO where it is mentioned that the Google-collections MapMaker is awesome.I went through the documentation but couldn't really figure out where i can use it.Can any one point out some scenario's where it would be appropriate to use a MapMaker.

推荐答案

以下是使用 MapMaker

private final ConcurrentMap<Long, Foo> fooCache = new MapMaker()
    .softValues()
    .makeComputingMap(new Function<Long, Foo>() {
          public Foo apply(Long id) {
            return getFooFromServer(id);
          }
        });

 public Foo getFoo(Long id) {
   return fooCache.get(id);
 }

get(id)在地图上调用,它将返回该ID的映射中的 Foo ,或者从服务器检索它,将其缓存,把它返还。一旦建立起来,我就不用再想了。另外,由于我设置了 softValues(),缓存不能填满并导致内存问题,因为系统能够根据内存需求清除条目。如果从地图中清除缓存的值,那么可以在下次需要时再次询问服务器!

When get(id) is called on the map, it'll either return the Foo that is in the map for that ID or it'll retrieve it from the server, cache it, and return it. I don't have to think about that once it's set up. Plus, since I've set softValues(), the cache can't fill up and cause memory issues since the system is able to clear entries from it in response to memory needs. If a cached value is cleared from the map, though, it can just ask the server for it again the next time it needs it!

事情就是这样一种可以使用的方法。使地图使用强,弱或软键和/或值的选项,以及在特定时间段之后删除条目的选项,可以让您做很多事情。

The thing is, this is just one way it can be used. The option to have the map use strong, weak or soft keys and/or values, plus the option to have entries removed after a specific amount of time, lets you do lots of things with it.

这篇关于使用Google收藏品MapMaker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆