带有 dict 的 gen_server vs mnesia 表 vs ets

发布于 2024-08-19 23:27:35 字数 469 浏览 4 评论 0原文

我正在构建一个 erlang 服务器。 用户向服务器发送http请求来更新他们的状态。 服务器上的http请求过程将用户状态消息保存在内存中。 服务器每分钟都会将所有消息发送到远程服务器并清除内存。 如果用户在一分钟内多次更新其状态,则最后一条消息将覆盖前一条消息。 重要的是,在读取所有消息并清除它们之间,没有其他进程会执行此操作。 能够写入状态消息。

实施它的最佳方法是什么?

  1. 带有字典的gen_server。关键是用户 ID。 dict:store/3 将更新或创建状态。 gen_server 解决了“交易”问题。

  2. 带有 ram_copies 的 mnesia 表。处理事务,我不需要实现 gen_server。这个解决方案的开销是否太大?

  3. ETS表更轻量并且有gen_server。可以在ETS中进行交易吗?要在读取所有消息和清除它们之间锁定表吗?

谢谢

I'm building an erlang server.
Users sends http requests to the server to update their status.
The http request process on the server saves the user status message in memory.
Every minute the server sends all messages to a remote server and clear the memory.
If a user update his status several times in a minute, the last message overrides the previous one.
It is important that between reading all the messages and clearing them no other process will
be able to write a status message.

What is the best way to implement it?

  1. gen_server with a dict. The key will be the userid. dict:store/3 will update or create the status. The gen_server solves the 'transaction' issue.

  2. mnesia table with ram_copies. Handle transactions and I don't need to implement a gen_server. Is there too much overhead with this solution?

  3. ETS table which is more light weight and have a gen_server. Is it possible to do the transaction in ETS? To lock the table between reading all the messages and clearing them?

Thanks

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

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

发布评论

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

评论(1

花开半夏魅人心 2024-08-26 23:27:35

由于您手动进行同步,记忆力就太多了。显然你不需要网络的东西,这是 ets 和 mnesia 之间的主要区别。

据我了解,Ets 只是围绕 dict/bag/... 的 otp 兼容流程,并且由于您有多个进程访问数据,因此您应该使用 ets。

我为自己想出了以下逻辑:

Multiple processes on multiple VMs -> mnesia
Multiple processes on one VM -> ets/dets
One process -> bag/dict/...

Since you do the syncing manually, mnesia is to much. You clearly don't need the networking stuff, thats the main difference between ets and mnesia.

Ets, as far as I undertand, is just a otp compliant process around a dict/bag/..., and since you have multiple processes accessing your data, you should use ets.

I came up with the following logic for myself:

Multiple processes on multiple VMs -> mnesia
Multiple processes on one VM -> ets/dets
One process -> bag/dict/...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文