3

我想尽量减少对 Youtube API 的调用,因为这些数据不会经常刷新。当我尝试缓存这个时:

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

我在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.

有任何想法吗?

4

1 回答 1

5

您可能想要缓存 XML 而不是 Nokogiri 对象。试试这个:

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)
于 2012-04-27T21:56:55.800 回答