HashMap with weak values

前端 未结 7 2366
悲&欢浪女
悲&欢浪女 2021-02-18 14:03

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

  • Method getObjectFromPersistence(long id); ///Takes about 3 seconds
  • M
7条回答
  •  鱼传尺愫
    2021-02-18 14:31

    I think the best option (if a dependency on Guava is undesirable) would be to use a custom subclass of WeakReference that remembers its ID, so that your cleanup thread can remove the weak values during cleanup of the WeakReferences.

    The implementation of the weak reference, with the necessary ReferenceQueue and cleanup thread would look something like this:

    class CustomObjectAccess {
    
        private static final ReferenceQueue releasedCustomObjects = 
                                                                      new ReferenceQueue<>();
    
        static {
            Thread cleanupThread = new Thread("CustomObject cleanup thread")                  
                while (true) {
                    CustomObjectWeakReference freed = (CustomObjectWeakReference) 
                                    CustomObjectWeakReference.releasedCustomObjects.remove();
                    cache.remove(freed.id);
                }
            };
            cleanupThread.start();
        }
    
        private Map cache;
    
        public CustomObject get(CustomObjectID id) {
            synchronized(this){
                CustomObject result= getFromCache(id);
                if (result==null) {
                    result=getObjectFromPersistence(id);
                    addToCache(result);
                }
            }
            return result;
        }
    
        private addToCache(CustomObject co) {
            cache.put(CustomObject.getID(), new CustomObjectWeakReference(co));
        }
    
        private getFromCache(CustomObjectID id) {
            WeakReference weak = cache.get(id);
            if (weak != null) {
                return weak.get();
            }
            return null;
        }
    
        class CustomObjectWeakReference extends WeakReference {
    
            private final CustomObjectID id;
    
            CustomObjectWeakReference(CustomObject co) {
                super(co, releasedCustomObjects);
                this.id = co.getID();
            }
        }
    }
    

提交回复
热议问题