views:

479

answers:

3

ActiveRecord query caching is enabled when a controller's action is invoked. I also know that you can do something like this to temporarily enable caching for a given Model for a given code block:

User.cache do  
    ....
end

But is there a way to enable query caching for other contexts (e.g. when a script is run under the rails environment using ./script/runner)?

+1  A: 

An ActiveRecord query cache lives only for the duration of a particular action (i.e. request). If you want a cached object to survive longer or be used between processes you need to look at something like memcached.

Jeremy
A: 

This plugin will persist the query cache using memcached.

pantulis
A: 

A simple solution is to wrap the code given to script/runner in a block, directly on the commandline:

script/runner "User.cache { ... }"

Notice the caching is not simply for the User model, but for all queries performed within the codeblock.

laust.rud