Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching evaluated result in rails helper methods

When implementing some helper methods, sometimes, I want to store some computed result somewhere accessible from the helper method as a cache.

If I store it into instance variable, it will pollute instances, so it looks like not wise to do that.

Any good place to store such value? Or doing such heavy weight computing in a helper is a bad idea?

like image 369
shigeya Avatar asked Oct 23 '12 01:10

shigeya


1 Answers

Sometimes I use Rails cache to store this kind of values. The code is like this,

def helper_method
  Rails.cache.fetch('helper_value') do
    # calculate the value if it does not exist
    ...
  end
end
like image 95
Yanhao Avatar answered Nov 11 '22 15:11

Yanhao