comparison app/models/tweet.rb @ 97:ff5557f9902c

Avoid uniformly expiring cache
author nanaya <me@myconan.net>
date Thu, 28 Jul 2016 07:08:52 +0900
parents cf9a9ce2d034
children c1d5d8be92c6
comparison
equal deleted inserted replaced
96:148d8c17cb79 97:ff5557f9902c
1 class Tweet 1 class Tweet
2 def cache_expires_time
3 (10 + rand(15)).minutes
4 end
5
2 def timeline 6 def timeline
3 start_client_config_id = @client_config_id 7 start_client_config_id = @client_config_id
4 8
5 begin 9 begin
6 @timeline ||= 10 @timeline ||=
7 Rails.cache.fetch({ :timeline => @twitter_id }, :expires_in => 20.minutes) do 11 Rails.cache.fetch({ :timeline => @twitter_id }, :expires_in => cache_expires_time) do
8 @client.user_timeline(@twitter_id, :count => 40, :exclude_replies => false, :include_rts => true) 12 @client.user_timeline(@twitter_id, :count => 40, :exclude_replies => false, :include_rts => true)
9 end 13 end
10 rescue Twitter::Error::TooManyRequests 14 rescue Twitter::Error::TooManyRequests
11 next_client_config_id = @client_config_id + 1 15 next_client_config_id = @client_config_id + 1
12 init_client next_client_config_id 16 init_client next_client_config_id
22 def user 26 def user
23 @user ||= 27 @user ||=
24 if timeline.any? 28 if timeline.any?
25 timeline.first.user 29 timeline.first.user
26 else 30 else
27 Rails.cache.fetch({ :user => @twitter_id }, :expires_in => 20.minutes) do 31 Rails.cache.fetch({ :user => @twitter_id }, :expires_in => cache_expires_time) do
28 @client.user(@twitter_id) 32 @client.user(@twitter_id)
29 end 33 end
30 end 34 end
31 end 35 end
32 36