My Rails 3 app solves a variant of the board game ‘Boggle’. Each request involves thousands of dictionary lookups. What’s the best way to persist the large dictionary object between requests?
My dictionary is implemented in a Trie, which works great. The problem is, the Trie takes about 30 seconds to build (big dictionary). How can I persist one dictionary between requests? The Dictionary is just a plain Ruby object, not ActiveRecord.
I’d like to keep the dictionary in the Rails app in this case, even though I suspect the standard approach is to delegate dictionary lookups to another process (is it?).
Thanks.
It’s perfectly reasonable to have a plain old ruby object or class act as a Rails model.
If your dictionary is implemented with class methods then that becomes very easy. Move it into app/models and Rails will autoload the class and make it available to your controllers. Add an initializer to config/initializers and you can ensure the the dictionary is loaded when your Rails app starts up.
If your dictionary is actually an instance of a Dictionary class then this isn’t as helpful. You’re probably best off initializing it as a global constant. Yes I know that we’re all trained to wince at the idea of globals these days; but the fact is that a single (presumably immutable) application dictionary does in fact need to be loaded into memory and globally available. Just make sure that you name (and maybe namespace) it in a way that’s likely to avoid confusion or conflicts.