有状态的 Rails 应用程序。存储不在数据库中的数据
我有一个 Rails 应用程序,其中包括聊天功能。一切正常,但现在我想将最后 50 条聊天消息存储在某个地方。如果重新加载页面,我需要它显示最后的消息。我不想使用数据库。将其存储在某种数组中会很好,但 Rails 是无状态的。我希望在你的帮助下让它变得更加有状态。
谢谢
UPD:
我找到了PStore
(http://www.ruby-doc.org/stdlib/libdoc/pstore/rdoc/classes/PStore.html )。它看起来很适合我,不是吗?
I have a Rails application, that includes chat. Everything works fine, but now I want to store the last 50 chat messages somewhere. I need it to show the last messages if a page is reloaded. I don't want to use database. It would be good to store it in some kind of array, but Rails is stateless. I'm looking to make it a little more stateful with your help.
Thx
UPD:
I've found PStore
( http://www.ruby-doc.org/stdlib/libdoc/pstore/rdoc/classes/PStore.html ). It looks pretty good for me, doesn't it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最简单的答案是 Marshal,因为它是 Ruby 核心的一部分。只需将实际数组作为二进制文件转储到磁盘并根据需要读回即可。
如果您希望文件格式易于人类阅读,请尝试 YAML (Ruby 标准库的一部分)或 JSON 作为 gem。两者都是纯文本文件格式,您可以转储到文件、查看并再次加载。
您说您“不需要数据库”,但没有说明原因。您是否知道 SQLite 的数据库位于单个文件中,易于安装,并且快速且轻量级?
Your simplest answer is Marshal, since it is part of the Ruby core. Just dump your actual array to disk as a binary file and read it back as you need.
If you want the file format to be human-readable, try YAML (part of the Ruby standard library) or JSON as a gem. Both are plain-text file formats that you can dump to file, see, and load again.
You say you "don't want a database", but you don't say why. Do you know that SQLite has its database in a single file, is easy to install, and is fast and lightweight?
你应该看看 Redis
You should look at Redis