Cache Youtube API XML response with Nokogiri error - marshalling error?

I would like to minimize my calls to the Youtube API, as this data is often not updated. When I try to cache this:

Rails.cache.fetch("youtube-#{@yt_name}", :expires_in => 1.day) do
  @youtube_doc = Nokogiri::XML(open("https://gdata.youtube.com/feeds/api/users/#{@yt_name}/uploads"))
end

I get an error in heroku:

Marshalling error for key 'youtube-NAME': no marshal_dump is defined for class Nokogiri::XML::NodeSet 
You are trying to cache a Ruby object which cannot be serialized to memcached.

Any ideas?

+3
source share
1 answer

You may want to cache XML, not a Nokogiri object. Try the following:

xml = Rails.cache.fetch("youtube-#{@yt_name}", :expires_in => 1.day) do
  open("https://gdata.youtube.com/feeds/api/users/#{@yt_name}/uploads").read
end
@youtube_doc = Nokogiri::XML(xml)
+5
source

All Articles