]> cat aescling's git repositories - mastodon.git/commitdiff
Invalidate cached matcher objects on KeywordMute commit. #164.
authorDavid Yip <yipdw@member.fsf.org>
Sun, 15 Oct 2017 07:52:53 +0000 (02:52 -0500)
committerDavid Yip <yipdw@member.fsf.org>
Sat, 21 Oct 2017 19:54:36 +0000 (14:54 -0500)
app/models/keyword_mute.rb

index f94e0f7951c4be9379bc1b79a8085270862d25b9..8b54ad696eb9c4f0a5b931a8afa6492a5ec1b0c1 100644 (file)
@@ -15,16 +15,24 @@ class KeywordMute < ApplicationRecord
 
   validates_presence_of :keyword
 
-  def self.matcher_for(account)
-    Rails.cache.fetch("keyword_mutes:matcher:#{account}") { Matcher.new(account) }
+  after_commit :invalidate_cached_matcher
+
+  def self.matcher_for(account_id)
+    Rails.cache.fetch("keyword_mutes:matcher:#{account_id}") { Matcher.new(account_id) }
+  end
+
+  private
+
+  def invalidate_cached_matcher
+    Rails.cache.delete("keyword_mutes:matcher:#{account_id}")
   end
 
   class Matcher
     attr_reader :regex
 
-    def initialize(account)
+    def initialize(account_id)
       re = [].tap do |arr|
-        KeywordMute.where(account: account).select(:keyword, :id).find_each do |m|
+        KeywordMute.where(account_id: account_id).select(:keyword, :id).find_each do |m|
           arr << Regexp.escape(m.keyword.strip)
         end
       end.join('|')
This page took 0.025082 seconds and 3 git commands to generate.