Caching Valued Result in Rail Assistant Methods

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

If I store it in an instance variable, it will pollute the instances, so it doesn't look reasonable.

Any good place to store that kind of value? Or doing such heavy calculations in an assistant is a bad idea?

+7
source share
1 answer

Sometimes I use Rails cache to store such values. The code is as follows:

def helper_method Rails.cache.fetch('helper_value') do # calculate the value if it does not exist ... end end 
+12
source

All Articles