Can't modify frozen hash memcached

Hi all,

I was starting to implement caching with memcached in one of our
projects
that still uses Rails 2.3 (yes, still).

As Rails only caches Active Record objects for one request, I thought of
caching them on memcached. For that, I’ve defined a method that
intercepts
the find method of Active Record and checks if the object is in the
cache
before hitting the actual find of Active Record.

Although, I’m getting an error of “Can’t modify a frozen hash” after
retrieving an object from the cache and trying to update it. I’ve done
some
research and found that objects are frozen by AR before they get saved.

I found the following hack on google that solves the problem:

if Rails.version <= “2.3.8”
module ActiveRecord
class Base
def dup
obj = super
obj.instance_variable_set(’@attributes’,
instance_variable_get(’@attributes’).dup)
obj
end
end
end
end

I have two questions about it:

  • Is it a good solution to use dup like that?

  • Is it a bad/good practice to cache Active Record objects?

Thanks in advance,

Tales Marchesan Chaves
Software Designer @Hewlett-Packard Brazil