简体   繁体   English

将数据存储在没有数据库的Ruby on Rails中

[英]Store data in Ruby on Rails without Database

I have a few data values that I need to store on my rails app and wanted to know if there are any alternatives to creating a database table just to do this simple task. 我有一些数据值,我需要存储在我的rails应用程序,并想知道是否有任何替代创建数据库表只是为了完成这个简单的任务。

Background: I'm writing some analytics and dashboard tools for my ruby on rails app and i'm hoping to speed up the dashboard by caching results that will never change. 背景:我正在为rails我的ruby编写一些分析和仪表板工具,我希望通过缓存永远不会改变的结果来加速仪表板。 Right now I pull all users for the last 30 days, and re-arrange them so I can see the number of new users per day. 现在我拉了所有用户最近30天,并重新安排它们,这样我就可以看到每天新用户的数量。 It works great but takes quite a long time, in reality I should only need to calculate the most recent day and just store the rest of the array somewhere else. 它工作得很好但需要相当长的时间,实际上我只需要计算最近一天并将其余的数组存储在其他地方。

Where is the best way to store this array? 存储此阵列的最佳方法是哪里?

Creating a database table seems a bit overkill, and I'm not sure that global variables are the correct answer. 创建数据库表似乎有点矫枉过正,我不确定全局变量是否正确答案。 Is there a best practice for persisting data like this? 是否有最佳实践来保存这样的数据?

If anyone has done anything like this before let me know what you did and how it turned out. 如果有人做过这样的事情之前让我知道你做了什么以及结果如何。

Ruby has a built-in Hash-based key value store named PStore. Ruby有一个名为PStore的内置基于Hash的键值存储。 This provides simple file based, transactional persistance. 这提供了基于文件的简单事务持久性。

Using a lightweight database like sqlite shouldn't feel like an overkill. 使用像sqlite这样的轻量级数据库应该不会觉得有点过分。 Alternatively, you can use key-store solutions like tokyo cabinet or even store the array in a flat file manually but I really don't see any overkill in using sqlite. 或者,您可以使用像tokyo cabinet这样的密钥存储解决方案,甚至可以手动将数组存储在平面文件中,但我真的看不到使用sqlite有任何过度杀伤力。

If you've got a database already, it's really not a big deal to create a separate table for tracking this sort of thing. 如果你已经有了一个数据库,创建一个单独的表来跟踪这种事情真的不是什么大问题。 When doing reporting, it's often to your advantage to create derivative summary tables exactly like what you're describing. 在进行报告时,创建与您所描述的完全相同的派生汇总表通常对您有利。 You can update these as required using a simple SQL statement and there's no worry that your temporary store will somehow go away. 您可以使用简单的SQL语句根据需要更新这些内容,并且不必担心您的临时存储会以某种方式消失。

That being said, the type of report you're trying to generate is actually something that can be done in real-time except on extravagantly large data sets. 话虽这么说,您尝试生成的报告类型实际上可以实时完成,除非有大量数据集。 The key is to have indexes that describe the exact grouping operation you're trying to do. 关键是要有索引来描述您正在尝试的确切分组操作。 For instance, if you're grouping by calendar date, you can create a "date" field and sync it to the "created_at" time as required. 例如,如果您按日历日期分组,则可以创建“日期”字段并根据需要将其同步到“created_at”时间。 An index on this date field will make doing a GROUP BY created_date very quick: 此日期字段的索引将非常快速地执行GROUP BY created_date:

SELECT created_date AS on_date, COUNT(id) AS new_users FROM users GROUP BY created_date

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM