简体   繁体   English

替代常规宝石红宝石方法的动作缓存?

[英]Alternate for action caching for normal gem ruby method?

Lets take an example. 让我们举个例子。

def get_data(param1,param2,param3)
  #do something
  #make some api calls
end

This method not belongs to controller. 此方法不属于控制器。 I am using this method in my ruby class in my gem.How to cache the result of method based on parameter coming.Something similar to action caching in rails comes with expiration ability. 我在我的gem的ruby类中使用了这个方法。如何基于参数的来缓存方法的结果。类似于rails中的动作缓存的方法具有到期功能。

You can use this low level caching: 您可以使用以下低级缓存:

def competing_price
    Rails.cache.fetch("#{param1}-#{param2}-#{param3}", expires_in: 12.hours) do
      # do something.. 
    end
end

It has nothing related that you have to do it in controller or model only. 它没有任何关系,只需要在控制器或模型中进行。 you can use it anywhere else like your method. 您可以在其他任何方式(例如您的方法)中使用它。

Checkout the reference: http://guides.rubyonrails.org/caching_with_rails.html 查看参考: http : //guides.rubyonrails.org/caching_with_rails.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM