How to implement a canonicalizing mapping in Java?

心不动则不痛 提交于 2019-12-20 09:40:37

问题


I am currently rolling my own little ORM, and find myself faced with the task of creating a canonicalizing mapping in order to prevent loading the same entity from the database more than once.

My current approach is to use a HashMap<Object, WeakReference<Object>>. The key is the primary key of the mapped database-entity (an ArrayList<Object> if it is a composite key), and the values are WeakReference<Object>.

My main problem is how to clean the map up? When an object is not used any more, the weak reference in the map will go null, and I will only discover this on the next lookup (or never, if I don't look the object up again). I could make the weak references register with a ReferenceQueue when they get cleared, and then check that queue every time I look something up. The cleared reference would not give me any hint as to which object was cleared though, so I guess I would have to subclass WeakReference to store the key in the map, so I can remove it after the reference was cleared.

Is this the way to go, or is there any simpler way to implement this?


回答1:


I would recommend using Guava's MapMaker, or the CacheBuilder in r10.

They allow automatic* time- and size-based eviction, as well as supporting weak keys or values. (The upcoming CacheBuilder promises to be specially tailored to this kind of use case.)

So you can initialize your map:

ConcurrentMap<Key, Object> cache = new MapMaker()
        .weakValues()
        .makeMap();

And the immediate benefit will be that when a value is garbage collected, the whole entry will be removed. Furthermore, you can use a computing map:

ConcurrentMap<Key, Object> cache = new MapMaker()
        .weakValues()
        .makeComputingMap(loadFunction);

where loadFunction is a Function<Key, Object> that loads an object from the database. The advantage of this is that the map will handle concurrent requests for a particular object, ensuring the query is only called once. Additionally, the requesting code needs only call get() and can always expect the object back, whether from cache or the database.

These examples are using MapMaker - I haven't had the pleasure to toy with CacheBuilder yet.

See my question my ideal cache using guava for more examples. That post discusses how to combine time-based eviction with canonicalization.



来源:https://stackoverflow.com/questions/7436765/how-to-implement-a-canonicalizing-mapping-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!