about summary refs log tree commit diff
path: root/app/models
diff options
context:
space:
mode:
authorDavid Yip <yipdw@member.fsf.org>2017-10-15 02:52:53 -0500
committerDavid Yip <yipdw@member.fsf.org>2017-10-21 14:54:36 -0500
commitb4b657eb1daebd1472384ff8ea1c1b9c4b313c5c (patch)
tree94cd904550b90225f95aa2f21d79602b86b1cfa2 /app/models
parent693c66dfde891a5d540dc4cdc0c712495c31100c (diff)
Invalidate cached matcher objects on KeywordMute commit. #164.
Diffstat (limited to 'app/models')
-rw-r--r--app/models/keyword_mute.rb16
1 files changed, 12 insertions, 4 deletions
diff --git a/app/models/keyword_mute.rb b/app/models/keyword_mute.rb
index f94e0f795..8b54ad696 100644
--- a/app/models/keyword_mute.rb
+++ b/app/models/keyword_mute.rb
@@ -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('|')