Caching Twitter Results with Dally

I am new to Ruby (and Rails) and was hoping you could help resolve some of the confusion I encountered.

I am trying to integrate Twitter on my website to get the latest user twist and capture a link to their profile picture. The gem works fine until, as it seems to me, the 100th API call in an hour, after which Twitter will disconnect you.

From what I compiled, I need to cache the result for ~ 1 minute using memcache. There was a big pseudocode here , but unfortunately it was a little over my head. I was hoping I could get some more details.

At the moment, I'm not sure where I can place this code? I want to display twitter information in the layout view of the application, so that it goes to the method in the application_helper.rb file?

My best attempt to find out was to lead to the following code that throws an "Missing Helper File" error.

module ApplicationHelper require "memcache" def twitter cache = MemCache.new twitter = cache.get("twitter").first if twitter.nil? begin twitter = Twitter.user("TwitterName") cache.set("twitter", twitter, :expires_in => 1.minute) if twitter rescue twitter = default end end return twitter end end 
+4
source share
1 answer

First enable caching and memcache for your environment (e.g. config / environment / production.rb)

 # Use a different cache store in production config.cache_store = :mem_cache_store 

Then in the view you want to show tweets do something like this

 <% cache("tweets", :expires_in => 42.minutes) do %> <% Twitter.user_timeline("User").each do %> ..... <% end %> <% end %> 
+5
source

All Articles