diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/lib/cached_fetch.rb	Fri Jul 14 22:42:20 2023 +0900
@@ -0,0 +1,17 @@
+module CachedFetch
+  def self.timeline(user_id)
+    cached("timeline:#{user_id}") { LegitClient.timeline(user_id)&.[](:timeline) }
+  end
+
+  def self.user_by_id(user_id)
+    cached("user_by_id:#{user_id}") { LegitClient.user_by_id(user_id)&.[](:user) }
+  end
+
+  def self.user_by_username(username)
+    cached("user_by_username:#{username}") { LegitClient.user_by_username(username)&.[](:user) }
+  end
+
+  def self.cached(key, &block)
+    Rails.cache.fetch(key, expires_in: (15 + rand(15)).minutes, &block)
+  end
+end