如何在Java中实现规范化映射? [英] How to implement a canonicalizing mapping in Java?

查看:341
本文介绍了如何在Java中实现规范化映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在滚动自己的小ORM,发现自己面临创建规范化映射的任务,以防止从数据库中多次加载同一实体.

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.

我当前的方法是使用HashMap<Object, WeakReference<Object>>.该键是映射的数据库实体的主键(如果为复合键,则为ArrayList<Object>),其值为WeakReference<Object>.

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>.

我的主要问题是如何清理地图?当不再使用某个对象时,映射中的弱引用将变为null,而我只会在下一次查找时发现它(或者,如果我不再查找该对象,则永远不会).当弱引用被清除时,我可以使它们在ReferenceQueue中注册,然后在每次查找时检查该队列.清除的引用不会提示我清除了哪个对象,因此我想我必须继承WeakReference子类以将键存储在映射中,因此可以在清除引用后将其删除.

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?

推荐答案

我建议使用番石榴的

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

它们允许自动 * 时间和大小基于驱逐,以及支持弱键或弱值. (即将发布的CacheBuilder承诺将专门针对此类用例进行定制.)

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);

其中,loadFunction是从数据库中加载对象的Function<Key, Object>.这样做的好处是,映射将处理对特定对象的并发请求,从而确保查询仅被调用一次.此外,发出请求的代码只需要调用get(),并且总是可以返回对象,无论是从缓存还是从数据库中获取.

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.

这些示例正在使用MapMaker-我还没有玩过CacheBuilder玩具的乐趣.

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.

这篇关于如何在Java中实现规范化映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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