On 5/15/06, Nuno email@example.com wrote:
Hello, I need to store somewhere one hundred of static ressources text
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 ?
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).