annotate app/lib/cached_fetch.rb @ 234:7a773720d81f legit-client

Totally legit client
author nanaya <me@nanaya.net>
date Fri, 14 Jul 2023 22:42:20 +0900
parents
children c800506ae799
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)
7a773720d81f Totally legit client
nanaya <me@nanaya.net>
parents:
diff changeset
15 Rails.cache.fetch(key, expires_in: (15 + rand(15)).minutes, &block)
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