kylejginavan / youtube_it

An object-oriented Ruby wrapper for the YouTube GData API
http://groups.google.com/group/ruby-youtube-library
595 stars 223 forks source link

Caching results only possible with heavy serialization #22

Closed masterkain closed 13 years ago

masterkain commented 13 years ago

Hello, currently I'm trying to store results in a normal Rails.cache key. The only method that worked was to serialize the videos_by call directly to_yaml and then wrap in a YAML::load call, like so:

@youtube_response = YAML::load(Rails.cache.fetch(youtube_search.cache_key) { $youtube_client.videos_by(query: term).to_yaml })

The obvious error I get with no yaml involved:

@youtube_response = Rails.cache.fetch(youtube_search.cache_key) { $youtube_client.videos_by(query: term) }

is

TypeError (singleton can't be dumped).

Now, using yaml or other possible serialization that I haven't think of leads to a huge performance loss as from my tests, around 4 seconds just for retrieving from the cache and parse.

The solution might be that youtube_it implements proper serialization methods in code, like (links in order): http://stackoverflow.com/questions/4228252/rails-cache-and-un-dumpable-marshal-structures-how-to-cache-on-the-example-f https://github.com/hanklords/flickraw/issues#issue/16 https://github.com/hanklords/flickraw/commit/d09db4c5549e72b42eefbf1ea5c98f95bd1bd3d9

kylejginavan commented 13 years ago

Thanks for this for your continued contributions Claudio. However, I don't think this is possible to fix.

For example: "YouTubeIt::Model::Video:0x000001033e9988 @video_id="http://gdata.youtube.com/feeds/api/videos/oFT7vWyC1Ys" is not serializable.

For more information please read http://stackoverflow.com/questions/6391855/rails-cache-error-in-rails-3-1-typeerror-cant-dump-hash-with-default-proc Specifically "Some objects cannot be dumped: if the objects to be dumped include bindings, procedure or method objects, instances of class IO, or singleton objects, a TypeError will be raised."

Thanks, Kyle

masterkain commented 13 years ago

I thought we can create a good snapshot hash out of the various classes instance methods by using Marshal's marshal_dump/marshal_load or _dump/_load to ease the serialization with at least some caching backend, I'll do a try

kylejginavan commented 13 years ago

I'm adding Faraday to the gem. Found a bug when using Net:HTTP. Should solve this bug and help you on your way to the serialization.