about summary refs log tree commit diff
diff options
context:
space:
mode:
authorFire Demon <firedemon@creature.cafe>2020-08-28 09:55:14 -0500
committerFire Demon <firedemon@creature.cafe>2020-08-30 05:45:20 -0500
commit8bf2aaf0e9e8290bcbc979471638d2e69b2d73b5 (patch)
tree1d433812232486f6e88d19b94403e94417175bfc
parenta4c56505f41a191a21e595c26060510df3f2b909 (diff)
[Performance] Cache results of formatting
-rw-r--r--app/lib/command_tag/processor.rb1
-rw-r--r--app/lib/formatter.rb18
-rw-r--r--app/services/update_status_service.rb1
3 files changed, 20 insertions, 0 deletions
diff --git a/app/lib/command_tag/processor.rb b/app/lib/command_tag/processor.rb
index 5a1f6a68d..f758994a0 100644
--- a/app/lib/command_tag/processor.rb
+++ b/app/lib/command_tag/processor.rb
@@ -232,6 +232,7 @@ class CommandTag::Processor
       Rails.cache.delete_matched("statuses/#{status.id}-*")
       Rails.cache.delete("statuses/#{status.id}")
       Rails.cache.delete(status)
+      Rails.cache.delete_matched("format:#{status.id}:*")
       redis.zremrangebyscore("spam_check:#{status.account.id}", status.id, status.id)
     end
   end
diff --git a/app/lib/formatter.rb b/app/lib/formatter.rb
index 6673f4b4b..5b8098ceb 100644
--- a/app/lib/formatter.rb
+++ b/app/lib/formatter.rb
@@ -32,6 +32,12 @@ class Formatter
   include ActionView::Helpers::TextHelper
 
   def format(status, **options)
+    Rails.cache.fetch(formatter_cache_key(status, options), expires_in: 1.hour) do
+      uncached_format(status, options)
+    end
+  end
+
+  def uncached_format(status, options)
     summary = nil
     raw_content = status.proper.text
     summary_mode = false
@@ -436,5 +442,17 @@ class Formatter
   def mention_html(account)
     "<span class=\"h-card\"><a href=\"#{encode(ActivityPub::TagManager.instance.url_for(account))}\" class=\"u-url mention\">@<span>#{encode(account.username)}</span></a></span>"
   end
+
+  def formatter_cache_key(status, options)
+    [
+      'format',
+      status.id.to_s,
+      options[:article_content]     ? '1' : '0',
+      options[:inline_poll_options] ? '1' : '0',
+      options[:plaintext]           ? '1' : '0',
+      options[:autoplay]            ? '1' : '0',
+      options[:custom_emojify]      ? '1' : '0',
+    ].join(':')
+  end
 end
 # rubocop:enable Metrics/ClassLength
diff --git a/app/services/update_status_service.rb b/app/services/update_status_service.rb
index 355e8ca97..348c2f66c 100644
--- a/app/services/update_status_service.rb
+++ b/app/services/update_status_service.rb
@@ -137,6 +137,7 @@ class UpdateStatusService < BaseService
     Rails.cache.delete_matched("statuses/#{@status.id}-*")
     Rails.cache.delete("statuses/#{@status.id}")
     Rails.cache.delete(@status)
+    Rails.cache.delete_matched("format:#{@status.id}:*")
     redis.zremrangebyscore("spam_check:#{@account.id}", @status.id, @status.id)
   end