comparison app/models/tweet.rb @ 152:fd7344643903

Better? cache key
author nanaya <me@nanaya.pro>
date Sat, 17 Feb 2018 20:53:00 +0900
parents 7ca8aeba1a63
children 1abfa910d83e
comparison
equal deleted inserted replaced
151:f0ac0c99af36 152:fd7344643903
13 13
14 def cache_expires_time 14 def cache_expires_time
15 (15 + rand(15)).minutes 15 (15 + rand(15)).minutes
16 end 16 end
17 17
18 def cache_key
19 "timeline:v2:#{@twitter_id}/#{Base64.urlsafe_encode64 @twitter_id.to_s}"
20 end
21
18 def timeline 22 def timeline
19 if @timeline.nil? 23 if @timeline.nil?
20 raw = Rails.cache.fetch("timeline:v2:#{@twitter_id}", :expires_in => cache_expires_time) do 24 raw = Rails.cache.fetch(cache_key, :expires_in => cache_expires_time) do
21 client_try(:user_timeline, @twitter_id, TIMELINE_OPTIONS).tap do |data| 25 client_try(:user_timeline, @twitter_id, TIMELINE_OPTIONS).tap do |data|
22 if data[:result] == :ok 26 if data[:result] == :ok
23 if data[:data].any? && data[:data].first.user.id != @twitter_id 27 if data[:data].any? && data[:data].first.user.id != @twitter_id
24 wrong_user = data[:data].first.user 28 wrong_user = data[:data].first.user
25 Rails.logger.warn "Wrong timeline data. Requested: #{@twitter_id}, got: #{wrong_user.id} (#{wrong_user.name.printable})" 29 Rails.logger.warn "Wrong timeline data. Requested: #{@twitter_id}, got: #{wrong_user.id} (#{wrong_user.name.printable})"