annotate app/lib/cached_fetch.rb @ 260:e2150dce4e90

Rubocop (rails) time
author nanaya <me@nanaya.net>
date Sun, 15 Dec 2024 22:59:09 +0900
parents c800506ae799
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
234
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
1 module CachedFetch
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
2 def self.timeline(user_id)
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
3 cached("timeline:#{user_id}") { LegitClient.timeline(user_id)&.[](:timeline) }
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
4 end
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
5
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
6 def self.user_by_id(user_id)
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
7 cached("user_by_id:#{user_id}") { LegitClient.user_by_id(user_id)&.[](:user) }
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
8 end
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
9
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
10 def self.user_by_username(username)
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
11 cached("user_by_username:#{username}") { LegitClient.user_by_username(username)&.[](:user) }
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
12 end
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
13
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
14 def self.cached(key, &block)
239
c800506ae799 More varied cache period for less chance of hitting rate limit
nanaya <me@nanaya.net>
parents: 234
diff changeset
15 Rails.cache.fetch(key, expires_in: (15 + rand(60)).minutes, &block)
234
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
16 end
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
17 end