Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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. 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. 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.

like image 324
Schneems Avatar asked May 04 '10 14:05

Schneems


2 Answers

Ruby has a built-in Hash-based key value store named PStore. This provides simple file based, transactional persistance.

  • PStore documentation
like image 163
John Topley Avatar answered Sep 19 '22 12:09

John Topley


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.

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. An index on this date field will make doing a GROUP BY created_date very quick:

SELECT created_date AS on_date, COUNT(id) AS new_users FROM users GROUP BY created_date
like image 28
tadman Avatar answered Sep 20 '22 12:09

tadman