如何在Java中实现规范化映射?

发布于 2024-12-05 08:31:55 字数 509 浏览 5 评论 0 原文

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

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

我的主要问题是如何清理地图?当不再使用某个对象时,映射中的弱引用将变为 null,并且我只会在下一次查找时发现这一点(或者永远不会,如果我不再查找该对象) )。当弱引用被清除时,我可以将它们注册到ReferenceQueue,然后在每次查找内容时检查该队列。清除的引用不会给我任何关于清除了哪个对象的提示,所以我想我必须子类化 WeakReference 才能将密钥存储在映射中,这样我就可以在引用被清除后将其删除已清除。

这是要走的路,还是有更简单的方法来实现这个?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光清浅 2024-12-12 08:31:55

我建议使用 Guava 的 MapMaker,或 CacheBuilder

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

因此您可以初始化您的映射:

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

直接的好处是,当一个值被垃圾收集时,整个条目都将被垃圾回收。已删除。此外,您可以使用计算映射:

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

其中 loadFunction 是从数据库加载对象的 Function。这样做的优点是映射将处理特定对象的并发请求,确保查询仅被调用一次。此外,请求代码只需调用 get() 并且始终可以期望返回对象,无论是来自缓存还是数据库。

这些示例使用 MapMaker - 我还没有愉快地尝试过 CacheBuilder

有关更多示例,请参阅我的问题我使用番石榴的理想缓存。该文章讨论了如何将基于时间的驱逐与规范化结合起来。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文