How can I cache my tweets for an hour in Rails 3?
I'm using the twitter gem to get my latest 5 tweets. They display on th开发者_开发百科e page just fine, but it is silly to go out and get the tweets on every page request. I thought it equally overkill to put the 5 tweets in MemCache since I'm not using MemCache for anything else.
So I decided to create a couple of constants, TWEETS and TWEET_TIME, in my ApplicationController. Then a before_filter method could check TWEET_TIME and then populate TWEETS if a certain amount of time had gone by. Likewise I can trap errors and keep the last tweets I grabbed in case there's a problem with Twitter.
Except I can't change these values because:
dynamic constant assignment
I can't put the values in @ variables, because those don't live from one request to another. What can I do to accomplish this? It seemed so simple when I started.
You could use class variables (@@variable), these are persistent across requests, or better yet write to the database.
精彩评论