简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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