具有较弱值的HashMap [英] HashMap with weak values

查看:188
本文介绍了具有较弱值的HashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为持久存储的对象实现缓存。这个想法是:


  • 方法 getObjectFromPersistence(long id); ///花费大约3秒

  • 方法 getObjectFromCache(long id)//立即



使用以下伪代码: getObject(long id) p>

  synchronized(this){
CustomObject result = getObjectFromCache(id)
if(result == null){
result = getObjectFromPersistence(id);
addToCache(result);
}
返回结果;
}

但是我需要让垃圾收集器收集CustomObject。到目前为止,我正在为实现使用 HashMap< Long,WeakReference< CustomObject> 。问题在于,随着时间的推移,HashMap变成了空的 WeakReferences

我已经检查过 WeakHashMap ,但那里的键很弱(而且这些值仍然是强引用),所以WeakReferences中的长整型没有意义。



解决这个问题的最佳解决方案是什么?是否有一些逆WeakHashMap或类似的东西?



谢谢 解决方案

p>您可以使用番石榴 MapMaker 为此:

  ConcurrentMap< Long,CustomObject>图表=新的MapMaker()
.weakValues()
.makeMap();

您甚至可以通过替换 makeMap()

  .makeComputingMap(
new函数< Long,CustomObject>(){
public CustomObject apply(Long id){
return getObjectFromPersistence(id);
}
});

因为你写的看起来很像缓存,所以更新,更专业 Cache (通过 CacheBuilder )可能与您更相关。它没有直接实现 Map 接口,但提供了更多的缓存控件。



你可以参考这个了解如何为CacheBuilder工作,这里是一个例子快速访问:

  LoadingCache< Integer,String> cache = CacheBuilder.newBuilder()
.maximumSize(100)
.expireAfterWrite(10,TimeUnit.MINUTES)
.build(
new CacheLoader< Integer,String>(){
@Override
public String load(Integer id)throws Exception {
returnvalue;
}
}
);


I'm implementing a cache for Objects stored persistently. The idea is:

  • Method getObjectFromPersistence(long id); ///Takes about 3 seconds
  • Method getObjectFromCache(long id) //Instantly

And have a method: getObject(long id) with the following pseudocode:

synchronized(this){
    CustomObject result= getObjectFromCache(id)
    if (result==null){
       result=getObjectFromPersistence(id);
       addToCache(result);
    }
    return result;
}

But I need to allow the CustomObject to be collected by the garbage collector. Until now I was using an HashMap<Long,WeakReference<CustomObject> for the implementation. The problem is that over the time the HashMap becomes filled of empty WeakReferences.

I've checked WeakHashMap but there the keys are weak (and the values are still strong references) so having the longs with WeakReferences have no sense.

Whats the best solution for solving this problem? Is there some "inverse WeakHashMap" or something similar?

Thanks

解决方案

You can use the Guava MapMaker for this:

ConcurrentMap<Long, CustomObject> graphs = new MapMaker()
   .weakValues()
   .makeMap();

You can even include the computation part by replacing makeMap() with this:

   .makeComputingMap(
       new Function<Long, CustomObject>() {
         public CustomObject apply(Long id) {
           return getObjectFromPersistence(id);
         }
       });

Since what you are writing looks a lot like a cache, the newer, more specialized Cache (built via a CacheBuilder) might be even more relevant to you. It doesn't implement the Map interface directly, but provides even more controls that you might want for a cache.

You can refer to this for a detailed how to work for CacheBuilder and here is an example for fast access:

LoadingCache<Integer, String> cache = CacheBuilder.newBuilder()
   .maximumSize(100)
   .expireAfterWrite(10, TimeUnit.MINUTES)
   .build(
       new CacheLoader<Integer, String>() {
           @Override
           public String load(Integer id) throws Exception {
               return "value";
           }
       }
   ); 

这篇关于具有较弱值的HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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