Skip to content

Instantly share code, notes, and snippets.

@cantlin
Created March 28, 2011 23:44
Show Gist options
  • Save cantlin/891560 to your computer and use it in GitHub Desktop.
Save cantlin/891560 to your computer and use it in GitHub Desktop.
# "Empty" results when this model method resides in a cache block
# app/models/user.rb
def following(cursor = -1)
Rails.cache.fetch("following/#{id}", :expires_in => 1.hour) do
result = twitter_request_authenticated('get_following', {:cursor => cursor})
followed_users = parse_body(result)['users']
twitter_user_list = []
followed_users.each do |user|
this_user = TwitterUser.new(:twitter_id => user['id'], :screen_name => user['screen_name'], :name => user['name'], :image => user['profile_image_url'], :location => user['location'])
this_user.status = user['status']['text'] unless user['status'].nil?
twitter_user_list.push this_user
end
twitter_user_list
end
end
# If we add a Rails.cache.delete, it works as expected
def following(cursor = -1)
Rails.cache.delete("following/#{id}")
Rails.cache.fetch("following/#{id}", :expires_in => 1.hour) do
result = twitter_request_authenticated('get_following', {:cursor => cursor})
...snip...
end
end
# app/views/users/show.html.erb
<%= render :partial => 'users/followed_user', :collection => @current_user.following %>
# app/views/users/_followed_user.html.erb
<h2><%= followed_user['screen_name'] %></h2>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment