]> cat aescling's git repositories - mastodon.git/commitdiff
Reset status cache when status_stat or media_attachment updates (#9119)
authorEugen Rochko <eugen@zeonfederated.com>
Sun, 28 Oct 2018 05:42:34 +0000 (06:42 +0100)
committerGitHub <noreply@github.com>
Sun, 28 Oct 2018 05:42:34 +0000 (06:42 +0100)
* Reset status cache when status_stat or media_attachment updates

Fix #8711

Media attachments are generally immutable, but admins can update
the sensitive flag, and this would ensure the change is visible
instantly. Same for updates to status stats. That is a regression
from #8185, because even the correct updated_at fetched from a join
doesn't seem to invalidate the cache.

* Remove join from Status#cache_ids since it has no effect

app/models/media_attachment.rb
app/models/status.rb
app/models/status_stat.rb

index 1e4fae3de74e3acd7e0857de3810c51d7b222b16..1bfe02fd68a0370ff41bdcccc198f58d14ed9ea5 100644 (file)
@@ -130,6 +130,7 @@ class MediaAttachment < ApplicationRecord
     "#{x},#{y}"
   end
 
+  after_commit :reset_parent_cache, on: :update
   before_create :prepare_description, unless: :local?
   before_create :set_shortcode
   before_post_process :set_type_and_extension
@@ -230,4 +231,9 @@ class MediaAttachment < ApplicationRecord
       bitrate: movie.bitrate,
     }
   end
+
+  def reset_parent_cache
+    return if status_id.nil?
+    Rails.cache.delete("statuses/#{status_id}")
+  end
 end
index cb2c010407778324031a7297e7c073da76e2b518..32fedb924aefccfcae680b7101ee0d1b3146b6f0 100644 (file)
@@ -240,10 +240,6 @@ class Status < ApplicationRecord
   before_validation :set_local
 
   class << self
-    def cache_ids
-      left_outer_joins(:status_stat).select('statuses.id, greatest(statuses.updated_at, status_stats.updated_at) AS updated_at')
-    end
-
     def selectable_visibilities
       visibilities.keys - %w(direct limited)
     end
index 9d358776b4d1bd6283d4415925fd5b59b19de80f..024c467e7150ffc055093fed9cb98612ffdb087f 100644 (file)
 
 class StatusStat < ApplicationRecord
   belongs_to :status, inverse_of: :status_stat
+
+  after_commit :reset_parent_cache
+
+  private
+
+  def reset_parent_cache
+    Rails.cache.delete("statuses/#{status_id}")
+  end
 end