Forum: Ruby on Rails Are the hashes faster than activerecord finds ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
77707eb62b77a12638d1d0a9424afdc9?d=identicon&s=25 Nuno (Guest)
on 2006-05-15 12:43
Hello, I need to store somewhere one hundred of static ressources text
field (internationalization).

I wonder if it better to put them in a database or in a hash defined at
app startup (in environment.rb)

I would go to the second one, but am I right ?

Thanks
F5c118aaeb5bb7162db613a37e595be8?d=identicon&s=25 Mick Sharpe (micksharpe)
on 2006-05-15 13:46
Hashes will be faster. Why not load a hash from a table in
environment.rb?
B780ee0ee1480454a85df58536702f63?d=identicon&s=25 Alder Green (Guest)
on 2006-05-15 14:39
(Received via mailing list)
On 5/15/06, Nuno <nomail@novalidatall991.com> wrote:
> Hello, I need to store somewhere one hundred of static ressources text
> field (internationalization).
>
> I wonder if it better to put them in a database or in a hash defined at
> app startup (in environment.rb)
>
> I would go to the second one, but am I right ?
>
> Thanks

The easy answer is: benchmark :)

Hashes of reasonable sizes that you keep in memory will be faster than
just about anything else. So, if there are some keyed pieces of
information that you use often and all over the place, you indeed
might want to put them inside a Hash constant loaded through
environment.rb. The Hash could be either plain Ruby Hash (e.g. a
constants.rb in /lib, then "require lib/constants" in environment.rb)
or something you load through Yaml (which is a bit cleaner).

Keeping the Hash in memory would be faster, but there's a memory cost.
So if the Hash becomes large, this attitude loses its appeal. Also,
it's a good solution for read-only pieces of information, *not* for
freely r/w operation (stuff like being thread-safe, locking etc. which
the database has for saving you from yourself). And if you don't use
that Hash that often, consider an alternative or at least think about
clearing the memory space (by deleting all references to the Hash).
88f69d680829d977e3585725fccea8fe?d=identicon&s=25 Guest (Guest)
on 2006-05-16 00:56
Thank you for your answer, I'll go the hash way (loaded from the DB at
app startup)

The hash (which will be read only BTW) will not be that huge so I
presume that the memory cost will not be to high and will be shared
accross all http session
This topic is locked and can not be replied to.