使用 Java 存储许多对象

发布于 2024-10-22 06:10:36 字数 389 浏览 2 评论 0 原文

我正在联系这里的社区,了解是否有一种方法可以在 Java 映射结构中存储许多对象(> 500K)。我知道,如果我使用 Java Collections API,并且我正在寻找其他解决方案(分布式缓存之外),那么在某些时候我的应用程序将超出其内存占用量。我的意图是存储一些达到 Map> 效果的东西。

要求:

  • 结构是不可变的(一次加载到地图中)。仅读取访问权限。
  • 对地图的访问需要相当快,但不寻求低延迟检索,更关心在内存中存储和保留这些对象。

有谁知道我可以利用这样一个库来实现这一点,或者有人遇到过需要在 Java 内存中加载许多对象的解决方案吗?我很想听听您的反馈。

谢谢。

I'm reaching out to the community here to understand if there is a way to store many objects in a Java map structure (> 500K). I know at some point my application will exceed its memory footprint if I use the Java Collections API and I'm looking for other solutions (outside of distributed caching). My intention is to store something to the effect of Map<String, List<String>>.

Requirements:

  • Structure will be immutable (one time load into the map). Only read access.
  • Access to the map needs to be fairly quick but not looking for low latency retrievals, more concerned about storing and retaining these objects in memory.

Does anyone know of such a library I can utilize that can achieve this or has anyone ever come across a solution in which they were required to load many objects in memory in Java? I'd be very interested in hearing your feedback.

Thanks.

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

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

发布评论

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

评论(4

这个俗人 2024-10-29 06:10:36

EhCache 非常适合此目的。最基本的是,它提供了一个键值映射、可选的溢出到磁盘以及可选的 JVM 重新启动持久性。它将把最常用的元素保留在内存中。

EhCache would be perfect for this. At its most basic, it offers a key-value map, with optional overflow to disk, and optional persistence over JVM restarts. It will keep elements in memory that are most frequently used.

强辩 2024-10-29 06:10:36

我和斯卡夫曼在一起。除了溢出到磁盘之外,EhCache 还允许您将缓存放置在进程内但堆外。如果您的缓存确实很大,这可能会对性能产生非常积极的影响,因为它减少了 GC 的压力。

然而,这个特定功能必须付费,否则 EhCache 是免费的。

除了 EhCache,Java 中还有一些其他缓存提供类似甚至有时甚至更高级的选项,例如 Infinispan 或 OsCache。

I'm with skaffman. In addition to the overflow to disk EhCache offers you to place the cache in-process but off-heap. If your cache is really big, this may have a very positive impact on performance since it reduces stress on the GC.

This specific feature however must be payed for, but otherwise EhCache is free.

Instead of EhCache, there are a couple of other caches in Java that offer similar or sometimes even more advanced options like Infinispan or OsCache.

冷夜 2024-10-29 06:10:36

您是否考虑过只读数据库,例如 java cdb: http://www.strangegizmo .com/products/sg-cdb/

Have you considered a read-only database, such as a java cdb: http://www.strangegizmo.com/products/sg-cdb/

墨洒年华 2024-10-29 06:10:36

H2 数据库引擎的 MVStore 功能

虽然我还没有尝试过,但 H2 数据库引擎 除了关系数据库引擎之外,还提供专门的键值存储。该商店名为 MVStore。虽然由 H2 内部使用,但文档建议您可以直接将其用于键值存储。

MVStore 允许您通过 java.util.Map 接口,不使用 JDBC 或 SQL。

示例用法,取自文档:

import org.h2.mvstore.*;

// open the store (in-memory if fileName is null)
MVStore s = MVStore.open(fileName);

// create/get the map named "data"
MVMap<Integer, String> map = s.openMap("data");

// add and read some data
map.put(1, "Hello World");
System.out.println(map.get(1));

// close the store (this will persist changes)
s.close();

阅读完整文档以了解其性能概况、并发性以及在内存中缓存同时持久保存到存储的能力。

MVStore feature of H2 Database Engine

While I have not tried it, the H2 Database Engine offers a specialized key-value store besides its relational database engine. This store is called MVStore. While used internally by H2, the documentations suggest you may use it directly for key-value storage.

MVStore lets you interact through the java.util.Map interface, without using JDBC or SQL.

Example usage, taken from the doc:

import org.h2.mvstore.*;

// open the store (in-memory if fileName is null)
MVStore s = MVStore.open(fileName);

// create/get the map named "data"
MVMap<Integer, String> map = s.openMap("data");

// add and read some data
map.put(1, "Hello World");
System.out.println(map.get(1));

// close the store (this will persist changes)
s.close();

Read the thorough documentation to learn about its performance profile, concurrency, and ability to cache in-memory while also persisting to storage.

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