first time trying caching in Rails, don't even know where to begin -
first time trying implement caching app, , find confusing. after reading this indept article , still pretty lost.
i decided start out simple. in test app i'm making, have following chunk of api call.
def get_champion champion_url = "#{base_url}/api/lol/static-data/na/v1.2/champion#{api_key}" parse_json(champion_url) end
i think best place me cache because data returned call static.
what best method me cache here? there many caching techniques described in article, , don't know 1 use.
if there technique think should try implement, please let me know, , i'll try best implement it.
ps: right now, server logs says
completed 200 ok in 2860ms (views: 2858.6ms | activerecord: 0.3ms)
which high, now?
edit: after reading through rails doc, thinking low-level cache might appropriate scenerio.
class product < activerecord::base def competing_price rails.cache.fetch("#{cache_key}/competing_price", expires_in: 12.hours) competitor::api.find_price(id) end end end
in code provided,it said cache_key needs generated. how did generate cache_key?
low-level caching work, believe resides in memory, , not persisted. if don't want keep in memory time, next best thing write disk , serve cached file whenever request made.
to that, you'll need add action_caching gem gemfile (only rails 4.0 , later):
gem 'actionpack-action_caching'
then add following controller:
caches_action :get_champion
that's it. first time request made, conduct full request , output cache file. subsequent requests retrieve cache file.
see https://github.com/rails/actionpack-action_caching more info.
Comments
Post a Comment