comparison app/lib/cached_fetch.rb @ 245:2935c5e52a71

No more official twitter api
author nanaya <me@nanaya.net>
date Mon, 17 Jul 2023 04:23:09 +0900
parents c800506ae799
children
comparison
equal deleted inserted replaced
233:0f0cc55ff11b 245:2935c5e52a71
1 module CachedFetch
2 def self.timeline(user_id)
3 cached("timeline:#{user_id}") { LegitClient.timeline(user_id)&.[](:timeline) }
4 end
5
6 def self.user_by_id(user_id)
7 cached("user_by_id:#{user_id}") { LegitClient.user_by_id(user_id)&.[](:user) }
8 end
9
10 def self.user_by_username(username)
11 cached("user_by_username:#{username}") { LegitClient.user_by_username(username)&.[](:user) }
12 end
13
14 def self.cached(key, &block)
15 Rails.cache.fetch(key, expires_in: (15 + rand(60)).minutes, &block)
16 end
17 end