You are talking about ActiveRecord Query Caching. That should work in Rake-Tasks too, provided you're running them in an environment with caching enabled, e.g. production.
See Rails Guide on Caching for examples.

I think that might only be true in controllers. There's text here: "it’s important to note that query caches are created at the start of an action and destroyed at the end of that action and thus persist only for the duration of the action." You should look at your environment log (i.e. log/test.log, if you take the above answer) and you'll see the repeated SQL queries.
–
chug2kJun 20 '13 at 19:57

The SQL cache isn't enabled per default for rake tasks. You can wrap your code in a cache block, like this:

task :foobar => :environment do
ActiveRecord::Base.connection.cache do
User.find 1 # Will hit the db
User.find 1 # Will hit the cache
end
end

This is essentially what Rails does for controller actions. Note that the cache uses memory and rake tasks has a tendency to work with large sets of data, which might give you issues. You can selectively opt-out of caching for parts of your code, using uncached