带有 dict 的 gen_server vs mnesia 表 vs ets
我正在构建一个 erlang 服务器。 用户向服务器发送http请求来更新他们的状态。 服务器上的http请求过程将用户状态消息保存在内存中。 服务器每分钟都会将所有消息发送到远程服务器并清除内存。 如果用户在一分钟内多次更新其状态,则最后一条消息将覆盖前一条消息。 重要的是,在读取所有消息并清除它们之间,没有其他进程会执行此操作。 能够写入状态消息。
实施它的最佳方法是什么?
带有字典的gen_server。关键是用户 ID。 dict:store/3 将更新或创建状态。 gen_server 解决了“交易”问题。
带有 ram_copies 的 mnesia 表。处理事务,我不需要实现 gen_server。这个解决方案的开销是否太大?
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?
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.
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?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您手动进行同步,记忆力就太多了。显然你不需要网络的东西,这是 ets 和 mnesia 之间的主要区别。
据我了解,Ets 只是围绕 dict/bag/... 的 otp 兼容流程,并且由于您有多个进程访问数据,因此您应该使用 ets。
我为自己想出了以下逻辑:
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: