Invalidate cached matcher objects on KeywordMute commit. #164.
parent
aa57929688
commit
be472f276d
|
@ -15,16 +15,24 @@ class KeywordMute < ApplicationRecord
|
||||||
|
|
||||||
validates_presence_of :keyword
|
validates_presence_of :keyword
|
||||||
|
|
||||||
def self.matcher_for(account)
|
after_commit :invalidate_cached_matcher
|
||||||
Rails.cache.fetch("keyword_mutes:matcher:#{account}") { Matcher.new(account) }
|
|
||||||
|
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
|
end
|
||||||
|
|
||||||
class Matcher
|
class Matcher
|
||||||
attr_reader :regex
|
attr_reader :regex
|
||||||
|
|
||||||
def initialize(account)
|
def initialize(account_id)
|
||||||
re = [].tap do |arr|
|
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)
|
arr << Regexp.escape(m.keyword.strip)
|
||||||
end
|
end
|
||||||
end.join('|')
|
end.join('|')
|
||||||
|
|
Loading…
Reference in New Issue