Home Rails.cache.fetch does not work for array of json
Reply: 0

Rails.cache.fetch does not work for array of json

user1863
1#
user1863 Published in July 19, 2018, 3:34 pm

I am trying to cache server API response using Rail.cache.fetch feature but it seems like just always 'nil' instead cached data. Method below runs one time, then always fails with 'access to nil class' kind error when I am trying to go through @my_api_data_ variable

def index
  api_statuses_

end
def api_statuses_
  Rails.cache.fetch('api_repsonse_m_', expires_in: 3.minute) do


      @mypairs_ = ['ticker1','ticker2', 'ticker3', 'ticker4', 'ticker5', 'ticker6', 'ticker7', 'ticker8', 'ticker9', 'ticker10']

      @my_api_data_ = []
      @my_api_data_poplr = []
      url = 'https://myserver.com/ticker/'

      @mypairs_.each do |item_|
        uri = URI(url + item_)
        response = Net::HTTP.get(uri)
        begin
          #response is like this {"status":"valu1","time":"valu2","data":"valu3"}
          myresult = JSON.parse(response)
        rescue JSON::ParserError

        end
        #Rails.logger.info myresult
        @my_api_data_.push(myresult);
      end

  end
end

I hope maybe someone knows why it goes bad each time

I tried to add line "@my_api_data_" right before the end of cache block. This is the same error I got on original circumstances:

I, [2018-01-13T19:29:14.384766 #5716]  INFO -- : Processing by Private::MydataController#index as HTML
I, [2018-01-13T19:29:14.403774 #5716]  INFO -- :   Rendered private/mydata/index.html.slim within layouts/mydatalayout_ (0.7ms)
I, [2018-01-13T19:29:14.403919 #5716]  INFO -- : Completed 500 Internal Server Error in 19ms
F, [2018-01-13T19:29:14.405077 #5716] FATAL -- : 
ActionView::Template::Error (undefined method `each' for nil:NilClass):
    37:         .row
    38:           /! Top head coin-boxes
    39:           .market-top-coin-boxes
    40:             - @my_api_data_.each do |pair_data|
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.366282 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO