I’ve got a tiny model (let’s call it “Node”) that represents a tree-like structure. Each node contains only a name and a reference to its father:
class Node < ActiveRecord::Base
validates_presence_of :name, :parent_id
end
The table isn’t very big – less than 100 elements. It’s updated rarely – in the last 4 months 20 new elements were added, in one occasion, by the site admin.
Yet it is used quite a lot on my application. Given its tree-like structure, on some occasions a request triggers more than 30 database hits (including ajax calls, which I use quite a lot).
I’d like to use some sort of caching in order to lower the database access – since the table is so small, I thought about caching all registers in memory.
Is this possible rails 2.3? Is there a better way to deal with this?
Why don’t you just load them all every time to avoid getting hit with multiple loads?
Here’s a simple example:
This will give you a hash indexed by Node#id so you can use this cache in place of a find call:
For small, simple records, loading them in quickly in one fetch is a fairly inexpensive operation.