Thursday, August 8, 2013

rails active record caching in memory

rails active record caching in memory

I have a model which has through relationships to another model through
two foreign key relationships. I do a lot of lookups on those tables
(which are 3-4K rows) while importing a lot of data, and I'm trying to
eliminate spurious repeated database lookups. (Ideally, my database would
be doing async writes/INSERTs only)
I have played with doing my own caching by ID, and recently switched to
using Rails.cache (with MemoryStore for now. I have no need to sync
against other instances, and I'm ram rich on the import machine). However,
I find that I am getting multiple copies of the same associated records,
and I'd like to get rid of this.
For instance:
irb> p = Phone.includes([:site => :client, :btn => :client]).first.
irb> p.site.client.object_id => 67190640 irb> p.btn.client.object_id =>
67170780.
Ideally, I'd like these to point to the same object in memory. Rails.cache
would serialize things in/out, which really just makes this worse, but I
was surprised by this. Could I override find_by_id() or some such in a way
that the association proxies would make use of my cache?
Maybe there is another caching module that I'm missing?
(please note that there is no web front end involved in this process. It's
all models and ORM)

No comments:

Post a Comment