> What would be the best way in rails to store a single variable and have
> it persist. Like a have a variable that says "total_visits" that counts
> the total number of page loads.
>
> The only solution I can think of is having a table with a single row.
One answer would be yes, pretty much if you really want to store a
single data point that's accessible to a number of instances of Rails,
then either a file or a database is what you'll need to use (assuming
you don't trust anything RAM based).
However, your specific scenario could quickly become an extreme
bottleneck. It would be better served by logging (preferably
asynchronously), and then read & tally the log to create your total. Do
it periodically, so you're not starting from the first log every every
time.
There's probably other ways, but using a single var for something like
page loads is looking for trouble as your app scales. If you know the
page requests will be low enough to not be a bottleneck then using the
DB with row or table locking (effectively the same for this case) might
be OK.
Another option if you can tolerate the potential for a little inaccuracy
is to go ahead and use RAM (with a mutex) to keep the count, and
periodically write that value to disk to minimize the disk access. If
you have load balanced app servers you'll need a central RAM cache like
memcache.
That's what comes to my mind -- others may have better ideas.
-- gw
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
No comments:
Post a Comment