diff 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
line wrap: on
line diff
--- a/app/models/tweet.rb	Thu Jul 28 04:25:47 2016 +0900
+++ b/app/models/tweet.rb	Thu Jul 28 07:08:52 2016 +0900
@@ -1,10 +1,14 @@
 class Tweet
+  def cache_expires_time
+    (10 + rand(15)).minutes
+  end
+
   def timeline
     start_client_config_id = @client_config_id
 
     begin
       @timeline ||=
-        Rails.cache.fetch({ :timeline => @twitter_id }, :expires_in => 20.minutes) do
+        Rails.cache.fetch({ :timeline => @twitter_id }, :expires_in => cache_expires_time) do
           @client.user_timeline(@twitter_id, :count => 40, :exclude_replies => false, :include_rts => true)
         end
     rescue Twitter::Error::TooManyRequests
@@ -24,7 +28,7 @@
       if timeline.any?
         timeline.first.user
       else
-        Rails.cache.fetch({ :user => @twitter_id }, :expires_in => 20.minutes) do
+        Rails.cache.fetch({ :user => @twitter_id }, :expires_in => cache_expires_time) do
           @client.user(@twitter_id)
         end
       end