缓存评估结果是rails helper方法

shi*_*eya 7 ruby-on-rails ruby-on-rails-3

在实现一些辅助方法时,有时,我想将一些计算结果存储在辅助方法可以作为缓存访问的地方.

如果我将它存储到实例变量中,它将污染实例,因此这样做似乎不明智.

有存储这种价值的好地方吗?或者在帮手中做这么重的计算是个坏主意?

Yan*_*hao 12

有时我使用Rails缓存来存储这种值.代码是这样的,

def helper_method
  Rails.cache.fetch('helper_value') do
    # calculate the value if it does not exist
    ...
  end
end
Run Code Online (Sandbox Code Playgroud)